PDA

View Full Version : Dynamically Naming Functions



louissi
August 31st, 2009, 10:29 PM
public function btnU1Out_Handler (event:Event ):void {btnUOut_Handler(1);}
public function btnU2Out_Handler (event:Event ):void {btnUOut_Handler(2);}
public function btnU3Out_Handler (event:Event ):void {btnUOut_Handler(3);}
public function btnU4Out_Handler (event:Event ):void {btnUOut_Handler(4);}
public function btnU5Out_Handler (event:Event ):void {btnUOut_Handler(5);}
public function btnU6Out_Handler (event:Event ):void {btnUOut_Handler(6);}
public function btnU7Out_Handler (event:Event ):void {btnUOut_Handler(7);}
public function btnU8Out_Handler (event:Event ):void {btnUOut_Handler(8);}
public function btnU9Out_Handler (event:Event ):void {btnUOut_Handler(9);}
public function btnU10Out_Handler (event:Event ):void {btnUOut_Handler(10);}
public function btnU11Out_Handler (event:Event ):void {btnUOut_Handler(11);}
public function btnU12Out_Handler (event:Event ):void {btnUOut_Handler(12);}
public function btnU13Out_Handler (event:Event ):void {btnUOut_Handler(13);}
public function btnU14Out_Handler (event:Event ):void {btnUOut_Handler(14);}

There must be a way to dynamically generate these functions :S

lewi-p
September 1st, 2009, 04:56 AM
You can't create dynamically named functions on the fly. The best way would be to have one function that all the Buttons/MovieClips call then deciding what to do with it after that...


mybutton0.addEventListener(MouseEvent.CLICK, buttonPress);
mybutton1.addEventListener(MouseEvent.CLICK, buttonPress);
mybutton2.addEventListener(MouseEvent.CLICK, buttonPress);
mybutton3.addEventListener(MouseEvent.CLICK, buttonPress);
mybutton4.addEventListener(MouseEvent.CLICK, buttonPress);
mybutton5.addEventListener(MouseEvent.CLICK, buttonPress);

function buttonPress($event:Event):void
{
trace($event.target + " pressed");
}

lewi-p

IQAndreas
September 1st, 2009, 05:01 AM
lewi-p is right... but...

1. I guess you can use the "$" character before a variable, but I wouldn't recommend it.
2. Use "currentTarget" instead of "target" - http://mysticnomad.wordpress.com/2008/03/21/difference-between-eventtarget-and-eventcurrenttarget-properties-in-an-event-object/

mybutton0.addEventListener(MouseEvent.CLICK, buttonPress);
mybutton1.addEventListener(MouseEvent.CLICK, buttonPress);
mybutton2.addEventListener(MouseEvent.CLICK, buttonPress);
mybutton3.addEventListener(MouseEvent.CLICK, buttonPress);
mybutton4.addEventListener(MouseEvent.CLICK, buttonPress);
mybutton5.addEventListener(MouseEvent.CLICK, buttonPress);

function buttonPress(event:Event):void
{
trace(event.currentTarget + " pressed");
}

Also, if you want to attach properties to the buttons (such as target URL's etc) use the method I described in this post:
http://www.kirupa.com/forum/showthread.php?p=2484903#3

lewi-p
September 1st, 2009, 05:10 AM
I guess it's down to personal preference, I always put '$' before argument variable, helps me figure out further down the page where the variables came from without having to scroll up to the top and see.

public = myVar
private = _myVar
static = MYVAR
arguments = $myVar

lewi-p

IQAndreas
September 1st, 2009, 05:47 AM
I guess it's down to personal preference, I always put '$' before argument variable, helps me figure out further down the page where the variables came from without having to scroll up to the top and see.

public = myVar
private = _myVar
static = MYVAR
arguments = $myVar

lewi-p
Hm... Quite logical. As long as AS3 doesn't throw an error in your face, I apologize, and might consider using your method. :)

Just thought I'd post my method in a nutshell:
classes = MyVar
public = myVar
private = _myVar
constants = MYVAR
arguments = new_myVar (For getter/setter methods or constructors)