Actionscript 3 处理多个TimerEvent
下面是我正在创建的一个游戏的简化版本。基本上,游戏有一些圆圈。每个圆圈都会发射与timerEvent一起发送的子弹。单击圆时,它将从舞台上移除。然而,子弹仍在继续发射。我不知道如何在单击每个圆圈时停止timerEventActionscript 3 处理多个TimerEvent,actionscript-3,event-handling,Actionscript 3,Event Handling,下面是我正在创建的一个游戏的简化版本。基本上,游戏有一些圆圈。每个圆圈都会发射与timerEvent一起发送的子弹。单击圆时,它将从舞台上移除。然而,子弹仍在继续发射。我不知道如何在单击每个圆圈时停止timerEvent var _timer:Timer = new Timer(1000); var speed:int = 20; for(var i:int=0; i<= 3; i++) { var _shape:MovieClip = new MovieClip();
var _timer:Timer = new Timer(1000);
var speed:int = 20;
for(var i:int=0; i<= 3; i++)
{
var _shape:MovieClip = new MovieClip();
_shape.graphics.beginFill(0x999999);
_shape.graphics.lineStyle(1, 0x000000);
_shape.graphics.drawCircle(0, 0, 20);
_shape.graphics.endFill();
addChild(_shape);
_shape.x = (stage.stageWidth)/5;
_shape.y = (stage.stageHeight)/3 + _shape.height*i*1.5;
_shape.name = "_shape"+i;
_shape.buttonMode = true;
_shape.addEventListener(MouseEvent.CLICK, removeMovieClip);
bullets(_shape);
}
function bullets(_shape:MovieClip):void
{
_timer = new Timer(Math.random()*100);
_timer.addEventListener(TimerEvent.TIMER, startFiring);
_timer.start();
function startFiring(e:TimerEvent):void
{
speed ++;
var _bullet:MovieClip = new MovieClip();
_bullet.graphics.beginFill(0x999999);
_bullet.graphics.lineStyle(1, 0x000000);
_bullet.graphics.drawRect(0, 0, 5,2);
_bullet.graphics.endFill();
addChild(_bullet);
_bullet.x = _shape.x + speed ;
_bullet.y = _shape.y;
}
}
function removeMovieClip(e:MouseEvent):void
{
removeChild(getChildByName(e.currentTarget.name));
// how to stop the timerEvent of clicked circle?
}
谢谢你的回答,但这对我不起作用。当我运行代码时,removeChilde.target给出错误1118:将静态类型对象的值隐式强制到可能不相关的类型flash.display:DisplayObject。第二,所有的子弹都从最后一圈开始。当我试图从每一个圆圈开始的时候。看起来计时器正在获取最后一个形状的值,并在同一个形状中放置4个子弹。但是移除计时器按照我想要的方式工作。现在唯一要做的就是让子弹从父形状开始。谢谢你,雷克斯还有一件事我不明白。如果没有计时器的新变量_shape.timer=new TimerMath.random*100,代码是如何工作的;当我按原样运行它时,您设置的程序可以正常工作。当我在代码中应用相同的概念时,其中_shape是movieClip的一个实例。movieClip是一个类,它上面的图像是通过xml添加的,并且它是从另一个带有自动动画方法的movieClip扩展而来的。它现在应该可以工作了。对于错误,您需要显式地将事件目标对象强制转换为MovieClip。第二条评论,我没有得到太多。也许你需要研究一些设计模式,因为这是一种丑陋/混乱的代码。根据您的需求,尝试以简洁的方式构建解决方案。干杯:
//var _timer:Timer = new Timer(1000); //I don't think this is necessary
var speed:int = 20;
for(var i:int=0; i<= 3; i++)
{
var _shape:MovieClip = new MovieClip();
_shape.graphics.beginFill(0x999999);
_shape.graphics.lineStyle(1, 0x000000);
_shape.graphics.drawCircle(0, 0, 20);
_shape.graphics.endFill();
addChild(_shape);
_shape.x = (stage.stageWidth)/5;
_shape.y = (stage.stageHeight)/3 + _shape.height*i*1.5;
_shape.name = "_shape"+i;
_shape.buttonMode = true;
bullets(_shape);
}
function bullets(_shape:MovieClip):void
{
_shape.timer = new Timer(Math.random()*100);
_shape.timer.addEventListener(TimerEvent.TIMER, startFiring);
_shape.timer.start();
_shape.addEventListener(MouseEvent.CLICK, removeMovieClip);
function startFiring(e:TimerEvent):void
{
speed ++;
var _bullet:MovieClip = new MovieClip();
_bullet.graphics.beginFill(0x999999);
_bullet.graphics.lineStyle(1, 0x000000);
_bullet.graphics.drawRect(0, 0, 5,2);
_bullet.graphics.endFill();
addChild(_bullet);
_bullet.x = _shape.x + speed ;
_bullet.y = _shape.y;
}
function removeMovieClip(e:MouseEvent):void
{
//stop the time and remove its listeners
e.target.timer.stop(); // you might need to cast this into Timer object
e.target.timer.removeEventListener(TimerEvent.TIMER, startFiring);
removeChild((MovieClip)e.target);
// how to stop the timerEvent of clicked circle?
}
}