Tuesday, October 2, 2012

Import ActionScript 3 external class to Flash CS5



Suppose you have flash file myflash.fla in
/work_dir/myflash.fla
and you have Main class in:
/work_dir/com/example/Main.as
Main.as is as follows:
package com.example
{
    public class Main extends Sprite
    {
        public function Main():void
        {
            trace("here");
        }
    }
}

There are two ways to call Main class in Flash CS5.
 1) In Flash CS5, File->ActionScript setting-> document class, input Main
or
2) Press F9, import the following script:
var test:Main = new Main();
addChild(test);
There is some difference between method 1) and method 2
If stage.stageWidth is used in the script, the method 1 is recommended. The method 2 will return the following error message:
Error #1009: Cannot access a property or method of a null object reference.

No comments:

Post a Comment