【问题标题】:Handling Multiple TimerEvent处理多个 TimerEvent
【发布时间】:2010-07-18 06:54:06
【问题描述】:

以下是我正在创建的游戏的简化版本。基本上,游戏有一些圈子。每个圆圈发射由 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();
    _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?
}

【问题讨论】:

    标签: actionscript-3 event-handling


    【解决方案1】:
    //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?
        }
    
    }
    

    【讨论】:

    • 谢谢你的答案,但它对我不起作用。当我运行代码时,removeChild(e.target) 给出错误“1118:将静态类型 Object 的值隐式强制转换为可能不相关的类型 flash.display:DisplayObject。”其次,所有子弹都从最后一个圆圈开始。当我试图从每个单独的圆圈开始子弹时。看起来计时器正在获取最后一个形状的值并将 4 颗子弹放入其中。但是删除计时器按我想要的方式工作。现在唯一要做的就是让子弹开始为父形状。谢谢你,雷克斯
    • 还有一点我不明白。如果没有计时器的新变量,代码是如何工作的 _shape.timer = new Timer(Math.random()*100);当我按原样运行它时,您设置的那个可以工作。当我在代码中应用相同的概念时,_shape 是movieClip 的一个实例。 movieClip 是一个类,其上的图像是通过 xml 添加的,它也是从另一个带有自动动画方法的movieClip 扩展而来的。
    • 它现在应该可以工作了。对于需要将事件目标对象显式转换为 MovieClip 的错误。第二条评论,我没听懂。
    • 可能您需要研究一些设计模式,因为这是一种丑陋/令人困惑的代码。根据您的要求,尝试以简洁的方式构建解决方案。干杯:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-03
    相关资源
    最近更新 更多