How to stop an object from moving past a point
<p>I was wondering if there was a way to stop an object from moving off the screen. In my code, I have a controllable character (player) and I want to prevent it from moving to the left when it's x coordinate is &lt; 1</p>

<p>I have tried to do this in the code below, however, the player will not be stopped if you hold down the left arrow key.</p>

<p>Is there a way to fix this? If I had a guess, I would somehow need my program to continuously check for the case where player.x &lt; 1</p>

<pre><code>motionx = 0; -- Variable used to move character along x axis
speed = 10; -- Set Walking Speed


local function moveplayer (event)
    player.x = player.x + motionx;
end

Runtime:addEventListener("enterFrame", moveplayer)

local function onKeyEvent( event )
    if ( event.phase == "down" ) then
        if ( event.keyName == "left" ) and player.x &gt; 1 then
            motionx = -speed
            return true
        elseif ( event.keyName == "right" ) then
            motionx = speed
            return true
        end
    end
end

Runtime:addEventListener( "key", onKeyEvent )
</code></pre>