views:

542

answers:

3

I've done this before, but i can't quite remember the syntax.

What i've got (simplified):

function createText(clazz:Class)
{
    var font:Font = new clazz(); //throws Instantiation attempted on a non-constructor.
}

I believe this can be done without using getQualifiedClassName, but its been a long time. Any help appreciated.

+1  A: 

how are you passing the class to the function ? the calling line should have trown an error in the first place if the wanted class wasn't available, this is weird.

can you post the real code ?

here a trick for loading a class compiled in an external swf

var clazz:Class = this.yourLoader.contentLoaderInfo.applicationDomain.getDefinition("yourClassName") as Class;
Benoit Jadinon
+2  A: 

You are probably passing null to the function.

package
{

import flash.display.Sprite;

public class ClassTest extends Sprite
{
    function ClassTest()
    {
     makeObject(Object);
     makeObject(Sprite);
     makeObject(null);
    }

    private function makeObject(type:Class):void
    {
     trace(typeof type);
     var obj:* = new type();
     trace(typeof obj);
     trace("");
    }
}

}

This outputs:

object
object

object
object

object
TypeError: Error #1007: Instantiation attempted on a non-constructor.
    at ClassTest/makeObject()
    at ClassTest()
James Fassett
thanks you helped me too!
matthy
A: 

Turns out I hadn't given the font a class name in CS3. So yes,I was passing null.

cellis