Lua Breakout游戏教程:球的怪异行为

时间:2013-07-24 16:57:44

标签: lua corona

我跟着这个tutorial进行了突破性比赛,但是在某些时候,当球角太大(太水平)时,球仍然靠在顶壁上。我可以调整任何逻辑,以便球可以避免这种行为吗?

以下是截图:

screenshot

球的相关源代码是:

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

1 个答案:

答案 0 :(得分: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;

保持编码.............:)