Brian Chia
       
 
categories
actionscript
actionscript 1.0
actionscript 2.0
misc
 
processing
 
links
brian chia
kelvin zhao
ronald leong
 
 
Bitmaps HitTest v01
 
Previously in Flash MX and earlier, if we want to do a precise hit collision, we will need to trace the image and create an invisible layer to insert in the button hit state.

Now, with BitmapData.hitTest(), we no longer need to do this. What we need is to import an transparent background bitmap and convert it into a BitmapData Object.

In the sample example below, i import in a bitmap and give it a linkage ID in the library so that i can use the BitmapData.loadBitmap() to load the bitmap. Next i perform a pixel-level hit detection between the loaded bitmap and another bitmap image.



import flash.display.BitmapData;
import flash.geom.*;

// create empty movieclip to store bitmap for precise hittest
var dm:MovieClip=createEmptyMovieClip("dm",getNextHighestDepth());

// loads a bitmap tat is identified by a linkage ID in the library
var db:BitmapData=BitmapData.loadBitmap("porsche");
// create another bitmap(1 by 1 pixel) for bitmapdata hittest comparsion
var cb:BitmapData=new BitmapData(1,1,false,0x00000000);

// attach the bitmap to the movieclip
dm.attachBitmap(db,1);

// define pixel location in 2 BitmapData instance
var dp:Point=new Point();
var cp:Point=new Point();

// define first pixel location point in first BitmapData instance
dp.x=dm._x;
dp.y=dm._y;

this.onEnterFrame=function():Void{

// define first pixel location point in second BitmapData instance
// set to _xmouse and _ymouse

cp.x=_xmouse;
cp.y=_ymouse;

// performs pixel-level hit detection
if(cb.hitTest(cp,255,db,dp,255)){

n_txt.text="PORSCHE DESIGN'S LEATHER";

}else{

n_txt.text="";

}

}



suggestion

 
 
 
         
  © copyright 2007 THINKLUNATIC.    
Brian Chia Jonathan Ng