as3炮彈飛行和碰撞演算法

來源:互聯網
上載者:User

 import flash.display.Sprite;
 import flash.display.Stage;
 import flash.events.Event;
 import flash.geom.Point;
 
 import zujian.Tank;
 
 public class Paodan extends Sprite
 {
  private var ball:Ball;
  private var point:Point=new Point();
  private var dx:Number;
  private var dy:Number;
  private var vx:Number;
  private var vy:Number;
  private var paodan:Paodan;
  private var speed:Number=8;
  private var stg:Stage;
  public function Paodan()
  {
   ball = new Ball();
   this.addChild(ball);
  }
  public function beginFly(paodan:Paodan,stage:Stage,tanke:Tank){
   this.paodan=paodan;
   this.stg = stage;
   point.x=stg.mouseX;
   point.y=stg.mouseY;
   dx=point.x-tanke.x;
   dy=point.y-tanke.y;
   var angle:Number = Math.atan2(dy, dx);  //計算方塊對滑鼠位置的角度
   vx = Math.cos(angle) * speed; //x方向移動距離/每楨
   vy = Math.sin(angle) * speed; //y方向移動距離/每楨
   paodan.x += vx*5.5;
   paodan.y += vy*5.5;
   this.paodan.addEventListener(Event.ENTER_FRAME,fly);
  }
  private function fly(evt:Event):void{
   var point:Point = new Point();
   point.x = paodan.x;
   point.y = paodan.y;
   var arr:Array=stg.getObjectsUnderPoint(point);
   if(arr.length>1){
    this.paodan.removeEventListener(Event.ENTER_FRAME,fly);
   }   
   paodan.x += vx;
   paodan.y += vy;
  }

 }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.