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.

Thanks,

R.
roscoeh23Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SamuelRostolCommented:
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
{
  gotoAndPlay(gotoTarget);
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GlauronCommented:
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"
alphaTween.addEventListener(
	TweenEvent.MOTION_END,
	function( e:Event )
	{
		gotoAndPlay('moveIt');
	}
);
 
// Same thing - a little more compact, foregoing the argument list in the anonymous function
alphaTween.addEventListener(
	TweenEvent.MOTION_END,
	function() { gotoAndPlay('moveIt'); }
);

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.