2013-06-24 19 views
0

無法找出爲什麼球對象從線條[2]中掉下來,而它從所有其他線條反彈。我有兩個班級,一個創建線條的主類,以及球的一些碰撞和運動代碼,以及球類。我在主類中包含了一條註釋,該註釋與不相互作用的行相關。當程序運行時,它反彈關閉所有與一些距離基於碰撞代碼行的 - 但右通過線 - 線在所述陣列中產生的線的一個[2]actionscript 3.0找不到爲什麼碰撞失敗,線條陣列中的一條線條發生故障

package 
{ 
    import flash.display.Sprite; 
    import flash.events.Event; 
    import flash.geom.Rectangle; 

    public class MultiAngleBounce extends Sprite 
    { 
     private var ball:Ball; 
     private var lines:Array; 
     private var numLines:uint = 6; 
     private var gravity:Number = 0.3; 
     private var bounce:Number = -0.6; 



     public function MultiAngleBounce() 
     { 
      init(); 
     } 

     private function init():void 
     { 
      ball = new Ball(20); 
      addChild(ball); 
      ball.x = 100; 
      ball.y = 100; 

      //create five lines 
      lines = new Array(); 
      for(var i:uint = 0; i < numLines; i++) 
      { 
       var line:Sprite = new Sprite(); 
       line.graphics.lineStyle(1); 
       line.graphics.moveTo(-50, 0); 
       line.graphics.lineTo(50, 0); 
       addChild(line); 
       lines.push(line); 
      } 

      lines[0].x = 100; 
      lines[0].y = 100; 
      lines[0].rotation = 30; 

      lines[1].x = 100; 
      lines[1].y = 230; 
      lines[1].rotation = 50; 

      //why does this one get ignored 
      lines[2].x = 250; 
      lines[2].y = 180; 
      lines[2].rotation = -30; 

      lines[3].x = 150; 
      lines[3].y = 330; 
      lines[3].rotation = 10; 

      lines[4].x = 230; 
      lines[4].y = 250; 
      lines[4].rotation = -30; 

      lines[5].x = 300; 
      lines[5].y = 350; 
      lines[5].rotation = -20; 

      addEventListener(Event.ENTER_FRAME, onEnterFrame); 

     } 

     private function onEnterFrame(event:Event):void 
     { 
      //normal motion code 
      ball.vy += gravity; 
      ball.x += ball.vx; 
      ball.y += ball.vy; 

      //bounce off ceiling, floor, walls 
      if(ball.x + ball.radius > stage.stageWidth) 
      { 
       ball.x = stage.stageWidth - ball.radius; 
       ball.vx *= bounce; 
      } 
      else if(ball.x - ball.radius < 0) 
      { 
       ball.x = ball.radius; 
       ball.vx *= bounce; 
      } 
      if(ball.y + ball.radius > stage.stageHeight) 
      { 
       ball.y = stage.stageHeight - ball.radius 
       ball.vy *= bounce; 
      } 
      else if(ball.y - ball.radius < 0) 
      { 
       ball.y = ball.radius; 
       ball.vy *= bounce; 
      } 

      //check each line 
      for(var i:uint = 0; i < numLines; i++) 
      { 
       checkLine(lines[i]); 
      } 
     } 

     private function checkLine(line:Sprite):void 
     { 
      //get the bounding box of the line 
      var bounds:Rectangle = line.getBounds(this); 
      if(ball.x > bounds.left && ball.x < bounds.right) 
      { 
       //get angle, sine and cosine 
       var angle:Number = line.rotation * Math.PI/180; 
       var cos:Number = Math.cos(angle); 
       var sin:Number = Math.sin(angle); 

       //get position of ball, relative to line 
       var x1:Number = ball.x - line.x; 
       var y1:Number = ball.y - line.y; 

       //rotate coordinates 
       var y2:Number = cos * y1 - sin * x1; 

       //rotate velocity 
       var vy1:Number = cos * ball.vy - sin * ball.vx; 

       //perform bounce with rotated values 
       if(y2 > -ball.height/2 && y2 < vy1) 
       { 
        //rotate coordinates 
        var x2:Number = cos * x1 + sin * y1; 

        //rotate velocity 
        var vx1:Number = cos * ball.vx + sin * ball.vy; 

        y2 = -ball.height/2; 
        vy1 *= bounce; 

        //rotate everything back; 
        x1 = cos * x2 - sin * y2; 
        y1 = cos * y2 + sin * x2; 
        ball.vx = cos * vx1 - sin * vy1; 
        ball.vy = cos * vy1 + sin * vx1; 
        ball.x = line.x + x1; 
        ball.y = line.y + y1; 
       } 
      } 
     } 
    } 
} 



package 
{ 
    import flash.display.Sprite; 

    public class Ball extends Sprite 
    { 
     public var radius:Number; 
     private var color:uint; 
     public var internalAngle:Number; 
     public var internalRadius:Number; 
     internal var vx:Number = 0; 
     internal var vy:Number = 0; 

     public function Ball(radius:Number = 40, color:uint = 0xff0000) 
     { 
      this.radius = radius; 
      this.color = color; 
      init(); 
     } 

     public function init():void { 
      graphics.beginFill(color); 
      graphics.drawCircle(0, 0, radius); 
      graphics.endFill(); 

     } 
    } 
} 
+0

我們需要更多標籤/背景。 thnx – Coffee

+1

這是Actionscript嗎? –

+0

對不起yes-actionscript 3.0。我在評論中增加了一點 - 但不確定還有什麼要說的。一個球對象從主類生成的每個線圖上反彈 - 除了一個 - 球位置爲2的陣列中球穿過它的線。查看代碼,我發現該行和數組中其餘行之間沒有任何不同。我不知道它是一個錯誤的動作本身 –

回答

0

您界限檢查通過

if(ball.x > bounds.left && ball.x < bounds.right) 

對第二行不合格。在達到線[2]時,球未達到足夠的速度,以至於它可以完全進入邊界線。你可以使用

if(line.hitTestObject(ball)) 

來代替碰撞檢測。

+0

的感謝!我將第二行的x值向左移動一點(降低行[2] .x和),並看到你確實是正確的 –