View Full Version : Passing varibles into a function...
gotlisch
October 11th, 2009, 02:36 PM
Hi there, I'm trying to achive the following, but it doesn't work...what am I doing wrong?
myMovieClip.myButton.addEventListener(MouseEvent.C LICK, jumpToSection('contact'));
function jumpToSection(sectionName:String):void{
myMovieClip.mySectionsMovieClip.gotoAndPlay(sectio nName);
}
Seem like a logical approach to me but then again I'm not really a flash developer :-(
Cheers,
Mikael
IQAndreas
October 11th, 2009, 03:14 PM
Sadly, it is not possible to say which variables you are allowed to pass into a function in an event listener, however, there are two ways around this.
The first is to define a new function directly in the constructor
myMovieClip.myButton.addEventListener(MouseEvent.C LICK, function(me:MouseEvent) {jumpToSection('contact');});
The second option is to put a property on "myMovieClip" which tells which part to jump to:
myMovieClip.myButton.gotoName = "contact";
myMovieClip.myButton.addEventListener(MouseEvent.C LICK, jumpToSection);
function jumpToSection(mouseEv:MouseEvent):void{
myMovieClip.mySectionsMovieClip.gotoAndPlay(mouseE v.currentTarget.gotoName);
}
There is also a third option, since not all items allow you to add properties to them like that:
(My handy, dandy, copy and paste list)
The Dictionary Class (Passing in custom variables to Events)
Personally, I really like using it since it allows you to pass in other variables to events such as telling which URLs go to which buttons, so you can use one function for the onClick event.
http://www.kirupa.com/forum/showthread.php?p=2484903
http://www.kirupa.com/forum/showthread.php?t=331118
http://www.kirupa.com/forum/showthread.php?p=2501990 - Some sample usage
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.