Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Using while loop crashes Corona
Started by saslynch12 Mar 10 2019 06:02 AM

7 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

saslynch12

[GLOBAL: userInfoPane.html]
saslynch12
  • Observer

  • 3 posts
  • Corona SDK

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.



[TOPIC: post.html]
#2

sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
  • Contributor

  • 452 posts
  • Corona SDK

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.

if event.phase == "down" then
     -- do something
end



[TOPIC: post.html]
#3

sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
  • Contributor

  • 452 posts
  • Corona SDK

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.



[TOPIC: post.html]
#4

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,373 posts
  • Enterprise

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



[TOPIC: post.html]
#5

saslynch12

[GLOBAL: userInfoPane.html]
saslynch12
  • Observer

  • 3 posts
  • Corona SDK

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.



[TOPIC: post.html]
#6

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,708 posts
  • Corona SDK

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.

[TOPIC: post.html]
#7

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,373 posts
  • Enterprise

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



[TOPIC: post.html]
#8

saslynch12

[GLOBAL: userInfoPane.html]
saslynch12
  • Observer

  • 3 posts
  • Corona SDK

Okay thanks a million for your help!




[topic_controls]
[/topic_controls]