我有一个Air Application(Adobe Flash CS4,Adobe AIR 1.1,ActionScript 3.0)。我已将其发布为* .air文件并将其安装在我的计算机上。它工作正常。但是当我试图在另一台计算机上使用它时,我发现了以下问题:
我的应用程序的代码如下:
public class Main extends MovieClip {
public function Main():void
{
this.stop();
var file:File = File.applicationDirectory.resolvePath("pattern.xml");
var fileStream = new FileStream();
fileStream.open(file, FileMode.READ);
var str:String = fileStream.readUTFBytes(fileStream.bytesAvailable);
str=str.substr(1);
var panoramaPattern=new XML(str);
fileStream.close();
}
}
我试图在Main()中评论几个命令。因此,代码无需
var panoramaPattern=new XML(str);
这个命令有什么问题? pattern.xml包含在“包含的文件”中。
答案 0 :(得分:1)
我想现在发生的事情是,只要创建了swf的主类(上面)(在初始化时),ENTER_FRAME事件就会将事件监听器绑定到按钮,但该按钮在技术上并不存在。你在这里初始化的方法是非常糟糕的做法,但请允许我解释这一切是如何运作的。
每当你有一个扩展DisplayObject类型的类时,你应该总是创建一个设计用于检测“stage”元素的修改构造函数,如果它不存在,请监听ADDED_TO_STAGE事件,然后执行回调中基于显示对象的初始化。这是因为基于显示对象的类是以半成为一种方式创建的。在创建/实例化类时立即调用构造函数,但该类的属性和方法(包括作为显示对象的子项(如本例中的按钮等)在将类添加到全局之前不可用)阶段“对象。
对于AIR,您的NativeWindow对象包含一个“stage”实例,该NativeWindow的所有子项都继承该实例。因此,当您向舞台添加MovieClip或Sprite等时,该显示对象的“stage”属性将填充对NativeWindow中包含的全局舞台对象的引用。所以永远记住,当涉及到flash时,处理构造函数/初始化显示对象的做法是将所有功能延迟到仅在全局“阶段”可用于引用时处理的回调。以下是使用您的代码的示例:
public class Main extends MovieClip {
public function Main():void
{
if(stage){
init();
}else{
this.addEventListener(Event.ADDED_TO_STAGE, init);
}
}
//Can be private or public, doesn't matter private is better practice
private function init(e:Event = null)
{
//Notice the function paramter has a default value assigned of null. This is required so we can call this function without args as in the constructor
//Also the flag variable is not necessary because this function is called once
btnDialogCreate.addEventListener(MouseEvent.CLICK,CreateProject);
}
//Also it is generally considered bad practice to put capitals on the start of your function/variable names. Generally only classes are named this way ie: Main.
public function createProject(e:MouseEvent){
//You do not need a try/catch statement for simply opening a file browser dialogue. This is a native method you're invoking with a native, predefined default directories inside the VM. Flash is already doing this for you
var directory:File=File.documentsDirectory;
directory.browseForDirectory("Directory of project");
}
}
最后,我强烈建议您观看本网站上的一些免费视频教程,因为有大量的主题可以教你很多关于flash的内容。
答案 1 :(得分:-1)
我找到了解决方案。
我已将pattern.xml的编码更改为ANSI
我已将XML加载算法更改为this one
有效!