这是上一个问题中我的程序的更新版本。 目前,我试图确定为什么监视我的盒子div的顶部和底部我MAXTOP变量返回NaN的值。球在边界外跳动
当我运行我的程序,球反弹的左侧和右侧墙壁上,但是当它到达底部/顶壁离开。有什么建议,为什么?
我的变量如下:
currentLeftPos = parseInt($("#ball").css('left'));
currentTopPos = parseInt($("#ball").css('top'));
// define the new position
var newLeftPos = currentLeftPos + velocity_x;
//alert(velocity_x);
var newTopPos = currentTopPos + velocity_y;
//alert(velocity_y);
// If the left position is greater than or equal to the maximum distance allowed or
// less than or equal to zero, then change the direction.
if(newLeftPos >= maxLeft || newLeftPos <= 0)
velocity_x *= -1; // multiply the value by -1
if(newTopPos >= maxTop || newTopPos <= 0)
velocity_y *= -1; // multiply the value by -1
maxLeft = parseInt($("#outerbox").css('width')) -parseInt($("#outerbox").css('border-left-width')) - parseInt($("#ball").css('width'));
maxTop = parseInt($("#outerbox").css('top')) -parseInt($("#outerbox").css('border-left-top')) - parseInt($("#ball").css('top'));
https://jsfiddle.net/W4Km8/4884/
这是“边框顶部宽”,而不是边界-top-height – Taplar
调试它并不困难,告诉你'border-top-height'是未定义的。没有像这样的CSS属性 –
在这样的情况下,你的结果是意想不到的,把它的每一部分和console.log()它通常是有帮助的,以确保你得到你所期望的。 – Taplar