• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 428
  • Last Modified:

Using the event listeners

I have been using the even listeners to call functions, but is is possible to use the second parameter to do something like this.

alphaTween.addEventListener(TweenEvent.MOTION_END, gotoAndPlay('moveIt'));

or does it have to be a call to a function.

Second part of the question

If it is a function, can I send a variable with it. Examples would be appreciated.


1 Solution
No, unfortunately that is impossible. You may however set up a variable and access that from the function like this:

var gotoTarget :String = "moveIt";

alphaTween.addEventListener(TweenEvent.MOTION_END, gotoVariable);
function gotoVariable(e :TweenEvent) :void
Sorry SamuelRostol, but your'e wrong :) and you might be glad to know that!
Actually technically you're correct, but there is a very nice trick to "pass arguments" through event listeners ...

Instead of:
alphaTween.addEventListener(TweenEvent.MOTION_END, gotoAndPlay('moveIt') );
try this:
alphaTween.addEventListener(TweenEvent.MOTION_END, function (e:Event) { gotoAndPlay('moveIt') } );

I've attached the above in a code snippet to make it clearer.

What you're doing when adding an event listener is defining which type of event you're interested in, and which FUNCTION to call on that event. When triggered, an Event object (or subclass) will be passed to the function so you can handle it appropriately.

What we've done here is defined an ANONYMOUS function (which accepts that Event object, but does nothing with it). The anonymous function then simply calls the desired function, with whatever arguments you like.

Can be very handy. I think you can even get away with dropping the argument list from the anon function, making it even clearer.

Hope it's not too late to be of some use roscoeh23.

// Define an anonymouse function as a handler, which calls our desired "gotoAndPlay"
	function( e:Event )
// Same thing - a little more compact, foregoing the argument list in the anonymous function
	function() { gotoAndPlay('moveIt'); }

Open in new window


Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now