1
votes

I quite new at LUA. I am using timer to popup alerts, made up with displaygroup, when necessary. To remove them I'm using timeperformwithdelay function that calls a remove function which removes the displaygroup object referencing with Self. However I have a stopwatch working with the timer function as well. When the stopwatch is zero I popup the Alert and after 2 seconds I want to remove it. But in this case the remove function does not work. Following is the code:

if timerleft==0 then
   screenalert("Game 1","Time is over")
   performwithdelay(2000,remove,1)


end

Thanks Hi there again, thanks for your support. Here is my code:

local function timerDown()
   timeLimit = timeLimit-1
   if timeLimit < 10 then
    timeLeft.text = "0:0"..timeLimit
   else
    timeLeft.text = "0:"..timeLimit
   end

 if(timeLimit==0)then
    timeLeft.text="0:00" 
    alertScreen("Game 1","Time is over!")

    timer.performWithDelay(2000,removeAlert,1)
    timeLimit=60    
 end
end


local function removeAlert()
   alertDisplayGroup:removeSelf()   
end

function alertScreen(title, message)

alertBox=display.newImage("cornice.png")
alertBox.x=W
alertBox.y=H/1.3
titolomessaggio=display.newText(title,0,0,"Arial",200)
titolomessaggio:setTextColor(255,255,0,255)

titolomessaggio.xScale=0.5
titolomessaggio.yScale=0.5

titolomessaggio.x=display.contentCenterX
titolomessaggio.y=display.contentCenterY-200

testomessaggio=display.newText(message,0,0,"Arial",100)
testomessaggio:setTextColor(255,255,0,255)
testomessaggio.xScale=0.5
testomessaggio.yScale=0.5

testomessaggio.x=display.contentCenterX
testomessaggio.y=display.contentCenterY+10


alertDisplayGroup=display.newGroup()
alertDisplayGroup:insert(alertBox)
alertDisplayGroup:insert(titolomessaggio)
alertDisplayGroup:insert(testomessaggio)
end
1
What does your remove function do? What does it expect to operate on because I can't imagine it gets any context or arguments when called that way. - Etan Reisner
Hi Etan,thanks for answering. Actually this function does not have any effect! I guess it shoul remove the displaygroup I used to create the screenAlert (an image and two text rows). But the same function works for example when I call it at the beginning of the game when I show the Alert with the game level number. Am I missing something? - ubaldo
What does the remove function look like? Does it take an argument? Is it a "member" function of some object/table? - Etan Reisner
Etan, here is my code: local function timerDown() timeLimit = timeLimit-1 if timeLimit < 10 then timeLeft.text = "0:0"..timeLimit else timeLeft.text = "0:"..timeLimit end if(timeLimit==0)then timeLeft.text="0:00" alertScreen("Game 1","Time is over!") timer.performWithDelay(2000,removeAlert,1) timeLimit=60 end end - ubaldo
And this is the function to remove: local function removeAlert() alertDisplayGroup:removeSelf() end - ubaldo

1 Answers

0
votes

I modified your code to test it out. so far this code I made works with the requirements you wanted. For functions to work in lua the function must be on the top part of that function meaning if you want to call function A from Function B. Function A must be on top of Function B.

example:

local functionA ()

end

local functionB ()

  functionA()

end

Here is the code snippet I made to make your code work to your liking:

local alertDisplayGroup = display.newGroup()
local timeLimit = 5

local function removeAlert()
   alertDisplayGroup:removeSelf()   
end

local function timerDown()
   timeLimit = timeLimit-1
    print(timeLimit)
   -- if timeLimit < 10 then
   --  print(timeLimit)

   -- else
   --  print(timeLimit)
   -- end

 if(timeLimit==0)then
    --timeLeft.text="0:00" 
    alertScreen("Game 1","Time is over!")

    timer.performWithDelay(2000,removeAlert,1)
    timeLimit=5    
 end
end


function alertScreen(title, message)

alertBox=display.newImageRect("images/error_message.png", 252, 85)
alertBox.x=screenW/2
alertBox.y=screenH/2
titolomessaggio=display.newText(title,0,0,"Arial",200)
titolomessaggio:setTextColor(255,255,0,255)

titolomessaggio.xScale=0.5
titolomessaggio.yScale=0.5

titolomessaggio.x=display.contentCenterX
titolomessaggio.y=display.contentCenterY-200

testomessaggio=display.newText(message,0,0,"Arial",100)
testomessaggio:setTextColor(255,255,0,255)
testomessaggio.xScale=0.5
testomessaggio.yScale=0.5

testomessaggio.x=display.contentCenterX
testomessaggio.y=display.contentCenterY+10


alertDisplayGroup=display.newGroup()
alertDisplayGroup:insert(alertBox)
alertDisplayGroup:insert(titolomessaggio)
alertDisplayGroup:insert(testomessaggio)
end

timer.performWithDelay( 1000, function() timerDown() end, 0)

as you can see. I edited it a little to test it out. Cheers.