Helpful stuff on Flash, ActionScript, After Effects etc

cases, code, tips and guidance

ActionScript 3.0: taking advantage of the event path when using addEventListener

without comments

lesson:
ok, I just learned this by Nutrox:
Mouse events in AS3 go through a capture, target and bubble phase. When a mouse event is triggered it is dispatched by the stage and drills down through the display list (capture phase) until it reaches the mouse target (target phase), it will then bubble up the display list back to the stage (bubble phase).

take a look at this taking advantage of the event path of mouse events in ActionScript 3.0

the old way of ActionScripting my way through a project
for an example like the one above, I would normally add an event listener for each of the 5 squares:

benched1.addEventListener(MouseEvent.CLICK, getThatBenchedColor, false, 0, true);
benched2.addEventListener(MouseEvent.CLICK, getThatBenchedColor, false, 0, true);
benched3.addEventListener(MouseEvent.CLICK, getThatBenchedColor, false, 0, true);
benched4.addEventListener(MouseEvent.CLICK, getThatBenchedColor, false, 0, true);
benched5.addEventListener(MouseEvent.CLICK, getThatBenchedColor, false, 0, true);

the new way of ActionScripting my way through a project
but with the new knowledge, I now collect all of my squares in a MovieClip, and simply assign the event listener to the new parent MovieClip:

benchedAll.addEventListener(MouseEvent.CLICK, getThatBenchedColor, false, 0, true);

this works just as well, nice, right?

the entire ActionScript:

benchedAll.addEventListener(MouseEvent.CLICK, getThatBenchedColor, false, 0, true);

benchedAll.benched1.buttonMode = true;
benchedAll.benched2.buttonMode = true;
benchedAll.benched3.buttonMode = true;
benchedAll.benched4.buttonMode = true;
benchedAll.benched5.buttonMode = true;

benchedAll.benched1._color = "C21201";
benchedAll.benched2._color = "7D1C18";
benchedAll.benched3._color = "33231B";
benchedAll.benched4._color = "094239";
benchedAll.benched5._color = "AD9742";

function getThatBenchedColor(e:MouseEvent):void {
          benchedtext.text = "e.target._color = "+e.target._color+" & e.target.name = "+e.target.name;
}


credits:

Nutrox, for all the patience :O)
the benched theme from Kuler

Bookmark and Share

Written by admin

januar 14th, 2010 at 11:30 pm

Leave a Reply