A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: addEventListener question

  1. #1
    absolutezero3424
    Join Date
    Nov 2006
    Posts
    508

    addEventListener question

    When you use addEventListener functions, is there any way to add parameters to the function that is called? for example:


    Code:
    some_mc.addEventListener(Event.ENTER_FRAME, onEnterFrame);
    
    function onEnterFrame(e:Event):void {
    
        //  some code...
    
    }

    is there any way to add parameters to the function "onEnterFrame" other than the event parameter? if so, how do you handle this? could you provide an example? Thx!

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I got this one, cancerinform.
    http://board.flashkit.com/board/show...hlight=passing

    We should put that on a t-shirt or something.

  3. #3
    Flash Developer
    Join Date
    Feb 2006
    Posts
    105
    some_mc.addEventListener(Event.ENTER_FRAME, onEnterFrame);
    some_mc.arg = some_arguement;
    function onEnterFrame(event:Event):void {
    trace(event.target.parent.arg);
    }

    // Hey man, first I thought this method would only work with dynamically created movieclips, but I tested it out and it works. I don't actually know why you have to add that parent in there, but it doesn't work without the parent. I haven't tried with events like onEnterFrame, but this works for button behavior.

  4. #4
    absolutezero3424
    Join Date
    Nov 2006
    Posts
    508
    I'm not surprised it works, but I am surprised you need the parent in THIS example. "event" specifies the "ENTER_FRAME" event in this case, "target" specifies who triggered the event, which would be "some_mc" in this case, and "parent" seems like it would specify the parent of "some_mc", which would depend on where "some_mc" lives and where it's being called from.

  5. #5
    Senior Member
    Join Date
    Oct 2003
    Posts
    1,354
    I believe you should be using 'currentTarget' with no parent.

    'currentTarget' returns the actual object that the listener is attached to.
    'target' returns a related object which is different depending on the type of listener. (hope I am explaining that right)

    Say you have a movieClip called 'box' with a MouseEvent.CLICK listener on it. And inside of that clip is another movieClip called 'circle'.
    If you click on the 'box' clip, 'currentTarget' will return the 'box' object and 'target' will return the 'box' object.
    If you click on the 'circle' clip inside the 'box' clip, 'currentTarget' will still return 'box' as that is where the listener is attached, and 'target' will return the 'circle' object, as that was what was actaully pressed on.

    Although in my playing with ENTER_FRAME, target and currentTarget are returning the same thing..

    Code:
    some_mc.addEventListener(Event.ENTER_FRAME, myonEnterFrame);
    some_mc.arg = "some_arguement";
    function myonEnterFrame(event:Event):void {
    trace(event.currentTarget.arg);//traces 'some_argument'
    trace(event.target.arg)//traces 'some_argument'
    trace(event.target.parent.arg)//trace 'undefined'
    }

  6. #6
    absolutezero3424
    Join Date
    Nov 2006
    Posts
    508
    cool, that'll be useful....thanks for the explanation!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center