ActionScript - 传递&实例化包含必需参数的类引用?

时间:2011-09-01 16:23:38

标签: actionscript-3 class parameters reference instantiation

我尝试实例化作为参数传递给另一个类的类的引用时失败。

在这个例子中有3个类: MainClass,Canvas,MyCircle

来自MainClass的

我正在创建一个Canvas实例,它传递了MyCircle的类引用,因为我想从Canvas中创建MyCircle的实例。但是,MyCircle构造函数包含从Canvas中创建的必需参数。

如何使用必需参数传递和实例化类引用?

MyCircle:

package
{
    //Imports
    import flash.display.Shape;

    //Class
    public class MyCircle extends Shape
    {
        //Constructor
        public function MyCircle(color:uint, radius:uint)
        {
            graphics.beginFill(color, 1.0);
            graphics.drawCircle(0, 0, radius);
            graphics.endFill();
        }
    }
}

帆布:

package
{
    //Imports
    import flash.display.DisplayObject;
    import flash.display.Sprite;
    import flash.utils.getDefinitionByName;

    //Class
    public class Canvas extends Sprite
    {
        //Constructor
        public function Canvas(circleClassReference:String, amount:uint)
        {
            var CircleReference:Class = getDefinitionByName(circleClassReference) as Class;

            for (var i:uint = 0; i < amount; i++)
            {
                var randomColor:uint = Math.random() * 0xFFFFFF;
                var randomRadius:uint = Math.random() * 100 + 50;

                var circleInstance:DisplayObject = new CircleReference(randomColor, randomRadius);
                circleInstance.y = i * randomRadius;

                addChild(circleInstance);   
            }
        }
    }   
}

MainClass:

package
{
    //Imports
    import flash.display.Sprite;

    //Class
    public class MainClass extends Sprite
    {
        //Constructor
        public function MainClass()
        {
            var myCanvas:Canvas = new Canvas("MyCircle", 10);
            addChild(myCanvas)
        }
    }
} 

[编辑]


似乎传递实际的Class而不是类的名称可以避免需要下面提到的脏修复。

MainClass构造函数

public function MainClass()
{   
    var myCanvas:Canvas = new Canvas(MyCircle, 10);
    addChild(myCanvas)
}

Canvas构造函数

public function Canvas(circleClassReference:Class, amount:uint)
{
    var CircleReference:Class = circleClassReference;

    for (var i:uint = 0; i < amount; i++)
    {
        var randomColor:uint = Math.random() * 0xFFFFFF;
        var randomRadius:uint = Math.random() * 100 + 50;

        var circleInstance:DisplayObject = new CircleReference(randomColor, randomRadius);
        circleInstance.y = i * randomRadius;

        addChild(circleInstance);   
    }
}

2 个答案:

答案 0 :(得分:3)

导入(或位于同一个包中)不足以使编译器在Application Domain中包含类定义。您实际上必须使用您想要的类,或者将该定义嵌入到您加载的SWF中。这应该可以解决您的错误:

package
{
    //Imports
    import flash.display.Sprite;

    //Class
    public class MainClass extends Sprite
    {
        //Constructor
        public function MainClass()
        {
            //unused variable definition for class embedding purposes
            var tempCircle:MyCircle;
            var myCanvas:Canvas = new Canvas("MyCircle", 10);
            addChild(myCanvas)
        }
    }
}

虽然这会阻止MyCircle在以后被定义,但我一直认为这是一个肮脏的修复(尽管在一些相当重要的项目中有它)。除了将定义嵌入单独的SWF中之外,我很想知道更好的解决方案。

答案 1 :(得分:1)

您是否知道可以将类视为对象?

根据您的尝试,您可以执行以下操作:

我的圈子:

package
{
    //Imports
    import flash.display.Shape;

    //Class
    public class MyCircle extends Shape
    {
        //Constructor
        public function MyCircle(color:uint, radius:uint);
        {
            graphics.beginFill(color, 1.0);
            graphics.drawCircle(0, 0, radius);
            graphics.endFill();
        }
    }
}

画布:

package
{
    //Imports
    import flash.display.DisplayObject;
    import flash.display.Sprite;
    import flash.utils.getDefinitionByName;

    //Class
    public class Canvas extends Sprite
    {
        //Constructor
        public function Canvas(classToCreate:Class, amount:uint)
        {
            for (var i:uint = 0; i < amount; i++)
            {
                var randomColor:uint = Math.random() * 0xFFFFFF;
                var randomRadius:uint = Math.random() * 100 + 50;

                var circleInstance:DisplayObject = new classToCreate(randomColor, randomRadius);
                circleInstance.y = i * randomRadius;

                addChild(circleInstance);   
            }
        }
    }   
}

MainClass:

package
{
    //Imports
    import flash.display.Sprite;

    //Class
    public class MainClass extends Sprite
    {
        //Constructor
        public function MainClass()
        {
            var myCanvas:Canvas = new Canvas(MyCircle, 10);
            addChild(myCanvas)
        }
    }
}