【问题标题】:How to load xml in as3, air for android如何在 as3、air for android 中加载 xml
【发布时间】:2013-11-08 15:13:05
【问题描述】:

我正在为 android in flash 制作一个应用程序,我希望显示一些文本,所以我认为使用 xml 是一种很好的方法,但我发现这并不像我想象的那么简单。我有一个代码,但那是针对 as2 的,没有用,所以我的问题是有人有一个很好的工作代码来从 xml 文件中加载一些文本,还是有人知道更好的方法来在 flash 中加载文本? 谢谢回复

【问题讨论】:

    标签: android xml actionscript-3 flash air


    【解决方案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>
    

    【讨论】:

    • 我收到此错误:场景 1,图层“图层 1”,第 1 帧,第 4 行 1114:公共属性只能在包内使用。场景 1,图层“图层 1”,第 1 帧,第 16 行 1114:公共属性只能在包内使用。场景 1,图层“图层 1”,第 1 帧,第 2 行 1013:私有属性只能用于类属性定义。场景 1,图层“图层 1”,第 1 帧,第 1 行 1013:私有属性只能用于类属性定义。
    猜你喜欢
    • 2012-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-09
    • 1970-01-01
    • 2021-02-11
    相关资源
    最近更新 更多