How can I delay a function??

how can I delay the changeScene function?? i want it to change scene a few seconds after the lives hit 0, is there any way i can do this? thanks!

1
2
3
4
5
6
7
8
9
10
local function killCrate (event)
crate:removeSelf()
lives = lives - 1
livesDisplay.text = lives
if lives == 0 then
director:changeScene("level1success")
end
end
 
crate:addEventListener("touch", killCrate)

you would want to put your changeScene code into a new function. Then you can use what is called a timer. A timer allows you to call a function at a set time. for example

timer.performWithDelay(2000, changeScene, 1 )

this code is saying, after 2 seconds ( or 2000 milliseconds) call the function changeScene, 1 time.

so perhaps you could do this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
local function changeScene()
director:changeScene("level1success")
end
 
local function killCrate (event)
crate:removeSelf()
lives = lives - 1
livesDisplay.text = lives
if lives == 0 then
timer.performWithDelay(2000, changeScene, 1)
end
end
 
crate:addEventListener("touch", killCrate)

works perfectly! thank you so much!

no problem, now make an awesome app! =p

views:1426 update:2011/10/2 9:44:12
corona forums © 2003-2011