将 physics.setGravity 分配一个不同的值。

函数"flyPlane"被设置为"touch"事件监听器,而"keepPlaneInBound"已被设置为"enterframe"事件监听器。我想要实现的是,当飞机超过上限时,它的先前"physics.setGravity"值被完全删除,并在用户抬起手指时分配新值(当事件="ended"时)。 奇怪的是它不接受新的"physics.setGravity()"值。如果我使用"physics.pause()",然后再赋给它新的重力值,在"physics.start"中,它首先完成了先前"setGravity"值的部分,因此再次向上移动。。 :/

点赞
用户822240
用户822240

你不应该把"flyPlane"作为"enterFrame"的回调函数。你应该像这样编写代码:

local function flyPlane(event)
   你的代码()
end

local function keepPlaneInBound(event)
   你的代码()
end

paperPlane:addEventListener("touch", flyPlane)
Runtime:addEventListener("enterFrame", keepPlaneInBound)

只要你的游戏逻辑正确,这就是你所需要的。

2014-02-21 00:35:00
用户1906738
用户1906738

大家好。我找到了我想要的东西。完全删除应用于物理体的力以便将新的重力值分配给它的技巧是将其 bodyType 改为另一种类型,然后再将所需的 bodyType 再次分配给对象。这样,先前重力值对物理对象的影响将完全被删除。这就是我所做的。我确信它适用于我。

-- drawButtons() -->

--***************************************************

local flyPlane = function(event)

    if event.phase == "began" and gameIsActive == true then
        print("began")
        -- paperPlane.bodyType = "dynamic"
        physics.setGravity( 0, -10 )
        -- physics.start()
    elseif event.phase == "ended" and gameIsActive == true then
        print("ended")
        -- paperPlane.bodyType = "dynamic"
        physics.setGravity( 0, 7 )
        -- physics.start()
    elseif event.phase == "moved" and gameIsActive == true then
    end

end

--***************************************************

-- keepPlaneInBound() -->

--***************************************************

local keepPlaneInBound = function()
    -- print("checking")
    if paperPlane.y <= paperPlane.height + 10 and gameIsActive == true then
        print("Out of Bound -y:   ", paperPlane.bodyType)
        physics.pause()
        paperPlane.bodyType = "static"
        paperPlane.y = paperPlane.height + 11
    elseif paperPlane.y > display.contentHeight - paperPlane.height - scale.height and gameIsActive == true then
        print("Out of Bound +y:   ", paperPlane.bodyType)
        physics.pause()
        paperPlane.bodyType = "static"
        paperPlane.y = display.contentHeight - paperPlane.height - scale.height - 1
    else
        print("In Bound:    ", paperPlane.bodyType)
        paperPlane.bodyType = "dynamic"
        physics.start()
    end

end
2014-02-21 15:36:22