Difference Between Runtime Events and While Loop

Hi,

I did a bit of testing as shown below:

[lua]local function main(event)
textobject.text = math.random(1,100)
end

Runtime:addEventListener(“enterFrame”, main)[/lua]
in contrast with

[lua]local i = 0
while true do
i=i+1
textobject.text = math.random(1,100)

if i==3 then break end
end[/lua]

In the first example I can see the text changing on screen but in the second example, the text doesn’t change at all until the while loop is broken.

Can someone explain this phenomenon to me?

Thank you. [import]uid: 108204 topic_id: 18862 reply_id: 318862[/import]

I’m assuming that you have some other code that is not shown. For example the code that actually updates the screen.

In the first example, when you use enterframe, this allows other code to run in between calls to your main() function. Thus the code that’s updating the screen is allowed to run.

In your second example, your app will only execute the code in your while loop and no other code is allowed to run until it finishes. This is why you don’t see any updates on the screen until the loop finishes.

Have you seen this post on the Event Model?

http://blog.anscamobile.com/2011/06/the-corona-event-model-explained/#more-7733

[import]uid: 67839 topic_id: 18862 reply_id: 72647[/import]

Well! Suppose your fps is 30,your screen is updated 30 times per second.

In the first case, main is called 30 times in a second, once after every screen update.

In the second case, main is called an infinite number of times IN A SINGLE FRAME. Hence the code breaks. [import]uid: 64174 topic_id: 18862 reply_id: 72725[/import]