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

use string as instance name

Contributor ,
Apr 29, 2013 Apr 29, 2013

Copy link to clipboard

Copied

var persona:Array = new Array();

for (var i:int = 1; i <= 26; i++)

{

          if (i<10)

          {

                    persona.push("persona0"+i);

          }

          else

          {

                    persona.push("persona"+i);

          }

}

//

var personaclip = persona[4]; //persona04

var personainstance:MovieClip = new personaclip();

addChild(personainstance);

What I'm trying to do here is first create an array of all the 26 clips which are already in the library and set to export to actionscript, persona01 to persona26.

Then I pick number for as the clip to be used, which is persona04. I want to add that to the stage. So I first create the instance of the persona04 clip through personainstance and then add it through addChild.

But the personainstance variable is where I go wrong as I get an error #1007 trying to create an instance on an element which isn't a constructor. How can I make it clear that for the personainstance movieclip I would like to select the persona04 clip form the library? Guess I have to convert the string 'persona04' to the constructor name somehow.

TOPICS
ActionScript

Views

360

Translate

Translate

Report

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

correct answers 1 Correct answer

LEGEND , Apr 29, 2013 Apr 29, 2013

creating dynamic class instances using Strings
-------------------------------------------------------------

var ClassRef:Class = Class(getDefinitionByName("className"));
var classInstance:* = new ClassRef();
addChild(classInstance);

Votes

Translate

Translate
LEGEND ,
Apr 29, 2013 Apr 29, 2013

Copy link to clipboard

Copied

LATEST

creating dynamic class instances using Strings
-------------------------------------------------------------

var ClassRef:Class = Class(getDefinitionByName("className"));
var classInstance:* = new ClassRef();
addChild(classInstance);

Votes

Translate

Translate

Report

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