将Flash(AS3)数据保存到XML [英] SAVING Flash (AS3) data to XML
问题描述
我已经遍及互联网,包括堆栈溢出,几个小时,试图找出一个稳固的可行的例子,将Flash中的信息保存到XML文件中。
I've been all over the interwebs, including Stack Overflow, for hours, trying to identify a solid, workable example of saving information in Flash into an XML file.
我想采取两种不同类型对象的位置,并将每个对象的列表导出到XML。我们将调用对象 ball 和 bat 。
I want to take the positions of two different types of objects and export the lists of each to XML. We'll call the objects ball and bat.
所以,我希望拥有XML外观像:
So, I'd love to have the XML look something like:
<objects>
<ball xPos=34 yPos=43/>
<ball xPos=12 yPos=94/>
<bat xPos=1 yPos=39/>
</objects>
听起来很简单,但是我还没有找到一个体面的例子,正是AS3代码可以实现这一点。数据是MovieClip的两个向量,所以我将使用蝙蝠[i] .x和蝙蝠[i] .y输入值。
Sounds simple enough, but I haven't been able to find a single decent example for exactly what AS3 code can accomplish this. The data is in two vectors of MovieClips, so I'd be using bats[i].x and bats[i].y for input values.
创建此XML,并将其保存在本地查看的地方?感谢任何帮助,这已经证明是非常令人沮丧的。
How can I create this XML, and save it somewhere local to view? Thank you for any help at all, this has proved extremely frustrating.
推荐答案
在AS3中使用XML非常简单,所以在TheDarkIn1978的答案中扩展一些代码:
Working with XML in AS3 is really easy, so to expand on TheDarkIn1978's answer with some code:
创建一个XML对象:
var objs:XML = new XML( <objects /> ); // create the <objects /> node
// for your objects
var ball1:XML = new XML ( <ball /> ); // create the <ball /> node
ball1.@xPos = 12; // add 12 as an attribute named "xPos"
ball1.@yPos = 42; // add 42 as an attribute named "yPos"
objs.appendChild( ball1 ); // add the <ball> node to <objects>
// an example of using variables in your xml
var name:String = "something";
var sx:XML = new XML( <{name} /> ); // creates a node <something />
使用TheDarkIn1978的 XML
AS3了解更多信息。
Use the TheDarkIn1978's to the XML
class in AS3 to learn more.
保存文件:
// saving out a file
var f:FileReference = new FileReference;
f.save( sx, "myXML.xml" ); // saves under the name myXML.xml, "sx" being your root XML node
压缩您的XML在保存之前(使用大型XML文件,这可以节省很多):
Compressing your XML before saving (with large XML files, this can save a lot):
// compressing before saving
var f:FileReference = new FileReference;
var bytes:ByteArray = new ByteArray;
bytes.writeUTFBytes( myXML ); // "myXML" being your root XML node
bytes.compress(); // compress it
f.save( bytes, "myXML.xml" );
加载压缩XML,解压缩并检索XML对象:
Loading in a compressed XML, uncompressing it, and retrieving the XML object:
// uncompressing a compressed xml
var loader = new URLLoader;
loader.dataFormat = URLLoaderDataFormat.BINARY;
// listen for our events
loader.addEventListener( Event.COMPLETE, this._onLoad );
loader.addEventListener( IOErrorEvent.IO_ERROR, this._onFail ); // not shown
loader.addEventListener( SecurityErrorEvent.SECURITY_ERROR, this._onSecurityError ); // not shown
private function _onLoad( e:Event ):void
{
var loader:URLLoader = e.target as URLLoader;
// get the data as a bytearray
var ba:ByteArray = loader.data as ByteArray;
// uncompress it
try
{
ba.uncompress();
}
catch ( e:Error )
{
trace( "The ByteArray wasn't compressed!" );
}
// get our xml data
myXML = XML( ba );
}
我创建了一个压缩/解压缩XML文件的简单工具。您可以在 http://divillysausages.com/blog/xml_compressor_uncompressor 获取SWF和来源,如果您是感兴趣的
I created a simple tool for compressing/uncompressing XML files. You can get the SWF and source at http://divillysausages.com/blog/xml_compressor_uncompressor if you're interested
这篇关于将Flash(AS3)数据保存到XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!