Flash AS3 addChild/分配运算符
下面的代码是我试图做的事情的简化版本。我的实际代码涉及将一个项目从一个数组复制到另一个数组(我能够做到)。然后在舞台上显示两个数组的内容Flash AS3 addChild/分配运算符,flash,actionscript-3,Flash,Actionscript 3,下面的代码是我试图做的事情的简化版本。我的实际代码涉及将一个项目从一个数组复制到另一个数组(我能够做到)。然后在舞台上显示两个数组的内容 //blueCircle is a library object with proper linkage set up. var ball = new blueCircle(); ball.x=100; ball.y=100; addChild(ball); //This is the line that is giving me trouble. var
//blueCircle is a library object with proper linkage set up.
var ball = new blueCircle();
ball.x=100;
ball.y=100;
addChild(ball);
//This is the line that is giving me trouble.
var anotherBall= ball;
anotherBall.x=200;
anotherBall.y=200;
addChild(anotherBall);
当我运行此代码时,舞台上只出现一个球(200200)
是否有另一种方法将一个值赋给另一个值,以便复制该值,而不只是添加指向原始变量的指针?如果没有,是否有方法复制球的实例并将其添加到另一个球的内存位置
我知道我可以打电话:
var anotherBall= new blueCircle();
但这对我正在编写的应用程序不起作用,因为我试图从中复制的数组的内容都是不同类型的对象。在ActionScript中,您根本无法复制对象。 除int、uint、Number、String、Boolean外,所有对象都通过引用赋值。 没有办法复制精灵。您只能创建新的精灵。 Sprite可能只有一个父级,如果您尝试将其添加到另一个DisplayObjectContainer,它将自动从旧的父级中删除。
因为Sprite扩展了EventDispatcher,所以您无法访问它的侦听器,也无法访问它。代码中的错误与编程中按引用或按值分配数据的区别有关。在ActionScript3中,每个变量都是指针,它们只存储对对象的引用。此外,非基元类型的赋值运算符仅复制引用(称为浅复制) ActionScript3在语法方面没有很好的解决方法,因此解决方案通常取决于程序的结构。也许你的BlueCircle类有一个克隆方法
public class BlueCircle extends MovieClip
{
public var someKindOfData:String;
public function BlueCircle()
{
someKindOfData = "something";
}
public function clone():BlueCircle
{
var clone:BlueCircle = new BlueCircle();
clone.x = x;
clone.y = y;
clone.someKindOfData = someKindOfData;
return clone;
}
}
或者,有一种(有点混乱的)方法来制作MovieClip的深度副本,这是详细的。您可以通过以下方式创建对象的新实例: Main.as(文档类):
package
{
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.utils.getDefinitionByName;
import flash.utils.getQualifiedClassName;
import Circle;
import Square;
public class Main extends Sprite
{
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}// end function
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
var shapes:Vector.<Shape> = Vector.<Shape>([new Circle(),
new Square()]);
var shape:Shape = shapes[0];
addChild(shape);
var shapeClone:Shape = cloneShape(shape);
shapeClone.x = 200;
addChild(shapeClone);
}// end function
private function cloneShape(shape:Shape):Shape
{
var ShapeClass:Class = getClass(shape);
return new ShapeClass();
}// end function
private function getClass(object:Object):Class
{
return getDefinitionByName(getQualifiedClassName(object)) as Class;
}// end function
}// end class
}// end package
package
{
import flash.display.Shape;
public class Circle extends Shape
{
public function Circle()
{
graphics.lineStyle(1);
graphics.drawCircle(50, 50, 50);
graphics.endFill();
}// end function
}// end class
}// end package
package
{
import flash.display.Shape;
public class Square extends Shape
{
public function Square()
{
graphics.lineStyle(1);
graphics.drawRect(0,0,100, 100);
graphics.endFill();
}// end function
}// end class
}// end package
方形。如:
package
{
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.utils.getDefinitionByName;
import flash.utils.getQualifiedClassName;
import Circle;
import Square;
public class Main extends Sprite
{
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}// end function
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
var shapes:Vector.<Shape> = Vector.<Shape>([new Circle(),
new Square()]);
var shape:Shape = shapes[0];
addChild(shape);
var shapeClone:Shape = cloneShape(shape);
shapeClone.x = 200;
addChild(shapeClone);
}// end function
private function cloneShape(shape:Shape):Shape
{
var ShapeClass:Class = getClass(shape);
return new ShapeClass();
}// end function
private function getClass(object:Object):Class
{
return getDefinitionByName(getQualifiedClassName(object)) as Class;
}// end function
}// end class
}// end package
package
{
import flash.display.Shape;
public class Circle extends Shape
{
public function Circle()
{
graphics.lineStyle(1);
graphics.drawCircle(50, 50, 50);
graphics.endFill();
}// end function
}// end class
}// end package
package
{
import flash.display.Shape;
public class Square extends Shape
{
public function Square()
{
graphics.lineStyle(1);
graphics.drawRect(0,0,100, 100);
graphics.endFill();
}// end function
}// end class
}// end package
如果您的主要问题是不知道复制的对象类应该是什么,您可以这样做:
// an example of the object you would like to copy.
// assuming that you don't know what the class is...
var mc:MovieClip = new MovieClip();
// getQualifiedClassName will return the name of the object class
// as a String, if the object was a blueCircle, the String returned
// would then be "blueCircle", in this instance it will return
// flash.display::MovieClip
var className:String = getQualifiedClassName(mc );
// now that you have the name you can get the Class
// with the getDefinitionByName method
var objectClass:Object = getDefinitionByName( className );
// time to copy your object
var obj:Object = new objectClass();
// check it all here...
trace("the class name is ", className );
trace("the object is ", objectClass );
trace("the copy is ", obj );
实际上,您可以在“utility”类中创建一个静态函数,该函数将返回作为参数传递的任何对象的副本
//Let's say you have a MyUtils Class
public static function getObjectCopy( obj:* ):*
{
var className:String = getQualifiedClassName(obj );
var objClass:Object = getDefinitionByName( className );
return new objClass();
}
//Then you could do
var ball = new blueCircle();
ball.x=100;
ball.y=100;
addChild(ball);
//And as explained above , you don't need to know the object type
var anotherBall= MyUtils.getObjectCopy( ball );
anotherBall.x=200;
anotherBall.y=200;
addChild(anotherBall);
如果需要2个球对象,则需要使用“new”操作符2次。其他任何操作都只是将内存引用传递给其他变量,如示例中所示。