Copy link to clipboard
Copied
Hi. In my project I created several classes named Square1, Square2, Circle 1, Circle2, Triangle1, Triangle2, etc. They all extend MovieClip and the create different shapes and have slightly different properties that set them appart.
There comes a point when I need to instance them and add the instances to the stage. I do this through buttons that have a sType:String property, that corresponds to the name of the class they should instantiate. So button S1 has sType = "Square1", and so on.
Now, what I would like to do is, instead of having a ton of handler functions for all the buttons, have only a single function that handles all the buttons and decides which class to instantiate. It would be something like this:
function buttonClicked ($event:MouseEvent):void {
var instanceClass:String = $event.target.sType; //let's say it's "Square1"
var newShape:MovieClip = new [instanceClass]();
}
Is this possible?
Thank you.
Copy link to clipboard
Copied
use:
function buttonClicked ($event:MouseEvent):void {
var C:Class=Class(getDefinitionByName($event.currentTarget.sType));
var instance:*=new C();
addChild(instance);
}
Copy link to clipboard
Copied
Thank you.
However, when I used getDefinitionByName I get "error #1065 variable is not defined". I also tried something I saw in another forum:
var C:Class = getDefinitionByName($event.currentTarget.sType) as Class;
but I got the same error. Reading my initial post I see I forgot to mention that Square1, etc. are linked to library items, but I don't think this makes a difference. I tried changing sType to a class that is not linked and I got the error again.
Copy link to clipboard
Copied
use:
trace($event.currentTarget.sType) to see which sType variable is not defined.
p.s. you did mean to say you were using movieclips, not buttons?? ie, S1 is not a simple button but a movieclip button, correct?
Copy link to clipboard
Copied
I used
import TestClass;
var B:TestClass = new TestClass();
trace (B); //all ok.
var C:Class = getDefinitionByName("TestClass") as Class;
and
var C:Class=Class(getDefinitionByName("TestClass"));
where TestClass is a class I created that is not linked to anything.
Thank you.
Copy link to clipboard
Copied
so, was there any problem with:
import TestClass;
var B:TestClass = new TestClass();
trace (B); //all ok.
trace(C);var C:Class = getDefinitionByName("TestClass") as Class;
trace(C1);var C1:Class=Class(getDefinitionByName("TestClass"));
and, again make sure S1 is not a simple button.
Copy link to clipboard
Copied
Try doing something like this--it will be very clear to read and make sure all the Classes you need are compiled in:
//make sure you use ctrl-space when you type the value of each property so the Class will get compiled in
protected var classes:Object = [Square1:Square1, Square2:Square2, Circle1:Circle1, Circle2:Circle2];
//we're not writing php, $ only belongs in variable names very deep in the Flex framework!
protected function buttonClicked(e:Event):void {
//note that only Class names should begin with upper case!
var c:Class = classes[(e.curentTarget as YourCustomButtonType).sType] as Class;
//do what you need to with the Class
}
The error "Property sType not defined on IEventDispatcher" that you report above is because sType isn't a property of IEventDispatcher. So, with strict typing on it will assume the property doesn't exist unless you cast .currentTarget to your custom button Class that does have that property.
Copy link to clipboard
Copied
Thank you, I'll look into this.
Also, I use $ on variables that are arguments on function definitions. I read somewhere this was a good and common way to distinguish them from other variables used in functions. If this might be a problem when using Flex, then I'll find another way to distinguish them.
Copy link to clipboard
Copied
These are what most people consider to be the definitive ActionScript coding standards (migrated from just being the Adobe coding standards). I'd be interested to see where you read that, since I've been doing AS3 for 6 years and have never seen it anywhere.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now