展开全部
Java像素碰撞检测原理大体分为基于屏幕像素颜色检测,当移动的62616964757a686964616fe78988e69d8331333337396332目标坐标像素的颜色与目标颜色相同时为发生碰撞。坐标检测,就是维持各感兴趣物体的坐标数据,判断二者坐标相同时为发生碰撞。可以使用ActionScript3.0自带bitmapdata功能对像素级碰撞检测:package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Shape;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.filters.GlowFilter;
import flash.geom.Matrix;
import flash.geom.Point;
[SWF(width="1000",height="600")]
public class Bitbdhitest extends Sprite
{
private var bd1:Bitmap;
private var bd2:Bitmap;
public function Bitbdhitest()
{
super();
stage.scaleMode=StageScaleMode.NO_SCALE;
stage.align=StageAlign.TOP_LEFT;
init();
}
private function init():void
{
var bdp1:BitmapData=new BitmapData(100,100,true,0);
bdp1.draw(getstar(50),new Matrix(1,0,0,1,50,50));
bd1=new Bitmap(bdp1);
bd1.x=200;
bd1.y=200;
this.addChild(bd1);
var bdp2:BitmapData=new BitmapData(100,100,true,0);
bdp2.draw(getstar(50),new Matrix(1,0,0,1,50,50));
bd2=new Bitmap(bdp2);
this.addChild(bd2);
this.addEventListener(Event.ENTER_FRAME,onMouseMove)
}
private function onMouseMove(e:Event):void
{
bd2.x=mouseX-50;
bd2.y=mouseY-50;
if(bd1.bitmapData.hitTest(new Point(bd1.x,bd1.y),1,bd2.bitmapData,new Point(bd2.x,bd2.y),255))
{
bd1.filters=[new GlowFilter];
bd2.filters=[new GlowFilter];
}
else
{
bd1.filters=[];
bd2.filters=[];
}
}
private function getstar(_r:Number):Sprite
{
var _star:Sprite=new Sprite;
_star.graphics.lineStyle(0);
_star.graphics.moveTo(_r,0);
for(var i:int=1;i<11;i++)
{
var r2:Number=_r;
if(i%2>0)
{
r2=_r/2;
}
var angle:Number=Math.PI*2/10*i;
_star.graphics.lineTo(Math.cos(angle)*r2,Math.sin(angle)*r2);
}
return _star;
}
}
}