# Using while loop crashes Corona

**URL:** <https://forums.solar2d.com/t/using-while-loop-crashes-corona/349575>\
**Category:** Newbie Questions\
**Created:** [March 10, 2019, 2:02pm UTC](https://forums.solar2d.com/t/using-while-loop-crashes-corona/349575 "2019-03-10T14:02:51Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![saslynch12](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/s/f475e1/32.png) [@saslynch12](https://forums.solar2d.com/u/saslynch12)\
**Post date:** [March 10, 2019, 2:02pm UTC](https://forums.solar2d.com/t/using-while-loop-crashes-corona/349575/1 "2019-03-10T14:02:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sporkfin](https://forums.solar2d.com/user_avatar/forums.solar2d.com/sporkfin/32/30_2.png) [@sporkfin](https://forums.solar2d.com/u/sporkfin)\
**Post date:** [March 11, 2019, 3:39pm UTC](https://forums.solar2d.com/t/using-while-loop-crashes-corona/349575/2 "2019-03-11T15:39:10Z")

</div>

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

[lua]

if event.phase == “down” then

&nbsp; &nbsp; &nbsp;– do something

end

[/lua]

---

<div class="post-metadata">

**Author:** ![sporkfin](https://forums.solar2d.com/user_avatar/forums.solar2d.com/sporkfin/32/30_2.png) [@sporkfin](https://forums.solar2d.com/u/sporkfin)\
**Post date:** [March 11, 2019, 3:42pm UTC](https://forums.solar2d.com/t/using-while-loop-crashes-corona/349575/3 "2019-03-11T15:42:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rob](https://forums.solar2d.com/user_avatar/forums.solar2d.com/rob/32/4_2.png) [@rob](https://forums.solar2d.com/u/rob)\
**Post date:** [March 11, 2019, 9:02pm UTC](https://forums.solar2d.com/t/using-while-loop-crashes-corona/349575/4 "2019-03-11T21:02:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![saslynch12](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/s/f475e1/32.png) [@saslynch12](https://forums.solar2d.com/u/saslynch12)\
**Post date:** [March 12, 2019, 12:24pm UTC](https://forums.solar2d.com/t/using-while-loop-crashes-corona/349575/5 "2019-03-12T12:24:38Z")

</div>

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

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.

---

<div class="post-metadata">

**Author:** ![nick\_sherman](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/n/958977/32.png) [@nick\_sherman](https://forums.solar2d.com/u/nick_sherman)\
**Post date:** [March 12, 2019, 3:40pm UTC](https://forums.solar2d.com/t/using-while-loop-crashes-corona/349575/6 "2019-03-12T15:40:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rob](https://forums.solar2d.com/user_avatar/forums.solar2d.com/rob/32/4_2.png) [@rob](https://forums.solar2d.com/u/rob)\
**Post date:** [March 12, 2019, 5:51pm UTC](https://forums.solar2d.com/t/using-while-loop-crashes-corona/349575/7 "2019-03-12T17:51:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![saslynch12](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/s/f475e1/32.png) [@saslynch12](https://forums.solar2d.com/u/saslynch12)\
**Post date:** [March 12, 2019, 11:21pm UTC](https://forums.solar2d.com/t/using-while-loop-crashes-corona/349575/8 "2019-03-12T23:21:40Z")

</div>

Okay thanks a million for your help!

---

<div class="post-metadata">

**Author:** ![sporkfin](https://forums.solar2d.com/user_avatar/forums.solar2d.com/sporkfin/32/30_2.png) [@sporkfin](https://forums.solar2d.com/u/sporkfin)\
**Post date:** [March 11, 2019, 3:39pm UTC](https://forums.solar2d.com/t/using-while-loop-crashes-corona/349575/9 "2019-03-11T15:39:10Z")

</div>

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

[lua]

if event.phase == “down” then

&nbsp; &nbsp; &nbsp;– do something

end

[/lua]

---

<div class="post-metadata">

**Author:** ![sporkfin](https://forums.solar2d.com/user_avatar/forums.solar2d.com/sporkfin/32/30_2.png) [@sporkfin](https://forums.solar2d.com/u/sporkfin)\
**Post date:** [March 11, 2019, 3:42pm UTC](https://forums.solar2d.com/t/using-while-loop-crashes-corona/349575/10 "2019-03-11T15:42:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rob](https://forums.solar2d.com/user_avatar/forums.solar2d.com/rob/32/4_2.png) [@rob](https://forums.solar2d.com/u/rob)\
**Post date:** [March 11, 2019, 9:02pm UTC](https://forums.solar2d.com/t/using-while-loop-crashes-corona/349575/11 "2019-03-11T21:02:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![saslynch12](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/s/f475e1/32.png) [@saslynch12](https://forums.solar2d.com/u/saslynch12)\
**Post date:** [March 12, 2019, 12:24pm UTC](https://forums.solar2d.com/t/using-while-loop-crashes-corona/349575/12 "2019-03-12T12:24:38Z")

</div>

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

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.

---

<div class="post-metadata">

**Author:** ![nick\_sherman](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/n/958977/32.png) [@nick\_sherman](https://forums.solar2d.com/u/nick_sherman)\
**Post date:** [March 12, 2019, 3:40pm UTC](https://forums.solar2d.com/t/using-while-loop-crashes-corona/349575/13 "2019-03-12T15:40:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rob](https://forums.solar2d.com/user_avatar/forums.solar2d.com/rob/32/4_2.png) [@rob](https://forums.solar2d.com/u/rob)\
**Post date:** [March 12, 2019, 5:51pm UTC](https://forums.solar2d.com/t/using-while-loop-crashes-corona/349575/14 "2019-03-12T17:51:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![saslynch12](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/s/f475e1/32.png) [@saslynch12](https://forums.solar2d.com/u/saslynch12)\
**Post date:** [March 12, 2019, 11:21pm UTC](https://forums.solar2d.com/t/using-while-loop-crashes-corona/349575/15 "2019-03-12T23:21:40Z")

</div>

Okay thanks a million for your help!
