1

im working on a fps game. The game contains multiple enemy. But im calling the same movieclip as enemy. That movieclip dispatches fire event and reduce player life. But i need to know which movieclip is dispatching event. I've added enemy randomly, i need to know the enemy location of the enemy who just have fired. Heres some code which may help...

dispatchEvent(new Event('Enemy_Fired')); // this event is dispatching from enemy movieclip

this.addEventListener('Enemy_Fired',ReduceLife);


    public function ReduceLife(e:Event){
        life--;
        var currentLife:int = life * lifeRatio;
        if(life<1){
            Game_Over();
            //game_completed();
        } else {
            sview.playerhit.gotoAndPlay(2);
            sview.lifebar.gotoAndStop(100 - currentLife);
            sview.health.text = String(currentLife);
        }
//Here i need to know this event dispatched from which enemy
    }

Thanks in advance

1 Answers1

4

You can get a reference to the object that dispatched the event by using:

e.target

It seems like the parent is dispatching the Event though as seen in this line of code.

dispatchEvent(new Event('Enemy_Fired')); // this event is dispatching from enemy movieclip

because dispatchEvent is the same as this.dispatchEvent, which means your root class is dispatching the event.

You need to change it to this yourEnemyMovieClip.dispatchEvent(new Event('ENEMY_FIRED',true,false);

Note that i am putting the bubbles property of your Event on true and cancelable on false. Bubbles means that your event will bubble up on the display chain. That is important , because you are listening for the event in your root class, which is one level higher than the movieclip that dispatched the event.

See the constructor of the Event class here: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/Event.html

in your eventlistener add the following

 e.stopImmediatePropagation();

this will stop the event from bubbling any higher up on the displaychain, saving performance in your application.

user1574041
  • 247
  • 4
  • 16
  • 1
    I checked that before, its showing my root class, where im catching the event, i need the movieclip where the event dispatching from... – soulfly pleasant Dec 26 '12 at 10:04