Lua打砖块游戏教程:球的奇怪行为

我按照这个教程制作了一个打砖块游戏,但在某个时候,当球的角度太大(太水平)时,球会倾斜在顶部墙上。有什么逻辑可以调整,让球避免这种行为吗?

以下是屏幕截图:

screenshot

球的相关源代码是:

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

很奇怪,但我曾经像这样做过...

创建三个变量/标志:

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-24 19:09:24