问题描述
我一直都在interwebs,包括堆栈溢出,几个小时,试图找出信息保存在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。我们将调用对象的球和蝙蝠
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 code能做到这一点。这些数据是在影片剪辑的两个向量,所以我会用球棒[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.
推荐答案
与XML在AS3工作是很容易,所以要扩大TheDarkIn1978的一些code答案:
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
的COM $ P $保存前pssing你的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" );
装载在COM pressed XML,uncom pressing它,并检索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 );
}
我创建了COM pressing / uncom pressing XML文件的简单工具。你可以得到的SWF和源的<一个href="http://divillysausages.com/blog/xml_com$p$pssor_uncom$p$pssor">http://divillysausages.com/blog/xml_com$p$pssor_uncom$p$pssor如果你有兴趣
这篇关于节能的Flash(AS3)数据到XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!