ActionScript - Passing & Instantiating Class Reference Containing Required Parameters?
I'm unsuccessfully attempting to instantiate a reference of a class that is passed as a parameter to another class.
in this example there are 3 classes: MainClass, Canvas, MyCircle
from the MainClass i am creating an instance of Canvas, which is passed a class reference of MyCircle as i want to create instances of MyCircle from within Canvas. however, the MyCircle constructor contains required parameters that are created from within Canvas.
how can i pass and instantiate a class reference with required parameters?
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();
}
}
}
Canvas:
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)
}
}
}
[EDIT]
it seems that passing the actual Class instead of the name of the class avoids the need for the dirty fix that is mentioned below.
MainClass Constructor
public function MainClass()
{
var myCanvas:Canvas = new Canvas(MyCircle, 10);
addChild(myCanvas)
}
Canvas Constructor
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);
}
}
Importing (or being in the same package) isn't enough to make the compiler include a class definition in the Application Domain. You will actually have to use the class you want somewhere, or embed the definition in an SWF that you load in. This should fix your error though:
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)
}
}
}
While this will prevent MyCircle from being undefined later on, I've always thought of it as a bit of a dirty fix (despite having it in some fairly major projects). I'd love to know a better solution, other than embedding the definition in a separate SWF.
Are you aware that you can treat classes as Objects?
Depending on what you're trying to do you could do something like this:
My Circle:
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();
}
}
}
Canvas:
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)
}
}
}
精彩评论