2011-12-19 87 views
1

代碼波紋管是我正在嘗試做的簡化版本。我的實際代碼涉及將一個項目從一個陣列複製到另一個陣列(我可以做到這一點)。然後在舞臺上顯示兩個陣列的內容。Flash AS3 addChild /賦值運算符

//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(); 

但這不是我寫,因爲數組的內容,我試圖複製從都是不同類型的對象的應用程序。

+2

如果你想2球OBJ那麼你需要使用「新」操作符2次。其他任何東西都只是將內存引用傳遞給其他變量,如在您的示例中。 – 2011-12-19 23:55:15

回答

0

在ActionScript中你根本不能複製對象。 所有對象按引用分配,但int,uint,Number,String,Boolean除外。 無法複製Sprite。你只能創建新的精靈。 Sprite可能只有一個父級,如果嘗試將其添加到另一個DisplayObjectContainer,它會自動從舊父級中刪除。 由於Sprite擴展了EventDispatcher,因此您無法訪問其偵聽器,並且無法訪問它。

3

與您的代碼錯誤已通過參考與分配數據的編程區別做。在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; 
    } 
} 

另外,還有一個(一種雜亂)的方式,使電影剪輯,這是詳細here的深層副本。

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 

正方形。如:

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 
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..." 

如果你的主要問題是不知道複製的對象類應該是什麼,你可以做這樣的事情:

// 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);