Skip to main content
Inspiring
September 8, 2014
Answered

AS3 - New movieclip instance

  • September 8, 2014
  • 1 reply
  • 945 views

I have 3 types of mc's. I would like to create a new instance of a random mc of one of those types. I wrote somenthing like this but I receive an error:

Error #1007: Instantiation attempted on a non-constructor.

i tried something like this:

var newMC:* = new match as match;

match is an array that contains the 3 types of mc's. match will be something like [object orange], so I want to create a new instance of orange mc.

How can I do that?

This topic has been closed for replies.
Correct answer Ned Murphy

var ClassRef:Class = Class(getDefinitionByName(match));    // where match is a String

var newMC:* = new ClassRef();

addChild(classInstance);

1 reply

Ned Murphy
Ned MurphyCorrect answer
Legend
September 8, 2014

var ClassRef:Class = Class(getDefinitionByName(match));    // where match is a String

var newMC:* = new ClassRef();

addChild(classInstance);

gibsyAuthor
Inspiring
September 9, 2014

I received an error, but I finally managed it. I don't know if it is the right approach, but it seems to work:

......

import flash.utils.getDefinitionByName;

import flash.utils.getQualifiedClassName;

......

var ClassRef: Class = Class(getDefinitionByName(getQualifiedClassName(match)));

var newMC:* = new ClassRef();

addChild(newMC);

gibsyAuthor
Inspiring
September 9, 2014

Now, I would like to use this in a "for" loop;

var classRef: Class;

for (var i:int = 0; i<10; i++)

{

     classRef = Class(getDefinitionByName(getQualifiedClassName(match)));

     var newPiece:* = new classRef();

}

I get this warning: duplicate variable definition.