如何在as3中加载xml,为android安装

时间:2013-11-08 15:13:06

标签: android xml actionscript-3 flash air

我正在使用flash制作Android应用程序,我想要一些文本到apear,所以我认为使用xml是一个很好的方法来做到这一点,但我发现这并不像我想象的那么简单。我hade一个代码,但那是为as2并没有工作,所以我的问题是有人有一个良好的工作代码从xml文件加载一些文本或有人知道更好的方式在Flash中加载文本??? 谢谢你的回复

1 个答案:

答案 0 :(得分:1)

下面的代码从您的应用程序目录中加载一个xml文件(您必须将xml文件添加到您的应用程序目录中)到_myXml属性中。

private var _myXml : XML;
private var _file : File;

public function loadXML() : void {
  _file = File.applicationDirectory.resolvePath("myXml.xml");
  if (_file.exists) {     
    var stream : FileStream = new FileStream();
    stream.open(_file, FileMode.READ);
    var str : String = stream.readUTFBytes(stream.bytesAvailable);
    stream.close();
   _myXml = new XML(str);
  } else {
    trace("WARNING file:" +_file.nativePath + " does not exist");
  } 
}

此代码显示了如何实际使用XML文件并从中提取数据

public function loadScenes(lang : String) : Vector.<Scene> {
var scenes : Vector.<Scene> = new Vector.<Scene>();

for each (var scene : XML in _myXml.children()) {
    var sc : Scene = new Scene(loadImage(scene.background.@imageName, scene.background.@width, scene.background.@height), LibraryManager.getFurnitureById(scene.furniture.@furnitureId),scene.furniture.@furnitureId,lang);
    sc.furniture.x = scene.furniture.@x;
    sc.furniture.y = scene.furniture.@y;
    sc.furniture.scaleX = scene.furniture.@scaleX;
    sc.furniture.scaleY = scene.furniture.@scaleY;
    sc.furniture.rotation = scene.furniture.@rotation;
    sc.furniture.gotoAndStop(scene.furniture.@currentFrame);
    sc.setup = true;
    scenes.push(sc);
}
return scenes;

}

我的xml看起来像这样

<scenes>
      <scene>
        <furniture furnitureId="13" currentFrame="1" rotation="0" scaleY="0.4021450653932559" scaleX="0.4021450653932559" y="510.7" x="468.7" id="1"/>
        <background height="640" width="980" imageName="Garden2"/>
      </scene>
      <scene>
        <furniture furnitureId="8" currentFrame="1" rotation="0" scaleY="0.5015106201171875" scaleX="0.5015106201171875" y="516.9" x="488.55" id="2"/>
        <background height="640" width="980" imageName="Garden3"/>
      </scene>
      <scene>
        <furniture furnitureId="15" currentFrame="1" rotation="-0.06605712343630953" scaleY="0.4068730437596716" scaleX="0.4068730437596716" y="454.85" x="518.5" id="1"/>
        <background height="640" width="980" imageName="Garden1"/>
      </scene>
    </scenes>