Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AS3 - Clone an object

I have a game with a variety of ship types. My Ship class has a static array holding one of each of the types in it. Whenever I make a new Ship (other than when initializing this array), I want to make it a clone of one of the existing Ship objects in my prototype array.

1 - How can I run through all the properties in one Ship object and assign them to a second Ship object?

2 - How can I see if a property is an object or a basic type like String or int? Some of the objects in my Ship class need to be cloned, and some are just references that need to stay the same.

like image 348
Martin Avatar asked Dec 11 '11 04:12

Martin


2 Answers

One option, arguably the most agile, would be to define clone methods for each class that you need to clone, such as:

class Ship
{
    public var prop1:Number;
    public var otherClassInstance:OtherClass;
    public function clone():Ship
    {
        var result:Ship = new Ship();
        result.prop1 = this.prop1;
        result.otherClassInstance = this.otherClassInstance.clone()
    }
}

class OtherClass
{
    public var prop1:Number;
    public function clone():OtherClass
    {
        var result:OtherClass = new OtherClass();
        result.prop1 = this.prop1;
    }
}

Another option is to clone an object by using the ByteArray class like this example from the Adobe documentation:

function clone( source:Object ):* 
{ 
    var myBA:ByteArray = new ByteArray(); 
    myBA.writeObject( source ); 
    myBA.position = 0; 
    return( myBA.readObject() ); 
}

I've seen instances where this approach does not work for cloning instances of custom classes, specifically view classes like Sprites.

Another approach is to use describeType from the flash.utils package. With describeType you can iterate through the properties of an object.

Here is an example of using describeType to inspect the properties of an object that is a part of a utils lib I wrote.

As for checking the type of the property, you can use describeType or you can also use the is operator like this:

if( myObj is SomeClass )
{

}

if( myObj is OtherClass )
{

}
like image 126
JeremyFromEarth Avatar answered Oct 21 '22 10:10

JeremyFromEarth


To run through all the properties of one ship object and assign them to a second:

shipobj1:Ship = new Ship();
//set values for all shipobj1 properties

shipobj2:Ship = new Ship();

for (item in shipobj2)
    item = shipobj1[item];

Checking if a property value is an object you could use typeof. The limitation of this is that there are only 6 possible types returned: boolean, function, number, object, string, and xml. So for example if you need to know if a property is an array you can't really do that with typeof since that would actually return "object" since "array" isn't one of the 6 options, but if you're just concerned with identifying simple types like numbers and strings versus other stuff it should do the trick:

if(typeof item == "object")
  // do whatever with object
else if(typeof item == "string")
  // do whatever with string
//etc, etc.

EDIT: Replaced variable "var" with "item" since var is a reserved word.

like image 38
TheOx Avatar answered Oct 21 '22 09:10

TheOx