2011年9月9日 星期五

as3 FLIXEL 真實碰撞 prefect collide

範例是使用鍵盤控制 用上下左右...
step1:使用overlap偵測碰撞
FlxG.overlap(targetImg,this,onOverlap);

step2:使用perfectCollision function偵測是否為真實碰撞
private function onOverlap(targetimg:FlxSprite,overlapimg:FlxSprite):void
{
if(perfectCollision(targetimg,overlapimg))
output_txt.text="true";
else
output_txt.text="false";
}

protected function perfectCollision(s1:FlxSprite,s2:FlxSprite):Boolean {
var alphaThreshold:uint = 0xFF;
var p1:FlxPoint=getScreenXY(s1);
var p2:FlxPoint=getScreenXY(s2);
return s1.framePixels.hitTest(new Point(p1.x,p1.y),alphaThreshold, s2.framePixels,new Point(p2.x,p2.y));
}

protected function getScreenXY(target:FlxSprite):FlxPoint
{
return target.getScreenXY(new FlxPoint());
}

完成~

沒有留言:

張貼留言