AS3 - 克隆一个对象 [英] AS3 - Clone an object

查看:35
本文介绍了AS3 - 克隆一个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一款包含多种船型的游戏.我的 Ship 类有一个静态数组,其中包含其中的每种类型之一.每当我创建一个新的 Ship(除了初始化这个数组时),我想让它成为原型数组中现有 Ship 对象之一的克隆.>

1 - 如何遍历一个 Ship 对象中的所有属性并将它们分配给第二个 Ship 对象?

2 - 如何查看属性是对象还是基本类型,例如 Stringint?我的 Ship 类中的一些对象需要克隆,而一些只是需要保持不变的引用.

解决方案

一种可以说是最灵活的选择是为您需要克隆的每个类定义克隆方法,例如:

class Ship{公共 var prop1:Number;公共 var otherClassInstance:OtherClass;公共函数 clone():Ship{var 结果:Ship = new Ship();result.prop1 = this.prop1;result.otherClassInstance = this.otherClassInstance.clone()}}班级其他班级{公共 var prop1:Number;公共函数 clone():OtherClass{var 结果:OtherClass = new OtherClass();result.prop1 = this.prop1;}}

另一种选择是使用 ByteArray 类来克隆一个对象,就像 Adob​​e 文档中的这个例子一样:

function clone( source:Object ):*{var myBA:ByteArray = new ByteArray();myBA.writeObject( 源代码);myBA.position = 0;返回( myBA.readObject() );}

我见过这种方法不适用于克隆自定义类的实例的实例,特别是像 Sprites 这样的视图类.

另一种方法是使用 flash.utils 包中的 describeType.使用 describeType,您可以遍历对象的属性.

以下是使用 describeType 检查作为 utils lib 我写的.

至于检查属性的类型,可以使用describeType,也可以像这样使用is操作符:

if( myObj 是 SomeClass ){}if( myObj 是 OtherClass ){}

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.

解决方案

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 )
{

}

这篇关于AS3 - 克隆一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆