AS3自定义对象为ByteArray中再自定义对象 [英] AS3 Custom Object to ByteArray then to Custom Object

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

问题描述

有问题读取字节组自定义对象。任何帮助AP preciated

 公共类CustomObject扩展对象{
公共职能CustomObject(){
公共变种_x:数= 100
公共变种_y:数= 10
公共变种_z:数= 60
}
}

VAR cObj:CustomObject =新CustomObject()
VAR字节:的ByteArray =新的ByteArray()
bytes.writeObject(cObj)
bytes.com preSS()

//读
尝试{bytes.uncom preSS()}赶上(五:错误){}
VAR OBJ:CustomObject = bytes.readObject()作为CustomObject

跟踪(OBJ)//空,为什么?
跟踪(obj._z)//很明显 - 类型错误:错误#1009:无法访问空对象引用的属性或方法。
 

解决方案

你想要做的是使用registerClassAlias​​方法与数据一起注册类型的信息。这样,Flash将会知道如何序列化/反序列化对象。下面是Adobe的<一一些示例code href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/package.html#registerClassAlias%28%29">documentation:

  registerClassAlias​​(com.example.eg,例如ExampleClass);
VAR EG1:例如ExampleClass =新ExampleClass中();
变种BA:的ByteArray =新的ByteArray();
ba.writeObject(例1);
ba.position = 0;
变种EG2:* = ba.readObject();
跟踪(EG2为ExampleClass中); // 真正
 

有应注意的是的所有的类型应序列必须注册类型信息进行保存。所以,如果你有一个由你的类型引用的另一种类型,它也必须进行注册。

Having problem reading bytearray of custom objects. Any help is appreciated

public class CustomObject extends Object {
public function CustomObject() {
public var _x:Number =  100
public var _y:Number = 10
public var _z:Number  = 60
}
}

var cObj:CustomObject = new CustomObject()
var bytes:ByteArray = new ByteArray()
bytes.writeObject(cObj)
bytes.compress()

//read
try { bytes.uncompress() } catch (e:Error) { }
var obj:CustomObject = bytes.readObject() as CustomObject

trace(obj) // null why?!
trace(obj._z) // Obviously - TypeError: Error #1009: Cannot access a property or method of a null object reference. 

解决方案

What you want to do is use the registerClassAlias method to register type information along with the data. That way Flash will know how to serialize/deserialize your object. Here's some sample code from Adobe's documentation:

registerClassAlias("com.example.eg", ExampleClass);
var eg1:ExampleClass = new ExampleClass();
var ba:ByteArray = new ByteArray();
ba.writeObject(eg1);
ba.position = 0;
var eg2:* = ba.readObject();
trace(eg2 is ExampleClass); // true

It should be noted that all types that should be serialized must be registered for the type information to be saved. So if you have another type that is referenced by your type, it too must be registered.

这篇关于AS3自定义对象为ByteArray中再自定义对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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