Best way to save data from flash
I have an AS3 document with roughly 200 individual elements I'm trying to save to the datab开发者_StackOverflow社区ase. I just need to saved information about their physical properties on the stage.
There are many ways to do this, but what is the recommended approach?
The backend is PHP/MySQL and it's not a two-way exchange. Save it, done.
apart from the server side, a good way to store / assign position/rotation/scale is to use the DisplayObjects' transformation matrices:
shape.transform.matrix
sprite.transform.matrix
it is rather compact ; need to store 6 Numbers per object. rounding the scale/rotation values down to 5 decimals ( 0.12345 instead of 0.123456789123456 ) and the translation to 1 decimal ( 0.1 instead of 0.123456 ) works pretty well to spare some Ko. for example this method:
private function storeMatrix( displayObject:DisplayObject, decimals:int = 5 ):String
{
var str:String = '';
var m:Matrix = displayObject.transform.matrix;
str += m.a.toFixed( decimals ) + ':';
str += m.b.toFixed( decimals ) + ':';
str += m.c.toFixed( decimals ) + ':';
str += m.d.toFixed( decimals ) + ':';
str += m.tx.toFixed( 1 ) + ':';
str += m.ty.toFixed( 1 );
return str;
}
will return something like:
-0.95119:-0.30550:0.30550:-0.95119:110.0:110.0
and this method sets the position/rotation/scale from the string:
private function assignMatrix( str:String, _do:DisplayObject ):void
{
var values:Array = str.split( ':' );
var m:Matrix = new Matrix();
m.a = values[ 0 ];
m.b = values[ 1 ];
m.c = values[ 2 ];
m.d = values[ 3 ];
m.tx = values[ 4 ];
m.ty = values[ 5 ];
_do.transform.matrix = m;
}
once you've collected all the object's matrices, you can serialize them with AMF and store to database. last time I did that, I used FZIP to compress the data even further ( 500Ko->20Ko). I don't think it'll help in your case, just wanted to point it out :)
NB: if needed, you can also store the transform.ColorTransform properties of an object in the very same way.
I would check out flash remoting/AMFPHP: http://amfphp.sourceforge.net/
It's pretty straightforwad, and really easy to implement.
Hope this helps.
精彩评论