【问题标题】:assigning physics.setGravity a different value为physics.setGravity 分配一个不同的值
【发布时间】:2014-03-21 22:06:03
【问题描述】:
--***************************************************

-- 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”事件列表器上,keepPlaneInBound 已设置为“enterframe”事件列表器。我想要实现的是当飞机超过上限时。它之前的 physcis.setGravity 值将被完全删除,并且当用户抬起手指时(当 event = "ended" 时)分配给它的新值。 奇怪的是它不接受新的physics.setGravity() 值。如果我使用physics.pause() 然后为其分配新的重力值。在physics.start 上,它首先完成前一个setGravity 值的部分,因此再次向上移动它......:/

【问题讨论】:

  • 您的重力代码先验没有问题,不确定边界检查,因为看起来keepPlaneInBound 中有一个额外的“结束”会导致语法错误。所以请更正它并编辑您的问题以显示您注册事件回调的代码,这可能是错误所在。
  • @Schollii。嗨,谢谢你帮助我。您所说的“结束”是在这里错误添加的。我已经编辑了代码。现在代码就是它在我的记事本++中的样子了。
  • 好的,但正如我所提到的,我看不出你的代码有什么问题,我什至尝试在测试应用程序中改变重力并且它工作正常,所以问题出在你没有的代码上显示。请显示您的 addEventListener 调用,以及用于调用这两个函数的任何其他代码。 "function flyPlane is set on "touch" "enterframe" eventlistner" 没有意义,需要看代码。
  • 痛,@Schollii。我同意你的看法,我在问题的描述中犯了一些严重的错误。现在解决了。我已经发布了我为摆脱它所做的事情。不管怎么说,多谢拉。 :)

标签: android lua coronasdk game-engine physics


【解决方案1】:

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

local function flyPlane(event) 
   your_code()
end

local function keepPlaneInBound(event)
   your_code()
end

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

这就是你所需要的。 (假设你的游戏逻辑是正确的)

【讨论】:

  • 嗨,亚当,感谢您的帮助。是的,这让我接近解决方案。但主要问题仍然存在。当玩家从屏幕上抬起手指时,我想分配给 paperPlane 对象的重力值。该对象只是保持自己与先前分配的重力值(在“开始”事件中分配)保持一致。
  • 唯一可能的原因是如果 gameIsActive 布尔值被评估为假。 -- 顺便说一句,你不必做“if boolean == true then”,你可以说“if boolean then”
【解决方案2】:

伙计们。我得到了我想要的东西。完全移除施加在物理体上的力以便为其分配新的重力值的技术是简单地将其 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

【讨论】:

  • 不可能,因为 setGravity 立即(即一旦事件处理程序返回)在所有物体上起作用,而不会暂停物理(我已经验证)。而且,为了使新的重力生效,您必须(完全)更改每种物理体类型是没有意义的。这是一个 hack,还有其他问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 2012-07-23
  • 1970-01-01
  • 1970-01-01
  • 2014-06-03
相关资源
最近更新 更多