Stop Propagating! Pt. 1 AS3, Event Handling, and Lil Timmy

I’ve periodically run into issues playing around with EventListeners where I want to remove one EL, and add another in the same function, something similar to the following code below.  First, a quick note on what’s going on in the code so we’re all on the same page.  I have a movie clip “defensive structure,” say, a tower for a Tower Defense game.  It has been added to the stage and I attached an EL to this movie clip of the tower when I added it to the stage, listening for a MOUSE_DOWN event on said tower.

Since then, I’ve added a few other movie clips INTO the tower Movie Clip.  The first issue I ran into was that normally I’ll use “e.target” as the “hey what did I click” variable to trace out or find out just what exactly was clicked.  e.target was pointing at a child of my tower movieclip, which gave me a number of errors like, “this function does not belong to this movie clip,” because functions belong to classes, and I’m targeting just a simple sprite or movie clip.

However, e.currentTarget returns the containing DisplayObjectContainer of e.target.  Usually e.currentTarget would trace out to be [object Stage], but since I specifically added e.target as a child of my defense movie clip, e.currentTarget is returning the actual object I need, the Defense object.

So, let’s take a look:

(more…)