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

AS3 - New movieclip instance

Explorer ,
Sep 08, 2014 Sep 08, 2014

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?

TOPICS
ActionScript
789
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

correct answers 1 Correct answer

LEGEND , Sep 08, 2014 Sep 08, 2014

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

var newMC:* = new ClassRef();

addChild(classInstance);

Translate
LEGEND ,
Sep 08, 2014 Sep 08, 2014

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

var newMC:* = new ClassRef();

addChild(classInstance);

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
Explorer ,
Sep 09, 2014 Sep 09, 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);

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
Explorer ,
Sep 09, 2014 Sep 09, 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.

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
LEGEND ,
Sep 09, 2014 Sep 09, 2014

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.

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
Explorer ,
Sep 09, 2014 Sep 09, 2014

It seems that match appears more than 1 time.

Thanks a lot.

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
LEGEND ,
Sep 09, 2014 Sep 09, 2014

You're welcome.  You should mark correct and helpful responses.

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
Explorer ,
Sep 09, 2014 Sep 09, 2014

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);

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
LEGEND ,
Sep 09, 2014 Sep 09, 2014

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.

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
Explorer ,
Sep 09, 2014 Sep 09, 2014
LATEST

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)));

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