views:

25

answers:

1

Below is simplified version of a game I am creating. Basically, the game have some circles. Each circle shoot bullets that is dispatched with timerEvent. When the circle is clicked it is removed from the stage. However, the bullet still keeps on dispatching. I couldn't figure out how to stop timerEvent of each individual circle when it is clicked.

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?
}

thanks in advance Rexon

A: 
//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?
    }

}
bhups
Thank you the answers but it is not working for me. When I run the code the removeChild(e.target) gives error "1118: Implicit coercion of a value with static type Object to a possibly unrelated type flash.display:DisplayObject."and second all the bullets starts from the last circle. Where as I am trying to start the bullet form each individual circle. Looks like the timer is taking the value of the last shape and putting 4 bullets in same.But the remove timer works the way I wanted it. Now the only thing is to make the bullet start for the parent shape.Thank you,Rex
rex
There is another thing that I don't understand. How did the code worked without a new variable for the timer _shape.timer = new Timer(Math.random()*100);The one you set up works when I run it as it is but. When I apply the same concept in my code where _shape is a instance of movieClip. The movieClip is a class and image on it is added via xml also it is extended from another movieClip with carries auto animate method.
rex
It should work now. for the error you need to explicitly cast the event target object into MovieClip. THe second comment, I didn't get it much.
bhups
Probably you need to look into some design patterns, 'coz this is kind of ugly / confusing code. based on your requirements, try to architect the solution in a neat way. cheers :)
bhups