Lua 如何延迟gotoscene
每当特定食物击中猴子时,游戏就会重新启动,但我想延迟几秒钟,并在重新启动前显示一些文字,但我似乎不能。不拖延,Lua 如何延迟gotoscene,lua,coronasdk,Lua,Coronasdk,每当特定食物击中猴子时,游戏就会重新启动,但我想延迟几秒钟,并在重新启动前显示一些文字,但我似乎不能。不拖延, local function monkeyCollision( self, event ) if event.phase == "began" then if event.target.type == "monkey" and event.other.type == "food" then print( "chomp!" )
local function monkeyCollision( self, event )
if event.phase == "began" then
if event.target.type == "monkey" and event.other.type == "food" then
print( "chomp!" )
event.other.alpha = 0
event.other:removeSelf()
addToScore(5)
-- get points!
else
print("ow!")
monkey.alpha = 0
monkey:removeSelf()
displayScore = display.newText( "The total score is " .. score , 0, 0, "Helvetica", 30 )
displayScore.x = screenLeft +150
displayScore.y = screenRight-100
displayre = display.newText( " The game is going restart", 0, 0, "Helvetica", 25 )
displayre.x = screenLeft +150
displayre.y = screenRight-200
storyboard.gotoScene("play", "fade", 1000)
end
为什么不把它放在计时器里呢
timer.performWithDelay(5000, function() storyboard.gotoScene("play", "fade", 1000); end)
在调用storybaord.gotoScene()之前会延迟5秒,为什么不将其放入计时器中:
timer.performWithDelay(5000, function() storyboard.gotoScene("play", "fade", 1000); end)
这将在调用storybaord.gotoScene()之前延迟5秒。根据Rob的喜好添加计时器
timer.performWithDelay(5000, function() storyboard.gotoScene("play", "fade", 1000); end)
但你现在也有问题了。如果你已经吃了另一种食物,又吃了另一种呢?这将使多个计时器关闭,并可能出现故障,因为它将删除已删除的猴子
local lostGame = false
local function monkeyCollision( self, event )
if event.phase == "began" then
if event.target.type == "monkey" and event.other.type == "food" then
print( "chomp!" )
event.other.alpha = 0
event.other:removeSelf()
addToScore(5)
-- get points!
else
if lostGame == false then
print("ow!")
monkey.alpha = 0
monkey:removeSelf()
displayScore = display.newText( "The total score is " .. score , 0, 0, "Helvetica", 30 )
displayScore.x = screenLeft +150
displayScore.y = screenRight-100
displayre = display.newText( " The game is going restart", 0, 0, "Helvetica", 25 )
displayre.x = screenLeft +150
displayre.y = screenRight-200
timer.performWithDelay(5000, function() storyboard.gotoScene("play", "fade", 1000); end)
lostGame = true
end
end
end
end
通过添加一个变量来检查您是否已经丢失,您可以防止它在您处于延迟状态时运行要离开的代码。如Rob所愿添加一个计时器
timer.performWithDelay(5000, function() storyboard.gotoScene("play", "fade", 1000); end)
但你现在也有问题了。如果你已经吃了另一种食物,又吃了另一种呢?这将使多个计时器关闭,并可能出现故障,因为它将删除已删除的猴子
local lostGame = false
local function monkeyCollision( self, event )
if event.phase == "began" then
if event.target.type == "monkey" and event.other.type == "food" then
print( "chomp!" )
event.other.alpha = 0
event.other:removeSelf()
addToScore(5)
-- get points!
else
if lostGame == false then
print("ow!")
monkey.alpha = 0
monkey:removeSelf()
displayScore = display.newText( "The total score is " .. score , 0, 0, "Helvetica", 30 )
displayScore.x = screenLeft +150
displayScore.y = screenRight-100
displayre = display.newText( " The game is going restart", 0, 0, "Helvetica", 25 )
displayre.x = screenLeft +150
displayre.y = screenRight-200
timer.performWithDelay(5000, function() storyboard.gotoScene("play", "fade", 1000); end)
lostGame = true
end
end
end
end
通过添加一个变量来检查您是否已经丢失,您可以防止它在您处于延迟状态时运行代码离开。这是一个很好的观点!我只是让这件事发生在我身上,延迟了500毫秒。在我的例子中,不是用户选择了其他东西,而是它也会注册一个“双击”或触摸事件,导致下一个场景调用scene:show twick。这是一个很好的观点!我只是让这件事发生在我身上,延迟了500毫秒。在我的例子中,这并不是因为用户选择了其他东西,而是因为它也会注册一个“双击”或触摸事件,导致下一个场景调用scene:show两次。请注意这一点,原因由joehinkle11 befow提供!由于joehinkle11 befow提供的原因,请注意这一点!