下面的代码是我想要做的简化版本。我的实际代码涉及将项目从一个数组复制到另一个数组(我能够做到)。然后在舞台上显示两个阵列的内容。
//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);
当我运行此代码时,舞台上只会出现1个球(200,200)。
是否有另一种方法可以将一个值分配给另一个值,以便将其复制而不是仅添加指向原始变量的指针?如果没有,是否有办法复制球的实例并将其添加到另一个球的记忆位置?
我知道我可以致电:
var anotherBall= new blueCircle();
但是这对我正在编写的应用程序不起作用,因为我试图从中复制的数组的内容都是不同类型的对象。
答案 0 :(得分:3)
代码中的错误与 reference 或 value 分配数据的编程有所区别。在ActionScript 3中,每个变量都是指针,它们只将引用存储到对象中。此外,非基本类型的赋值运算符仅复制引用(称为浅复制)。
var x:int = 0;
var y:int = x;
y += 1;
trace(x, y); // Output is "0 1" because int is a primitive datatype.
var objectA:Object = { name: "I'm object A." };
var objectB:Object = objectA; // This is just a shallow copy. objectA and objectB
// point to the same value.
objectB.name = "I'm object B.";
trace(objectA.name, objectB.name); // Output is "I'm object B. I'm object B."
ActionScript 3在语法方面没有很好的方法,所以解决方案通常在于程序的结构。也许您的 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的深层副本,详细here。
答案 1 :(得分:0)
在ActionScript中,您根本无法复制对象。 除int,uint,Number,String,Boolean外,所有对象都通过引用进行分配。 无法复制Sprite。你只能创建新的精灵。 Sprite可能只有一个父级,如果您尝试将其添加到另一个DisplayObjectContainer,它会自动从旧父级中删除。 因为Sprite扩展了EventDispatcher,所以你无法访问它的监听器,也无法访问它。
答案 2 :(得分:0)
您可以通过以下方式创建对象的新实例:
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
<强> Circle.as:强>
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
<强> Square.as:强>
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
答案 3 :(得分:0)
"but this won't work for the application I am writing because the contents of
the array I am trying to copy from are all different types of objects..."
如果你的主要问题是不知道复制的Object类应该是什么,你可以这样做:
// 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 );
您实际上可以在“实用程序”类中创建一个静态函数,它将返回您作为参数传递的任何对象的副本
//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);