如何在flash中的指定区域触发事件(用AS3.0实现)
例如,现在有一个九宫格(好比数字键盘的1--9一般),当鱼游动到不同的格子时,按下对应的按键可以将鱼抓住,鱼在九宫格内游动的方向是随机的(即做的是布朗运动),速度恒定,有...
例如,现在有一个九宫格(好比数字键盘的1--9一般),当鱼游动到不同的格子时,按下对应的按键可以将鱼抓住,鱼在九宫格内游动的方向是随机的(即做的是布朗运动),速度恒定,有4种鱼,不同的鱼有不同的速度。
展开
2个回答
展开全部
新建一个FLA,把下面代码贴进去,用鼠标点击区域捉鱼。一些关键的参数我都注释出来了,直接修改就行了。
stage.scaleMode = StageScaleMode.SHOW_ALL;
Mouse.cursor = MouseCursor.BUTTON;
stage.frameRate = 60;
var edge:Number = 5;//边缘宽度
var sizeX:int = 3;//横向分割数
var sizeY:int = 3;//纵向分割数
var border:Sprite = new Sprite();
var borderArr:Array = new Array();
var fishArr:Array = new Array();
var fishNum:int = 4;//鱼的数量
var fishSize:Number = 10;//鱼的大小
var catching:Boolean = false;
var clickRect:Rectangle;
var tf:TextField = new TextField();
drawBorder();
drawFish();
drawMouseTF();
this.addEventListener(Event.ENTER_FRAME,enterFrameHandler);
stage.addEventListener(MouseEvent.CLICK,onClickHandler);
function drawBorder():void {
with (border.graphics) {
lineStyle(1);
var borderWidth:Number = stage.stageWidth - 2 * edge;
var borderHeight:Number = stage.stageHeight - 2 * edge;
for (var i:int=0; i<=sizeX; i++) {
moveTo(edge + i* borderWidth / sizeX,edge);
lineTo(edge + i* borderWidth / sizeX, stage.stageHeight - edge);
}
for (var j:int=0; j<=sizeY; j++) {
moveTo(edge, edge + j* borderHeight / sizeY);
lineTo(stage.stageWidth - edge, edge + j* borderHeight / sizeY);
}
for (var m:int=0; m<sizeX; m++) {
for (var n:int=0; n<sizeY; n++) {
var rect:Rectangle = new Rectangle(m*(stage.stageWidth - edge)/sizeX + edge,n*(stage.stageHeight - edge)/sizeY + edge,(stage.stageWidth - edge)/sizeX,(stage.stageHeight - edge)/sizeY);
borderArr.push(rect);
}
}
}
this.addChild(border);
}
function drawFish():void {
for (var i:int=0; i<fishNum; i++) {
var obj:Object = new Object();
obj.fish = getFish();
obj.fish.x = edge + Math.random()*(stage.stageWidth - edge * 2) - obj.fish.width / 2;
obj.fish.y = edge + Math.random()*(stage.stageHeight - edge * 2) + obj.fish.height / 2;
obj.target = new Point(obj.fish.x,obj.fish.y);
obj.distance = getDistance(new Point(obj.fish.x,obj.fish.y),obj.target);
obj.speed = 2 + Math.round(Math.random() * 4);
obj.beCatch = false;
obj.shock = 0;
fishArr.push(obj);
this.addChild(obj.fish);
}
}
function drawMouseTF():void {
tf.mouseEnabled = false;
tf.width = 200;
tf.x = 50;
tf.y = 50;
this.addChild(tf);
}
function getFish():Sprite {
var fish:Sprite = new Sprite();
with (fish.graphics) {
var color:uint = 0;
var red:uint = 0;
var green:uint = 0;
var blue:uint = 0;
while (red < 33 && green < 33 && blue < 33) {
color = uint(Math.random() * 0xFFFFFF);
red = color >> 16 & 0xFF;
green = color >> 8 & 0xFF;
blue = color & 0xFF;
}
beginFill(color);
drawCircle(0,0,fishSize);
endFill();
}
return fish;
}
function enterFrameHandler(e:Event):void {
for (var i:int=0; i<fishArr.length; i++) {
var obj:Object = fishArr[i];
if (obj.fish.alpha == 0) {
fishArr.splice(i,1);
} else if (obj.beCatch) {
if (obj.shock % 2 == 0) {
obj.fish.scaleX += 0.1;
obj.fish.scaleY+=0.1;
if (obj.fish.scaleX>=2) {
obj.shock++;
}
} else {
obj.fish.scaleX-=0.1;
obj.fish.scaleY-=0.1;
if (obj.fish.scaleX<=0.5) {
obj.shock++;
}
}
if (obj.shock>=6) {
obj.fish.scaleX=1;
obj.fish.scaleY=1;
obj.fish.alpha=0;
obj.beCatch=false;
catching=false;
this.removeChild(obj.fish);
}
} else {
var dis:Number=getDistance(new Point(obj.fish.x,obj.fish.y),new Point(obj.target.x,obj.target.y));
if (dis<=obj.speed) {
obj.fish.x=obj.target.x;
obj.fish.y=obj.target.y;
obj.target = new Point(edge + Math.random()*(stage.stageWidth - edge * 2) - obj.fish.width / 2,edge + Math.random()*(stage.stageHeight - edge * 2) + obj.fish.height / 2);
obj.distance=getDistance(new Point(obj.fish.x,obj.fish.y),obj.target);
} else {
obj.fish.x+=obj.speed*Math.cos(getAngle(obj.target,new Point(obj.fish.x,obj.fish.y)));
obj.fish.y+=obj.speed*Math.sin(getAngle(obj.target,new Point(obj.fish.x,obj.fish.y)));
}
}
}
tf.text="X:"+mouseX+",Y:"+mouseY;
if (fishArr.length==0) {
this.removeEventListener(Event.ENTER_FRAME,enterFrameHandler);
stage.removeEventListener(MouseEvent.CLICK,onClickHandler);
gameOver();
}
}
function getDistance(pointA:Point,pointB:Point):Number {
var dis:Number=Math.sqrt(Math.pow(pointA.x-pointB.x,2)+Math.pow(pointA.y-pointB.y,2));
return dis;
}
function getAngle(pointA:Point,pointB:Point):Number {
var angle:Number=Math.atan2(pointA.y-pointB.y,pointA.x-pointB.x);
return angle;
}
function onClickHandler(e:MouseEvent):void {
if (! catching) {
var mX:Number=this.mouseX;
var mY:Number=this.mouseY;
for (var i:int=0; i<borderArr.length; i++) {
var rect:Rectangle=borderArr[i];
if (mX>=rect.x&&mX<=rect.right&&mY>=rect.y&&mY<=rect.bottom) {
clickRect=rect;
}
}
if (clickRect!=null) {
for (var j:int=0; j<fishArr.length; j++) {
var obj:Object=fishArr[j];
if (obj.fish.x>=clickRect.x&&obj.fish.x<=clickRect.right&&obj.fish.y>=clickRect.y&&obj.fish.y<=clickRect.bottom) {
obj.beCatch=true;
catching=true;
}
}
}
clickRect=null;
}
}
function gameOver():void {
var format:TextFormat = new TextFormat();
format.font="黑体";
format.bold = true;
format.size=24;
format.color=0x000000;
tf.text="Game Over";
tf.setTextFormat(format);
tf.width=int(format.size)*tf.length;
tf.x=stage.stageWidth/2-tf.width/2;
tf.y=stage.stageHeight/2-tf.height/2;
}
stage.scaleMode = StageScaleMode.SHOW_ALL;
Mouse.cursor = MouseCursor.BUTTON;
stage.frameRate = 60;
var edge:Number = 5;//边缘宽度
var sizeX:int = 3;//横向分割数
var sizeY:int = 3;//纵向分割数
var border:Sprite = new Sprite();
var borderArr:Array = new Array();
var fishArr:Array = new Array();
var fishNum:int = 4;//鱼的数量
var fishSize:Number = 10;//鱼的大小
var catching:Boolean = false;
var clickRect:Rectangle;
var tf:TextField = new TextField();
drawBorder();
drawFish();
drawMouseTF();
this.addEventListener(Event.ENTER_FRAME,enterFrameHandler);
stage.addEventListener(MouseEvent.CLICK,onClickHandler);
function drawBorder():void {
with (border.graphics) {
lineStyle(1);
var borderWidth:Number = stage.stageWidth - 2 * edge;
var borderHeight:Number = stage.stageHeight - 2 * edge;
for (var i:int=0; i<=sizeX; i++) {
moveTo(edge + i* borderWidth / sizeX,edge);
lineTo(edge + i* borderWidth / sizeX, stage.stageHeight - edge);
}
for (var j:int=0; j<=sizeY; j++) {
moveTo(edge, edge + j* borderHeight / sizeY);
lineTo(stage.stageWidth - edge, edge + j* borderHeight / sizeY);
}
for (var m:int=0; m<sizeX; m++) {
for (var n:int=0; n<sizeY; n++) {
var rect:Rectangle = new Rectangle(m*(stage.stageWidth - edge)/sizeX + edge,n*(stage.stageHeight - edge)/sizeY + edge,(stage.stageWidth - edge)/sizeX,(stage.stageHeight - edge)/sizeY);
borderArr.push(rect);
}
}
}
this.addChild(border);
}
function drawFish():void {
for (var i:int=0; i<fishNum; i++) {
var obj:Object = new Object();
obj.fish = getFish();
obj.fish.x = edge + Math.random()*(stage.stageWidth - edge * 2) - obj.fish.width / 2;
obj.fish.y = edge + Math.random()*(stage.stageHeight - edge * 2) + obj.fish.height / 2;
obj.target = new Point(obj.fish.x,obj.fish.y);
obj.distance = getDistance(new Point(obj.fish.x,obj.fish.y),obj.target);
obj.speed = 2 + Math.round(Math.random() * 4);
obj.beCatch = false;
obj.shock = 0;
fishArr.push(obj);
this.addChild(obj.fish);
}
}
function drawMouseTF():void {
tf.mouseEnabled = false;
tf.width = 200;
tf.x = 50;
tf.y = 50;
this.addChild(tf);
}
function getFish():Sprite {
var fish:Sprite = new Sprite();
with (fish.graphics) {
var color:uint = 0;
var red:uint = 0;
var green:uint = 0;
var blue:uint = 0;
while (red < 33 && green < 33 && blue < 33) {
color = uint(Math.random() * 0xFFFFFF);
red = color >> 16 & 0xFF;
green = color >> 8 & 0xFF;
blue = color & 0xFF;
}
beginFill(color);
drawCircle(0,0,fishSize);
endFill();
}
return fish;
}
function enterFrameHandler(e:Event):void {
for (var i:int=0; i<fishArr.length; i++) {
var obj:Object = fishArr[i];
if (obj.fish.alpha == 0) {
fishArr.splice(i,1);
} else if (obj.beCatch) {
if (obj.shock % 2 == 0) {
obj.fish.scaleX += 0.1;
obj.fish.scaleY+=0.1;
if (obj.fish.scaleX>=2) {
obj.shock++;
}
} else {
obj.fish.scaleX-=0.1;
obj.fish.scaleY-=0.1;
if (obj.fish.scaleX<=0.5) {
obj.shock++;
}
}
if (obj.shock>=6) {
obj.fish.scaleX=1;
obj.fish.scaleY=1;
obj.fish.alpha=0;
obj.beCatch=false;
catching=false;
this.removeChild(obj.fish);
}
} else {
var dis:Number=getDistance(new Point(obj.fish.x,obj.fish.y),new Point(obj.target.x,obj.target.y));
if (dis<=obj.speed) {
obj.fish.x=obj.target.x;
obj.fish.y=obj.target.y;
obj.target = new Point(edge + Math.random()*(stage.stageWidth - edge * 2) - obj.fish.width / 2,edge + Math.random()*(stage.stageHeight - edge * 2) + obj.fish.height / 2);
obj.distance=getDistance(new Point(obj.fish.x,obj.fish.y),obj.target);
} else {
obj.fish.x+=obj.speed*Math.cos(getAngle(obj.target,new Point(obj.fish.x,obj.fish.y)));
obj.fish.y+=obj.speed*Math.sin(getAngle(obj.target,new Point(obj.fish.x,obj.fish.y)));
}
}
}
tf.text="X:"+mouseX+",Y:"+mouseY;
if (fishArr.length==0) {
this.removeEventListener(Event.ENTER_FRAME,enterFrameHandler);
stage.removeEventListener(MouseEvent.CLICK,onClickHandler);
gameOver();
}
}
function getDistance(pointA:Point,pointB:Point):Number {
var dis:Number=Math.sqrt(Math.pow(pointA.x-pointB.x,2)+Math.pow(pointA.y-pointB.y,2));
return dis;
}
function getAngle(pointA:Point,pointB:Point):Number {
var angle:Number=Math.atan2(pointA.y-pointB.y,pointA.x-pointB.x);
return angle;
}
function onClickHandler(e:MouseEvent):void {
if (! catching) {
var mX:Number=this.mouseX;
var mY:Number=this.mouseY;
for (var i:int=0; i<borderArr.length; i++) {
var rect:Rectangle=borderArr[i];
if (mX>=rect.x&&mX<=rect.right&&mY>=rect.y&&mY<=rect.bottom) {
clickRect=rect;
}
}
if (clickRect!=null) {
for (var j:int=0; j<fishArr.length; j++) {
var obj:Object=fishArr[j];
if (obj.fish.x>=clickRect.x&&obj.fish.x<=clickRect.right&&obj.fish.y>=clickRect.y&&obj.fish.y<=clickRect.bottom) {
obj.beCatch=true;
catching=true;
}
}
}
clickRect=null;
}
}
function gameOver():void {
var format:TextFormat = new TextFormat();
format.font="黑体";
format.bold = true;
format.size=24;
format.color=0x000000;
tf.text="Game Over";
tf.setTextFormat(format);
tf.width=int(format.size)*tf.length;
tf.x=stage.stageWidth/2-tf.width/2;
tf.y=stage.stageHeight/2-tf.height/2;
}
2010-08-02
展开全部
用Rectangle造一个区域就行了。要是鱼与这个区域撞碰了。就逮到鱼了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询