Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Dynamically create variable type using string value

Participant ,
Feb 13, 2013 Feb 13, 2013

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.

TOPICS
ActionScript
1.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 13, 2013 Feb 13, 2013

use:

function buttonClicked ($event:MouseEvent):void {

var C:Class=Class(getDefinitionByName($event.currentTarget.sType));

var instance:*=new C();

addChild(instance);

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 14, 2013 Feb 14, 2013

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 14, 2013 Feb 14, 2013

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 15, 2013 Feb 15, 2013

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 15, 2013 Feb 15, 2013

so, was there any problem with:

import TestClass;

var B:TestClass = new TestClass();

trace (B); //all ok.

var C:Class = getDefinitionByName("TestClass") as Class;

trace(C);

var C1:Class=Class(getDefinitionByName("TestClass"));

trace(C1);

and, again make sure S1 is not a simple button.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Feb 15, 2013 Feb 15, 2013

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 15, 2013 Feb 15, 2013

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Feb 15, 2013 Feb 15, 2013
LATEST

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines