0

我有这个 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 )

有什么理由发生这种情况吗?

4

2 回答 2

1

确保您local function onCollision(event)在其他功能之外。

也许问题出在您的Runtime:removeEventListener( "collision", onCollision ),它找不到,onCollision因为它在其他功能的内部。

于 2013-07-09T16:59:54.913 回答
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 条生命,依此类推

您不显示MinLife,因此我们无法知道该代码中是否存在错误。我会检查您是否多次添加碰撞处理程序(例如,确保您的 exitScene 处理程序被击中等)

于 2013-07-09T16:23:31.197 回答