我有这个onCollision功能,但每次游戏开始时碰撞加倍
local function onCollision(event)
if event.phase == "began" and gameIsActive == true then
local obj1 = event.object1;
local obj2 = event.object2;
if obj1.name == "jetplayer" and obj2.name == "BCloud1" then
MinLife()
elseif obj1.name == "jetplayer" and obj2.name == "BCloud2" then
pontsMin10()
elseif obj1.name == "jetplayer" and obj2.name == "BCloud3" then
pontsMin20()
elseif obj1.name == "jetplayer" and obj2.name == "GCloud1" then
pontsplus50()
elseif obj1.name == "jetplayer" and obj2.name == "bla" then
score = score - 20
end
end
end
Runtime:addEventListener( "collision", onCollision )
function scene:exitScene( event )
Runtime:removeEventListener( "collision", onCollision )
有没有理由发生这种情况?
答案 0 :(得分:1)
确保local function onCollision(event)
超出其他功能。
也许问题出在您的Runtime:removeEventListener( "collision", onCollision )
中,它无法找到onCollision
,因为它位于其他功能内部。
答案 1 :(得分:0)
if obj1.name == "jetplayer" and obj2.name == "BCloud1" then MinLife()
elseif obj1.name == "jetplayer" and obj2.name == "BCloud2" then pontsMin10()
elseif obj1.name == "jetplayer" and obj2.name == "BCloud3" then pontsMin20()
elseif obj1.name == "jetplayer" and obj2.name == "GCloud1" then pontsplus50()
elseif obj1.name == "jetplayer" and obj2.name == "bla" then score = score - 20
end
仅供参考,这是obj1.name == "jetplayer"
的大量冗余测试。我会重写那段代码进行一次测试:
if obj1.name == "jetplayer" then
if obj2.name == "BCloud1" then MinLife()
elseif obj2.name == "BCloud2" then pontsMin10()
elseif obj2.name == "BCloud3" then pontsMin20()
elseif obj2.name == "GCloud1" then pontsplus50()
elseif obj2.name == "bla" then score = score - 20
end
end
我第一次开始游戏并且我点击了“MinLife()”它会让一生失去,但当你重新开始游戏'gameover.lua-> start.lua-> game.lua'当你点击“ MinLife()“它需要2次生命,第3次重启3次生命等等
您没有显示MinLife
,因此我们无法知道该代码中是否存在错误。我会检查你是不是多次添加你的碰撞处理程序(例如确保你的exitScene处理程序被攻击等)。