如果从JQuery开始是真的

时间:2015-03-26 12:57:16

标签: jquery

我正在打乒乓球比赛,想要在比赛结束时重置球。右边缘的代码工作得很好但是当我添加左边缘的代码时它会冻结球。实际上它会一遍又一遍地重置。因为if从开始就是真的(idk如何)

这是代码的一部分工作正常。当击中右边缘时重击左边反弹

function moveBall() {
// reference useful variables
var playgroundHeight = parseInt($("#playground").height());
var playgroundWidth = parseInt($("#playground").width());
var ball = pingpong.ball;
// check playground boundary
// check bottom edge
if (ball.y + ball.speed*ball.directionY > playgroundHeight)
{
ball.directionY = -1;
}
// check top edge
if (ball.y + ball.speed*ball.directionY < 0)
{
ball.directionY = 1;
}
// check right edge
if (ball.x +ball.speed*ball.directionX > playgroundWidth)
{
// player B lost.


// reset the ball;
ball.x = 250;
ball.y = 100;
$("#ball").css({
"left": ball.x,
"top" : ball.y
});
ball.directionX = -1;
}
// check left edge


if (ball.x + ball.speed*ball.directionX < 0)
{
ball.directionX = 1;
}
ball.x += ball.speed * ball.directionX;
ball.y += ball.speed * ball.directionY;

但是当我为左边添加这条线时

// check left edge
if (ball.x + ball.speed*ball.directionX < 0)
{
// player A lost.
// reset the ball;
ball.x = 150;
ball.y = 100;
$("#ball").css({
"left": ball.x,
"top" : ball.y
});
ball.directionX = 1;
}

球不再移动了。就像它一次又一次地重置一样。


0 个答案:

没有答案