PDA

View Full Version : Creating an instance of what a variable evaluates as?



Blommestein
June 1st, 2007, 05:52 PM
trace(t[i*3]); //Prints 'a1' (without quotes)
var d = new a1(); //Works fine, creates a new instance of class a1
var d = new t[i*3](); // Does not work

But how could I make that work?

Thanks in advance. :)

Krilnon
June 1st, 2007, 06:09 PM
You could use flash.utils.getDefinitionByName. If a1 is in the default package, then just 'a1' would work as an argument. Otherwise, you would need to have an instance of the class already or know the fully qualified name some other way.

Blommestein
June 1st, 2007, 06:21 PM
a1 is just a generated class of a movieclip in the library, no external as files are involved in this case.

I just tried using getDefinitionByName, but I get Cannot call method global/flash.utils::getDefinitionByName() as constructor.

>>you would need to have an instance of the class already

Why? That doesn't make sense to me.

>>know the fully qualified name some other way.

Not quite sure what you mean by this.

Thanks though for the help.
______________________________________

Nevermind, doing:

var e = getDefinitionByName(t[i*3])
var d = new e();

works fine. Thanks!

FizixMan
June 1st, 2007, 06:42 PM
Might have to resort to:

var myobj:Object = null;

if (t[i*3] == "a1")
{
myobj = new a1();
}
else if (t[i*3] == "a2")
{
myobj = new a2();
}
else if (t[i*3] == "b1")
{
myobj = new b1();
}



I don't know anything about AS3's reflection routines, so this is all I can suggest for now.
:-/

Krilnon
June 1st, 2007, 06:42 PM
>>you would need to have an instance of the class already

Why? That doesn't make sense to me.

>>know the fully qualified name some other way.

Not quite sure what you mean by this.Well, if you only knew the name of the class (a1, for example), but the fullyqualified name was com.example.a1, then just knowing 'a1' wouldn't be enough, unless you already had a string containing the package name, or another instance of the class that you wanted to instantiate again (then you could have used flash.utils.getQualifiedClassName to find the fully qualified name of the class from the instance).

FizixMan: It looks like Blommestein got it, so everything should be okay.

Blommestein
June 1st, 2007, 08:48 PM
Yes thanks, Krilnon's method works great. :)

However I have a similar problem.

for(var i:uint = 0; i<1000; i++)
{
var "mc"+i = new MovieClip();
}

This gives a syntax error, but how would I do what I'm trying to do? That being, create movieclips mc1,mc2,mc3, etc.

Thanks for any help.

TheCanadian
June 2nd, 2007, 12:13 AM
This should hopefuly help:
http://www.kirupa.com/forum/showpost.php?p=2130411&postcount=5

Blommestein
June 2nd, 2007, 12:29 AM
Yes that was extremely helpful as it answered my next question as well. :D

Thanks!

TheCanadian
June 2nd, 2007, 12:52 AM
You're welcome :hoser: