Helpful stuff on Flash, ActionScript, After Effects etc

cases, code, tips and guidance

ActionScript 3.0: how to add property to eventListener

with 8 comments

sooner or later you’ll wish there was a way to add a property to an eventListener in ActionScript3.0.

here’s the description of the eventListener, directly from Help in Flash:

function eventResponse(eventObject:EventType):void
{
// Actions performed in response to the event go here.
}

eventSource.addEventListener(EventType.EVENT_NAME, eventResponse);

you let your eventSource (i. e. a button) await the EventType (i. e. a click) and assign an eventResponse (i. e. a function that changes the color of your button from yellow to blue)
here’s an example that illustrates the above:

example where you wish you could pass a property with the eventListener

by clicking the small boxes on the left, the big box changes color.
smart.

but the way this works annoys me a bit, heres the code:

//imports
import  caurina.transitions.Tweener;

blueBox.addEventListener(MouseEvent.CLICK, changeColor);
redBox.addEventListener(MouseEvent.CLICK, changeColor);
yellowBox.addEventListener(MouseEvent.CLICK, changeColor);

function changeColor( e:Event ):void{
   switch( e.target.name ){
      case "blueBox":
         trace( "blueBox" );
         Tweener.addTween(this.bigBox, {_color:0x0000FF, time:1, transition:"linear"});
         break;
      case "redBox":
         Tweener.addTween(this.bigBox, {_color:0xFF0000, time:1, transition:"linear"});
         break;
      case "yellowBox":
         Tweener.addTween(this.bigBox, {_color:0xFFFF00, time:1, transition:"linear"});
         break;
   }
}

you’ll have to use a switch to find out which box is clicked, and then assign a color to the big box..

but now I’ve finally found a way around this.
here’s the new code:

//imports
import caurina.transitions.Tweener;

blueBox.addEventListener(MouseEvent.CLICK, changeColor);
redBox.addEventListener(MouseEvent.CLICK, changeColor);
yellowBox.addEventListener(MouseEvent.CLICK, changeColor);
blueBox._color=  0x0000FF;
redBox._color=0xFF0000;
yellowBox._color=0xFFFF00;

function changeColor( e:Event ):void {
   Tweener.addTween(this.bigBox, {_color:e.target._color, time:1,  transition:"linear"});
}

each box is assigned a _color-property like this blueBox._color = 0×0000FF; …and this can later be used like this e.target._color

this works perfectly and makes the works quite a bit easier, I think :O)
edited on the 12th of december 2009: changed the word parameter to property

Bookmark and Share

Written by felisan

august 28th, 2008 at 9:14 am

8 Responses to 'ActionScript 3.0: how to add property to eventListener'

Subscribe to comments with RSS or TrackBack to 'ActionScript 3.0: how to add property to eventListener'.

  1. Thanks for the help, been looking every where for this answer. Ony one question I don’t undersatnd where you get “bigBox” from.
    //
    Tweener.addTween(this.bigBox, {_color:e.target._color, time:1, transition:”linear”});
    }

    Regards
    Mat

    mat

    23 jun 08 at 14:26

  2. hi Mat.

    glad you could use it – the bigBox is a movieclip from the library… alternatively you could just place a movieclip on stage and instancename it bigBox.

    easy as pie :O)

    felisan

    23 jun 08 at 18:06

  3. var boxes:Array = [blueBox, redBox, yellowBox, blackBox, whiteBox];
    var colors:Array = ["0x0000FF", "0xFF0000", "0xFFFF00", "0x000000", "0xFFFFFF"];

    for (var i:Number = 0; i<boxes.length; i++){
    boxes[i].addEventListener(MouseEvent.CLICK, changeColor);
    }
    function changeColor(e:MouseEvent):void{
    Tweener.addTween(this.bigBox, {color:colors[boxes.indexOf(e.currentTarget)], time:1, transition:”linear”});
    }

    //——–
    //i think this is the best way if there are more than three boxes and colors

    Damir Asanov

    2 aug 09 at 16:05

  4. Nice site. go to my favorites. TNx

    Zashkaser

    5 aug 09 at 18:38

  5. Assigning the desired parameter to the item itself seems to be very clever. (e.g. blueBox._color = 0×0000FF; …)

    This won’t work with buttons. I think there has to be a movieclip around the button and you will have to use event bubbling (?) to catch the CLICK event on the movieclip.

    Any other ideas?

    Thomas Link

    29 nov 09 at 15:33

  6. you can only add those type of parameters to MovieClips. neither buttons nor Sprites will accept parameters. you can extend the Sprite class to accept parameters though :)

    admin

    9 dec 09 at 16:16

  7. When you say “parameter,” do you really mean “property”?

    At any rate, doesn’t adding a property to an object without using “var” generate an error in AS 3?

    Tristan

    12 dec 09 at 02:44

  8. actually Tristan, you’re right, it’s a property, not a parameter.
    and therefore the post has been edited :)
    but besides from that, I’ve been passing on parame…. erhm, properties to objects like in the example above in lots and lots of files without getting into any trouble.

    please let me know if you get into any kind of problem using the code example above.
    thanks

    admin

    12 dec 09 at 21:32

Leave a Reply