這是上一個問題中我的程序的更新版本。 目前,我試圖確定爲什麼監視我的盒子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