repeating a function

Hi everyone,
I’m trying to program a quiz to familiarise with lua and I’m stuck on how to repeat the procedure once the game move from one question to the next.
the following code may help to clarify my problem: why won’t the code do the same operation three times?

contW = display.contentWidth
contH = display.contentHeight
centerX = display.contentCenterX
centerY = display.contentCenterY

– Create the touch event handler function
local function myButtonHandler( event )
 
    if (event.phase == “began”) then 
 
        myText.text = "Button Phase is: " … event.phase
        myButton.xScale = 0.85 – Scale the button on touch down so user knows its pressed
        myButton.yScale = 0.85
 
    elseif (event.phase == “moved”) then
 
        myText.text = "Button Phase is: " … event.phase
 
    elseif (event.phase == “ended” or event.phase == “cancelled”) then
        Round=Round+1
        myText.text = "Phase is: " … event.phase…“Round:”…Round
        myButton.xScale = 1 – Re-scale the button on touch release
        myButton.yScale = 1
     myButton:removeEventListener(“touch”, myButtonHandler)
    end
 
    return true
 
end

local function Main()

for Round=0,3 do
    myButton = display.newImageRect(“buttonBlue.png”,120,120)
 
    – Reposition the Button to the center of the screen
    myButton.x = centerX
    myButton.y = centerY

    – Add a text box to display the state/phase of the touch event from the button
    myText = display.newText( “Round”…Round, centerX, centerY, native.systemFont, 30 )
    myText:setFillColor( 1, 0, 0 )

    – Add a touch event handler to myButton
    myButton:addEventListener(“touch”, myButtonHandler)
end
end

Hello @lcazzulani and welcome to the forums. Before I discuss your question, it’s highly recommended that when posting code to the forums you use code formatting. This is the blue <> button in the bar with Bold, Italics, etc. Click that, paste your code into the window that pops up then save the form and your code will be much easier to read for community members and will increase the likelyhood of getting a response.

Now for your question. You’re hitting a very common problem. Corona SDK is an event driven system. That is there is no " GameLoop" as such.  When your code does:

for Round=0,3 do myButton = display.newImageRect("buttonBlue.png",120,120) -- Reposition the Button to the center of the screen myButton.x = centerX myButton.y = centerY -- Add a text box to display the state/phase of the touch event from the button myText = display.newText( "Round"..Round, centerX, centerY, native.systemFont, 30 ) myText:setFillColor( 1, 0, 0 ) -- Add a touch event handler to myButton myButton:addEventListener("touch", myButtonHandler) end

that for loop is going to run as fast as it can (in micro seconds) and basically you’re stacking three display.newImageRects()’ and three display.newTexts() on top of each other and it’s happening in one frame.

Internally Corona SDK runs on a clock that triggers either 30 times per second or 60 times per second (or Frames Per Second - FPS) depending on what you set your app to run at. I’ll use 30 fps for illustrative purposes. (Most people run at 60 fps).

Every 0.0333 seconds Corona will do as much processing as it can do, and then update the screen. That for loop very likely finished in one frame.

One you have an object on the screen, the user can then interact with it, such as touching the button. That will trigger an event where your app can do work. It can respond to the touch. Since you’re doing a quiz you will probably have multiple buttons for each answer. One the user selects an answer that function can then make a decision if the answer was right or wrong and then display something showing the result. Once the user interacts with that, or perhaps a “Next Round” button, the app would then change the text for the next question. At some point, when processing the touch for each answer you will know you’ve run out of questions and then it can be programmed to go to the next phase of the app (game over screen, etc.). None of this requires a loop.

Sometimes you need to do things based on time. For instance you might only want to give the person 15 seconds to answer the question. At that point you can generate your own events using our timer.* library in particular, timer.performWithDelay() where  you can say ever X milliseconds do something. The timer can be programmed to fire once, fire a limited number of times or trigger until cancelled. Some gameLoop like features can be implemented with timers.

Finally if you need to do things continuously, you would avoid constructs like for and while loops because they block the interface from responding by creating a tight loop. Instead it’s possible to get an event generated every frame tick so you can do things every 30th or 60th of a second. You might want to have a game where the background scrolls smoothly and continuously. You would receive an event every frame, move your background a few pixels and have this happen every frame for a smooth motion.

Rob

Hello @lcazzulani and welcome to the forums. Before I discuss your question, it’s highly recommended that when posting code to the forums you use code formatting. This is the blue <> button in the bar with Bold, Italics, etc. Click that, paste your code into the window that pops up then save the form and your code will be much easier to read for community members and will increase the likelyhood of getting a response.

Now for your question. You’re hitting a very common problem. Corona SDK is an event driven system. That is there is no " GameLoop" as such.  When your code does:

for Round=0,3 do myButton = display.newImageRect("buttonBlue.png",120,120) -- Reposition the Button to the center of the screen myButton.x = centerX myButton.y = centerY -- Add a text box to display the state/phase of the touch event from the button myText = display.newText( "Round"..Round, centerX, centerY, native.systemFont, 30 ) myText:setFillColor( 1, 0, 0 ) -- Add a touch event handler to myButton myButton:addEventListener("touch", myButtonHandler) end

that for loop is going to run as fast as it can (in micro seconds) and basically you’re stacking three display.newImageRects()’ and three display.newTexts() on top of each other and it’s happening in one frame.

Internally Corona SDK runs on a clock that triggers either 30 times per second or 60 times per second (or Frames Per Second - FPS) depending on what you set your app to run at. I’ll use 30 fps for illustrative purposes. (Most people run at 60 fps).

Every 0.0333 seconds Corona will do as much processing as it can do, and then update the screen. That for loop very likely finished in one frame.

One you have an object on the screen, the user can then interact with it, such as touching the button. That will trigger an event where your app can do work. It can respond to the touch. Since you’re doing a quiz you will probably have multiple buttons for each answer. One the user selects an answer that function can then make a decision if the answer was right or wrong and then display something showing the result. Once the user interacts with that, or perhaps a “Next Round” button, the app would then change the text for the next question. At some point, when processing the touch for each answer you will know you’ve run out of questions and then it can be programmed to go to the next phase of the app (game over screen, etc.). None of this requires a loop.

Sometimes you need to do things based on time. For instance you might only want to give the person 15 seconds to answer the question. At that point you can generate your own events using our timer.* library in particular, timer.performWithDelay() where  you can say ever X milliseconds do something. The timer can be programmed to fire once, fire a limited number of times or trigger until cancelled. Some gameLoop like features can be implemented with timers.

Finally if you need to do things continuously, you would avoid constructs like for and while loops because they block the interface from responding by creating a tight loop. Instead it’s possible to get an event generated every frame tick so you can do things every 30th or 60th of a second. You might want to have a game where the background scrolls smoothly and continuously. You would receive an event every frame, move your background a few pixels and have this happen every frame for a smooth motion.

Rob