0

我按照本教程制作了一个突破游戏,但在某些时候,当球的角度太大(太水平)时,球会一直靠在顶墙上。是否有任何逻辑我可以调整,以便球可以避免这种行为?

这是屏幕截图:

截屏

球的相关源码为:

local ballRadius = 10
ball = display.newCircle( display.contentWidth / 2, display.contentHeight / 2, ballRadius )
physics.addBody(ball, "dynamic", {friction=0, bounce = 1, radius=ballRadius})
4

1 回答 1

2

这有点奇怪。但是我曾经做过一次这样的......

创建 3 个变量/标志:

local horizontalMotionFlag,yPos_1,yPos_2 = 0,0,0

然后:

wall.type = "LeftWall"  -- to the LeftWall
            -- and --
wall.type = "RightWall"  -- to the RightWall

在里面添加以下行event.phase == "ended"

------------------------------------------------------------------------------------------

if(event.other.type == "LeftWall") then
    yPos_1 = ball.y
    if(horizontalMotionFlag==0)then
        horizontalMotionFlag = 1
    else
        if(math.abs(yPos_1-yPos_2) < 50)then
            print("CoHorizontal motion detected. Change angle...1")
            horizontalMotionFlag = 0
            ball:applyForce( 0, 1, ball.x, ball.y )  -- apply a small downward force
            ball:applyForce( 0, 0, ball.x, ball.y )  -- resetting the force
            -- You can also check the direction of ball and apply force to -1(upwards) also --
        end
    end
end

if(event.other.type == "RightWall") then
    yPos_2 = ball.y
    if(horizontalMotionFlag==0)then
        horizontalMotionFlag = 1
    else
        if(math.abs(yPos_1-yPos_2) < 50)then
            print("CoHorizontal motion detected. Change angle...")
            horizontalMotionFlag = 0
            ball:applyForce( 0, 1, ball.x, ball.y )  -- apply a small downward force
            ball:applyForce( 0, 0, ball.x, ball.y )  -- resetting the force
            -- You can also check the direction of ball and apply force to -1(upwards) also --
        end
    end
end
------------------------------------------------------------------------------------------

然后在里面添加以下行event.other.type == "destructible"event.other.type == "bottomWall"

horizontalMotionFlag = 0;

继续编码...... :)

于 2013-07-24T19:09:24.177 回答