2012-07-09 66 views
1

有一個弧檢測底部物體的位置。這個弧線或球直到第二次反彈時纔會反彈,並在地板上出現毛刺。變量ball.gSy控制球落下的速度。現在它被乘以10(ball.y += 10*ball.gSy;),以加速地板上的毛刺的影響。此外,您可以使用箭頭鍵來控制球,並且它會卡在側壁,但底層問題是我特別感興趣的問題。碰撞「毛刺」,如果你願意,在畫布

這裏是一個的jsfiddle鏈接,以更好地滿足您的惠顧: http://jsfiddle.net/nhQtk/

回答

1

你的問題是,你移動了球,但是當有一個碰撞,你不動球備份到一個位置,它不會卡在地板上。

因此,您正確地檢測到碰撞,但您的球已經部分穿過牆壁,無法向上移動。

下面是你需要做一個粗略的想法:

ball.y += 10*ball.gSy; 
for(var i = 0; i < objects.length; i++){ 
         if(ball.x > objects[i].x - ball.r && 
          ball.y > objects[i].y - ball.r && 
          ball.x < ball.r + objects[i].x + objects[i].w && 
          ball.y < ball.r + objects[i].y + objects[i].h) 
         { 
          ball.gSy = (-1 * ball.gSy); 
          ball.y -= 10;// <--- important step 
          // You need to calculate how much to move the ball BACK up to where it has not quite hit the floor. 

         } 
+1

**非常感謝你!** – Chief120 2012-07-09 06:16:17