0
votes

I have an Air Application (Adobe Flash CS4, Adobe AIR 1.1, ActionScript 3.0). I've published it as *.air file and installed it on my computer. It worked fine. But when I tried to use it on the other computer, I found the following problem:

  1. I installed AIR from http://get.adobe.com/ru/air/
  2. I installed my Main.air and launched it.
  3. It can't parse XML file (pattern.xml) correctly.

The code of my app is following:

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();
    }
}

I tried to comment a several commands in Main(). So, code works without

var panoramaPattern=new XML(str);

What is wrong with this command? pattern.xml was included into "Included files".

2

2 Answers

1
votes

I imagine what is happening is that as soon as your swf's main class here (above) is created (right at initialization), the ENTER_FRAME event is binding the event listener to the button, but the button does not technically exist. Your methodology for initializing here is very bad practice but allow me explain how this all works.

Any time that you have a class that extends a type of DisplayObject, you should ALWAYS create a modified constructor designed to detect the "stage" element, and if it doesn't exist, listen for the ADDED_TO_STAGE event, and then perform your display-object based initializations within the callback. This is because display object based classes are kind of created in a half-assed way. The constructor is called immediately when the class is created/instantiated, but the properties and methods of that class, including children that are display objects (as in this case, buttons etc) are not available until the class has been added to the global "stage" object.

In the case of AIR, your NativeWindow object contains a single instance of "stage" that all children of that NativeWindow inherit. So when you add a MovieClip or a Sprite etc to the stage, the "stage" property of that display object is populated with a reference to the global stage object contained within NativeWindow. So always remember, when it comes to flash the practice with dealing with constructors/initialization of display objects is to delay all functionality to a callback that is processed only when the global "stage" has become available to reference. Below is an example using your code:

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");
    }

}

Lastly I would highly recommend watching some of the free video tutorials on this site, as there are a wide range of subjects covered that will teach you much about flash.

http://gotoandlearn.com/

-1
votes

I've found the solution.

  1. I've change the encoding of pattern.xml to ANSI

  2. I've change XML loading algorithm to this one

It works!