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