Question

I have many buttons in a movie clip. Rather than creating listeners for each I just want to perform an action based on what target they clicked in the mc. I want to do change the alpha based on the button they click on. How can this be done?

I tried 4 options below and none of these work.

      manyButtons.addEventListener(MouseEvent.MOUSE_UP, mUp);

function mDown(e:Event)
        {
trace(e.target.name); // Works! Outputs name of button I click              
this[e.target.name].alpha = .5; // Does not work
e.target.name.alpha = .5; // Error: can not create property alpha on sting
e.target.alpha = .5; // changes ALL children buttons and parent mc.
        }
Was it helpful?

Solution

The e.target.name returns you the name of your button, as you've observed that already. But your button is not in "this". It is in this.manyButtons. You have to provide the full path. So the solution is:

this.manyButtons[e.target.name].alpha = .5;

...assuming that the buttons are children of the manyButtons display object.

Btw, a better approach would be simply this:

MovieClip(e.target).alpha = .5

EDIT: if you are coming from AS2 -> remember the scope does not change in listener anymore. In case you wrote something like this before, forget it:

this.manyButtons.onRelease = function() {
   trace(this); //this changed the scope to the manyButtons object!
}

this does not change anymore in as3. It always refers to the object it is declared in!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top