2016-07-25 85 views
0

嗨我想做一个简单的pong游戏,我遇到了碰撞检测的麻烦。球没有登记桨。碰撞检测不注册球

function moveBall() { 
    var rightRadius = ballX + radius; 
    var leftRadius = ballX -radius; 

    if (ballX + radius > canvas.width || ballX - radius < 0) { 
     ballOffX = -ballOffX; 
    } 
    /* 
    The following code is handling the collision of the ball with the plate 
    */ 
    if((rightRadius <= (player1.x + paddleWidth))&&(leftRadius >= player1.x) &&(player1.y == ballY + 10)){ 
     ballOffY = -ballOffY; 
    } 

    ballX += ballOffX; 
    ballY += ballOffY; 

} 

enter image description here

+0

球员球盘?你能展示一个示例图片吗? –

+0

我重命名板,希望这有助于。谢谢 – Muffin

+0

您应该使用矢量来检测碰撞。 – kollein

回答

0

我做了一个if语句来检测在JavaScript中的碰撞,那就是:

if circle x < rect x + circle width && circle x + rect width > rect x && circle y < rect y + circle height && rect height + circle y > rect y { 

这部作品通过把球内的“虚构的盒子”,并在任何的'虚拟盒子'的边缘碰到矩形的任何边缘,就会检测到碰撞。