2012年1月3日 星期二

[AS3] AS3 內建的 BitmapData.hitTest()方法 - 像素級碰撞偵測

[AS3] AS3 內建的 BitmapData.hitTest()方法 - 像素級碰撞偵測
BitmapData.hitTest()方法:
BitmapData.hitTest(firstPoint:Point, firstAlphaThreshold:uint, secondObject:Object, secondBitmapDataPoint:Point = null, secondAlphaThreshold:uint = 1):Boolean

A. BitmapData.hitTest() 似乎是個常被忽略的方法
B. 這與 DisplayObject 的 hitTestObject()hitTestPoint() 方法的能耐截然不同

















1010102_BitmapData.hitTest.swf
→ 操作:拖曳藍或黃球彼此碰撞,偵測結果顯示為true或false。



package  {
 
 import flash.display.MovieClip;
 import fl.motion.easing.Back;
 import flash.display.Bitmap;
 import flash.display.Sprite;
 import flash.events.MouseEvent;
 import flash.events.Event;
 import flash.geom.Point;
 import flash.text.TextField;
 
 
 public class Main extends MovieClip {
  
  private var bmdBallBlue:BmdBallBlue;
  private var bmdBallYellow:BmdBallYellow;
  private var bmBlue:Bitmap;
  private var bmYellow:Bitmap;
  private var blueSprite:Sprite;
  private var yellowSprite:Sprite;
  public var info:TextField;

  public function Main() {
   this.initObject();
   this.addMouseListener();
   return;
  }
  
  private function initObject():void{
   this.bmdBallBlue = new BmdBallBlue(145,145,true,0xFFFFFFFF);
   this.bmdBallYellow = new BmdBallYellow(145,145,true,0xFFFFFFFF);
   
   this.bmBlue = new Bitmap(this.bmdBallBlue);
   this.bmYellow = new Bitmap(this.bmdBallYellow);
   /*this.bmBlue.x = -this.bmBlue.width/2;
   this.bmBlue.y = -this.bmBlue.height/2;
   this.bmYellow.x = -this.bmYellow.width/2;
   this.bmYellow.y = -this.bmYellow.height/2;*/
   
   this.blueSprite = new Sprite();
   this.yellowSprite = new Sprite();
   this.blueSprite.x = 30;
   this.blueSprite.y = 30;
   this.yellowSprite.x = 350;
   this.yellowSprite.y = 220;
   
   this.addChild(this.blueSprite);
   this.addChild(this.yellowSprite);
   this.blueSprite.addChild(this.bmBlue);
   this.yellowSprite.addChild(this.bmYellow);
   
   this.info.border = true;
   this.info.borderColor = 0xFF0000;
   this.info.background = true;
   this.info.backgroundColor = 0xFF0000;
   return;
  }
  
  private function addMouseListener():void{
   this.stage.addEventListener(MouseEvent.MOUSE_DOWN,mouseHandler);
   this.stage.addEventListener(MouseEvent.MOUSE_UP,mouseHandler);
   return;
  }
  
  private function mouseHandler(event:MouseEvent):void{
   var tmpSprite:Sprite = event.target as Sprite;
   /*trace("tmpSprite : " + tmpSprite);*/
   switch(event.type){
    case "mouseDown":
     if(tmpSprite != null){
      this.drapMethod(tmpSprite);
      this.stage.addEventListener(MouseEvent.MOUSE_MOVE,mouseHandler);
     }
    break;
    
    case "mouseUp":
     if(tmpSprite != null){
      this.stopDrapMethod(tmpSprite);
     }
    break;
    
    case "mouseMove":
     if(tmpSprite != null){
      bmdHitTest();
     }
    break;
    
    default:
    break;
    
   }
   return;
  }
  
  private function drapMethod(_targetObject:Sprite):void{
   _targetObject.startDrag(false);
   return;
  }
  
  private function stopDrapMethod(_targetObject:Sprite):void{
   _targetObject.stopDrag();
   return;
  }
  
  /* AS3 的 BitmapData 像素級碰撞偵測 */
  private function bmdHitTest():void{
   var bmdBallBluePoint:Point = new Point(this.blueSprite.x, this.blueSprite.y);
   var bmdBallYellowPoint:Point = new Point(this.yellowSprite.x, this.yellowSprite.y);
   var isHit:Boolean = this.bmdBallBlue.hitTest(bmdBallBluePoint,0x55,bmdBallYellow,bmdBallYellowPoint,0x55);
   this.info.text = isHit.toString();
   return;
  }
 }
 
}


● startDrag()和stopDrag()方法由Sprite類別所定義

沒有留言: