为physics.setGravity分配一个不同的值

时间:2014-02-20 18:40:55

标签: android lua corona game-engine physics

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

-- drawButtons() --> 

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

local flyPlane = function(event)

    if event.phase == "began" and gameIsActive == true then
        print("began")
        physics.setGravity( 0, -10 )
    elseif event.phase == "ended" and gameIsActive == true then
        print("ended")
        physics.setGravity( 0, 7 )
        inBound = true
    elseif event.phase == "moved" and gameIsActive == true then
        -- do nothing
    end

end

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

-- keepPlaneInBound() --> 

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

local keepPlaneInBound = function()
    -- print("checking")
    if paperPlane.y <= paperPlane.height + 10 and gameIsActive == true and inBound == true then
        inBound = false
        paperPlane.y = paperPlane.height + 12
    end

    if paperPlane.y > display.contentHeight - paperPlane.height - scale.height and gameIsActive == true then
        paperPlane.y = display.contentHeight - paperPlane.height - scale.height
    end

end

函数flyPlane设置在“touch”eventlistner上,keepPlaneInBound已设置为“enterframe”eventlistner。我想要实现的是当飞机超过上限时。它的先前physcis.setGravity值将被完全删除,并且当用户抬起手指时(当事件=“结束”时)将分配给它的新值。 奇怪的是,它不接受新的physics.setGravity()值。如果我使用physics.pause()然后为它分配新的重力值。在physics.start上它首先完成前一个setGravity值的一部分,然后再将它向上移动..:/

2 个答案:

答案 0 :(得分:0)

你不应该将“flyPlane”作为“enterFrame”中的回调。你应该有这样的东西:

local function flyPlane(event) 
   your_code()
end

local function keepPlaneInBound(event)
   your_code()
end

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

这就是你所需要的一切。 (鉴于你的游戏逻辑是正确的)

答案 1 :(得分:0)

人。我得到了我想要的东西。为了给它分配新的重力值,完全去除施加到物理主体上的力的技术是简单地将其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