2009-04-16 24 views
0

我有这个代码,使球弹跳,但我所寻找的是从地面射击子弹,一旦他们击中球,他们应该反弹回来。目标不是让球击中地面。我确信之前已经完成了这项工作,但我觉得很难理解。使用命中测试弹跳球在行动脚本3

验证码:

package { 

public class ball extends MovieClip { 

    var timer:Number=0; 
    var initialPos:Number=0; 
    var finalPos:Number=0; 
    var currentPos:Number=0; 
    var initialSpeed:Number=0; 




    function ball() { 

     startFallingBall(); 
    } 

    function moveBallDown(e:Event) { 
     timer+=1; 

     this.y = initialPos + .5 *(timer * timer); 
     checkBottomBoundary(); 
    } 

    function moveBallUp(e:Event) { 
     timer+=1; 

     var posA=this.y; 

     this.y = currentPos - initialSpeed*timer + .5*(timer * timer); 

     var posB=this.y; 

     checkTopBoundary(posA, posB); 
    } 

    function checkBottomBoundary() { 
     if (this.y+this.height>stage.stageHeight) { 
      finalPos=this.y; 

      stopFallingBall(); 
     } 
    } 

    function checkTopBoundary(firstPos:Number, secondPos:Number) { 
     if (secondPos>firstPos) { 
      stopRisingBall(); 
      startFallingBall(); 
     } 
    } 

    function startFallingBall() { 
     timer=0; 
     initialPos=this.y; 
     this.addEventListener(Event.ENTER_FRAME, moveBallDown); 
    } 

    function stopFallingBall() { 
     this.removeEventListener(Event.ENTER_FRAME, moveBallDown); 


     if (finalPos-initialPos<.1*this.height) { 
      stopRisingBall(); 
     } else { 
      startRisingBall(); 
     } 
    } 

    function startRisingBall() { 
     initialSpeed=Math.sqrt(Math.abs(finalPos-initialPos)); 
     timer=0; 

     currentPos=this.y; 

     this.addEventListener(Event.ENTER_FRAME, moveBallUp); 
    } 

    function stopRisingBall() { 
     this.removeEventListener(Event.ENTER_FRAME, moveBallUp); 
    } 

    function stopEverything() { 
     this.removeEventListener(Event.ENTER_FRAME, moveBallUp); 
     this.removeEventListener(Event.ENTER_FRAME, moveBallDown); 
    } 
} 

}

回答

0

有用于预成型则hitTest功能,你可以在这里读到它: http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/DisplayObject.html#hitTestObject()

虽然与圈子里是很容易,如果你知道半径,如果中心之间的距离小于它们的半径之和;他们互相接触。

但通过查看你的代码,你应该正确地重写整个事情。球实际上不应该在内部处理碰撞和运动逻辑。它应该只有一个位置和速度向量,并且运动和分离逻辑应该在其他类中。

至于反应的代码,这取决于你想要它有多复杂。而且可以通过简单地翻转速度矢量的y部分来实现相当复杂的数学运算。

在这个领域有很多东西和例子,我想你会更好的在谷歌上找到一些东西并玩它。然后写你自己的。

1

一个简单的方法是使用DisplayObject的hitTestObject。这检查重叠。你当然可以自己写一些更高级的东西。

对于每个游戏更新,你检查是否有子弹击中球,并采取行动,如果他们这样做。你还应该考虑分离游戏逻辑和显示更新。看看使用计时器。

package { 
    public class BallGame extends Sprite 
    { 
     private var ball:Ball; 
     private var bullets:Array; 

     public function BallGame() { 
      addEventListener(Event.ENTER_FRAME, checkCollision); 
      addEventListener(MouseEvent.CLICK, fireBullet); 
     } 

     private function fireBullet(e:MouseEvent):void { 
      // adds a fired bullet to an array so we can loop over all bullets 
      bullets.push(new Bullet()); 
     } 

     private function checkCollision(e:Event):void { 
      // loops through all bullets in play 
      for each(var bullet:Bullet in bullets) { 
       // check if the bullet is inside the ball 
       if (ball.hitTestObject(bullet)) { 
        // the bullet hit the ball 
        ball.startRisingBall(); 

        // TODO: remove the bullet :) 
       } 
      } 
     } 
    } 
}