Copy link to clipboard
Copied
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?
var ClassRef:Class = Class(getDefinitionByName(match)); // where match is a String
var newMC:* = new ClassRef();
addChild(classInstance);
Copy link to clipboard
Copied
var ClassRef:Class = Class(getDefinitionByName(match)); // where match is a String
var newMC:* = new ClassRef();
addChild(classInstance);
Copy link to clipboard
Copied
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);
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Look for where you have duplicate variable definitions ( var whatever...) If you have multiple loops defined that declare var i then that is enough to trip that error message.
Copy link to clipboard
Copied
It seems that match appears more than 1 time.
Thanks a lot.
Copy link to clipboard
Copied
You're welcome. You should mark correct and helpful responses.
Copy link to clipboard
Copied
I always marked the correct answers when the things worked well.
I marked this answer now because this one worked for me:
......
import flash.utils.getDefinitionByName;
import flash.utils.getQualifiedClassName;
......
var ClassRef: Class = Class(getDefinitionByName(getQualifiedClassName(match)));
var newMC:* = new ClassRef();
addChild(newMC);
Copy link to clipboard
Copied
That is the answer I provided. You do not need the import lines. If that is how you thank people for helping you then don't be surprised if you get less help.
Copy link to clipboard
Copied
Ok. I don't want to argue with you about that because you helped me a lot. I'll check your answer as correct.
But I've changed this line:
var ClassRef:Class = Class(getDefinitionByName(match));
with this line to work for me:
var ClassRef: Class = Class(getDefinitionByName(getQualifiedClassName(match)));
Find more inspiration, events, and resources on the new Adobe Community
Explore Now