如何使用Corona增加XY方向的重力?

在我的游戏中,我有左右两个按钮。当人物到达左侧按钮时,重力值变为负数;右侧按钮则为正数。如何设置碰撞检测,以适用于左右两个方向。

local function onLocalPreCollision( self, event )

        if (left.name == "left")  then
                    physics.setGravity(-10,0)

        end
                    if (right.name == "right")  then
                    physics.setGravity(10,0)

        end
    end

boy.preCollision = onLocalPreCollision
boy:addEventListener( "preCollision", boy )

原文链接 https://stackoverflow.com/questions/9888076

点赞
stackoverflow用户785576
stackoverflow用户785576

尝试这段代码

你必须使用self或者event

  • self是指撞击到的对象,也就是男孩
  • event.target是指撞击到的另一个对象,即左或右
local left = display.newRect()
left.name = "left"
local right = display.newRect()
right.name = "right"
physics.addBody("static", left)
physics.addBody("static", right)

local function onLocalPreCollision(self, event)
    if (event.other.name == "left") then
        physics.setGravity(-10, 0)
    end
    if (event.other.name == "right") then
        physics.setGravity(10, 0)
    end
end

boy.preCollision = onLocalPreCollision
boy:addEventListener("preCollision", boy)
2012-05-03 07:54:35