Using while loop crashes Corona

I’m trying to move my sprite forward by key pressing but when i want the sprite to move constant forwards by using a while loop Corona crashes everytime.

Can you share some code?  My hunch is that you are triggering a whole bunch loops with each key press.  Are you using event.phase to control the key events?  That might help.

[lua]

if event.phase == “down” then

     – do something

end

[/lua]

We you say move forward do you mean a change in the x-position of the sprite or a change in the sprite’s animation frame?

Depending on your answer, you might consider using enterFrame instead of a while loop.

Generally, you’re not going to use while loops to move objects. Your devices run millions of operations per second and a loop is just going to execute too fast to see anything visual. Also, those loops, if they don’t end correctly you can tie up your CPU and with Corona is single threaded, it will appear to lock up your device.

Use timers, enterFrame listeners, transitions, or physics to move objects.

Rob

local function walkPerson(event)   if (event.keyName == ‘d’ and event.phase == ‘down’) then   while ((event.keyName == ‘d’ and event.phase == ‘down’) == true) do   Sprite:setLinearVelocity(200,0)   return true   end   end   if (event.keyName == ‘a’ and event.phase == ‘down’) then   Sprite:setLinearVelocity(-200, 0)   return true   end   if(event.keyName == ‘space’) then   Sprite:applyLinearImpulse(0, -.25)   end  

end

That’s my code so how would I used a while loop to see if it would continue the sprite moving in one direction rather than constant pressing, when I press A it moves once.

You wouldn’t use a while loop. You need to keep track of whether the key is held down. If it’s been pressed, but not released, then it must still be down. As Rob says, look into enterFrame listeners which are functions that run once a frame. Check within here whether a variable like ‘isUpKeyHeld’ etc is true and perform your movement accordingly.

Please use code formatting when posting code. Click on the blue <> button in the row with Bold, Italic, etc. and copy/pasted your code into the popup window!

Thanks

Rob

Okay thanks a million for your help!

Can you share some code?  My hunch is that you are triggering a whole bunch loops with each key press.  Are you using event.phase to control the key events?  That might help.

[lua]

if event.phase == “down” then

     – do something

end

[/lua]

We you say move forward do you mean a change in the x-position of the sprite or a change in the sprite’s animation frame?

Depending on your answer, you might consider using enterFrame instead of a while loop.

Generally, you’re not going to use while loops to move objects. Your devices run millions of operations per second and a loop is just going to execute too fast to see anything visual. Also, those loops, if they don’t end correctly you can tie up your CPU and with Corona is single threaded, it will appear to lock up your device.

Use timers, enterFrame listeners, transitions, or physics to move objects.

Rob

local function walkPerson(event)   if (event.keyName == ‘d’ and event.phase == ‘down’) then   while ((event.keyName == ‘d’ and event.phase == ‘down’) == true) do   Sprite:setLinearVelocity(200,0)   return true   end   end   if (event.keyName == ‘a’ and event.phase == ‘down’) then   Sprite:setLinearVelocity(-200, 0)   return true   end   if(event.keyName == ‘space’) then   Sprite:applyLinearImpulse(0, -.25)   end  

end

That’s my code so how would I used a while loop to see if it would continue the sprite moving in one direction rather than constant pressing, when I press A it moves once.

You wouldn’t use a while loop. You need to keep track of whether the key is held down. If it’s been pressed, but not released, then it must still be down. As Rob says, look into enterFrame listeners which are functions that run once a frame. Check within here whether a variable like ‘isUpKeyHeld’ etc is true and perform your movement accordingly.

Please use code formatting when posting code. Click on the blue <> button in the row with Bold, Italic, etc. and copy/pasted your code into the popup window!

Thanks

Rob

Okay thanks a million for your help!