开发者

AS3: Approach to store MovieClips in a database

I have potentially an interesting situation. I'm developing a dynamic drawing tool using shapes and I need to be able to store the current "state" of the objects in a database.

The "shapes" will be simple geometric along with custom-drawn shapes so i can't just store the geometric footprints of them.

Here's an example 开发者_如何学Cscreen shot (simple shapes):

AS3: Approach to store MovieClips in a database

During use, these shapes can be dragged, resized, rotated, deleted and split into smaller shapes so their current properties need to be retained.

Any ideas? I just need a starting point.


you could write/read your data as ByteArray objects to the file system. this is particularly useful if you're developing an AIR application since you have the option to encrypt your data using the EncryptedLocalStore class (currently for desktop deployment targets only).


One typical approach is to store the set of drawing steps, and then re-parse these after loading them. i.e basically store a list of drawing commands. This also lets you unwind your drawing without too much extra work, do collaborative drawing, etc. It's a nice approach and very low-memory for uncomplicated scenes too.

It's not really important, but I think this is how Flash does drawing itself anyway when you draw primitives on a Graphics object.


I'd probably recommend exporting and importing from a subset of SVG.

Try http://code.google.com/p/as3svgrendererlib/ this library, you may find it useful.


I suspect there are going to be some application specific circumstances, but this is how I would approach it (I have done similar things before):

  1. Put all of the savable things in one Sprite or MovieClip.
  2. When you need to save a snapshot, loop through all of the children in this container and save off x,y,scaleX,scaleY,alpha,rotation. You may also want to save off the name and possibly what flash.utils.describeType returns so you can dynamically instantiate the objects.
  3. Stuff all of this into an associative array (probably one for each object) , serialize it, and save it somewhere. You could also create an XML structure and save it all there, too.

Depending on your needs you may need to save off filters, transform, etc.

To reconstruct, you essentially do everything backwards. Just be careful with your stacking order to make sure you layer things the same way.

This is pretty simplified, but should be a start. If I were doing this, I would put some more thought into doing essentially the same thing, but with a better object-oriented approach.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜