【游客模式】——注册会员,加入11RIA 闪客社区吧!一起见证Flash的再次辉煌……
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
本帖最后由 雪原xy 于 2019-2-21 14:50 编辑
[Actionscript3] 纯文本查看 复制代码 //显示对象小练习之三(碰撞检测);
package{
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Point;
public class SampleHit extends Sprite{
//定义一个三角形图形;
private var triangle:Sprite=new Sprite();
//定义一个矩形图形;
private var rect:Sprite=new Sprite();
//定义一个点;
private var P:Point=new Point(150,200);
//下面是构造函数;
public function SampleHit():void{
//画三角形;
drawT();
//画一个点,这是为了看清点在哪里,方便调试;
drawP();
//拖拽三角形;
triangle.startDrag(true);
//画矩形;
drawR();
//注册侦听器:检测碰撞;
triangle.addEventListener(Event.ENTER_FRAME,hitFun);
}
private function drawT():void{
triangle.graphics.lineStyle(3,0xffffff);
triangle.graphics.beginFill(0x996600);
triangle.graphics.moveTo(60,0);
triangle.graphics.lineTo(30,40);
triangle.graphics.lineTo(90,40);
triangle.graphics.lineTo(60,0);
triangle.graphics.endFill();
addChild(triangle);
}
private function drawP():void{
this.graphics.beginFill(0x990000);
this.graphics.drawCircle(P.x,P.y,5);
this.graphics.endFill();
}
private function drawR():void{
rect.graphics.lineStyle(3,0xffffff);
rect.graphics.beginFill(0x00ff00);
rect.graphics.drawRect(100,80,100,80);
rect.graphics.endFill();
addChild(rect);
}
private function hitFun(e:Event):void{
if(e.target.hitTestObject(rect)){
trace("你碰到了矩形!");
//矩形的长宽减少2;
rect.height-=2;
rect.width-=2;
}
if(e.target.hitTestPoint(P.x,P.y,true)){
trace("你碰到了点!");
//三角形发生形变;
e.target.scaleX=0.5;
e.target.scaleY=2;
}
}
}
}
|