Reputation: 2606
I have a TActor class and a function to_bytes() inside it that should compress it to a bytes array as in this example: http://jacksondunstan.com/articles/1642
public function to_bytes():ByteArray
{
registerClassAlias("TActor",TActor);
var bytes:ByteArray=new ByteArray();
bytes.writeObject(this as TActor);
bytes.position=0;
trace(bytes.readObject());
bytes.position=0;
trace(bytes.readObject() as TActor);
return bytes;
}
However, the first trace prints undefined and the second one null instead of [object TActor]. What do I do wrong?
Upvotes: 2
Views: 39
Reputation: 1714
It's important to note that the this
keyword returns the current instance of the object. What you are currently doing is attempting to pass the this
instance to writeObject
, which will only work if there is an instance of TActor
instantiated. So it would work in this scenario:
In some class where you instantiate TActor:
var tactor:TActor = new TActor();
tactor.to_bytes();
Then it should serialize correctly.
Also as we discovered in the comments, TActor
is of type MovieClip
, currently you cannot use writeObject()
on Objects of type MovieClip
. More specifically any object that is a dynamic class cannot be used in writeObject. Changing it to Sprite
solved this particular case.
Upvotes: 1