PDA

View Full Version : Reusable function... instance names?


nektir
04-22-2008, 12:13 AM
I'm trying to create a reusable function with dynamic instance names.. I want to use a function that when you click on a button, it opens a pop up. But I have several buttons and several pop ups.

Here's the code I currently have:
private function openPopup(event:MouseEvent):void {
trace("Target Clicked: " + event.currentTarget.name);
this[event.currentTarget.name + "_mc"].visible = true;
this[event.currentTarget.name + "_mc"].gotoAndPlay("play");
this.stage.addChild(this[event.currentTarget.name + "_mc"]);

}

There are objects on the stage with a general instance name like "apple"... so the trace returns "apple".

Here's the non-reusable version of the code:
private function openPopup(event:MouseEvent):void {
trace("Target Clicked: " + event.currentTarget.name); //returns apple
apple_mc.visible = true;
apple_mc.gotoAndPlay("play");
this.stage.addChild(apple_mc);

}

How would I do this? I'm coming from AS2 world... so I'm not exactly sure...

RelaxGuy
04-22-2008, 01:47 AM
Try this:

private function openPopup(event:MouseEvent):void {
trace("Target Clicked: " + event.currentTarget.name); //returns apple
event.currentTarget.visible = true;
event.currentTarget.gotoAndPlay("play");
this.stage.addChild(event.currentTarget); }

nektir
04-22-2008, 04:12 PM
That gives me a couple errors,

first:

TypeError: Error #1006: gotoAndPlay is not a function.

for this line of code:
event.currentTarget.gotoAndPlay("play");

And then... this error:

1118: Implicit coercion of a value with static type Object to a possibly unrelated type flash.display:DisplayObject.

for this line of code:
this.stage.addChild(event.currentTarget);

Any other ideas?

nektir
04-23-2008, 05:24 PM
The reason code like

event.currentTarget.visible = true;

will not work is because the currentTarget is the button... and I want to use the name of the button "apple" to be evaluated as the name of the movie clip "apple_mc" to make the function more reusable... that's why I tried

this[event.currentTarget.name + "_mc"].visible = true;

but it gives me errors...