开发者

Relationship of code on first frame of main time line to class code and library objects

Got to the point where the hole in my AS3 knowledge is getting large! Realising I have confusion about the relationship of:

Body of code on first frame of AS3 file. (Which I have so far used to create instances of library objects using addchild and make calls to class code). Is this code called the document code? What do people call it?

Numerous classes linked together by extending each other etc.

Library objects (usually graphical objects)

Should it be done differently?

I have the following problems due to lack of understanding:

开发者_开发技巧

Addchild complicated from a class but straight forward in main body code. Cant call functions on the main body code from classes, because class code does not know the main body code exists?

So it feels like the two cant communicate easily?

Any pointers will be greatly appreciated.


The code on the firstframe is called 'timeline script' and should be kept to a minimum (depending on the project). I use timeline code, at most, to setup new objects from other classes.

The document class is different, and can be considered a special case. It is automatically called when your SWF starts. It is set at design time by clicking on the stage and typing in the class name in the document class field in the properties section.

Behind the scenes, Flash grabs all your timeline code and puts into the document class as a series of methods that are invoked when the timeline changes frame. If you haven't created a document class Flash will create a default one and still put the timeline code there. This process is invisible to the developer, and few knows how it works. The results of this become evident when you start using methods such as addFrameScript (an advanced method - look into it when you're a bit more confident.)

Class/stage/timeline/object/movieclip communication. Ah the joys. @Adrian do not worry, this is not an easy thing to master. Your confusion stems from the fact that Adobe has grabbed a Object Oriented language and welded it to a legacy based timeline driven system - don't get me wrong, it's the way it had to happen to keep AS2's happy yet keeping those happy who strived for a strongly typed language.

One way to setup a class is:

package {
    import flash.display.*;

    public class Foo extends Sprite {
        private var _stage:MovieClip;

        public function Foo(stage:MovieClip) {
            _stage = stage; // access to the stage      
        }

        public function generateBoxes(n:int):void {
            for(var i:int = 0; i < n; i++) {
                                // call stage function
                _stage.addBox(Math.random() * 50, Math.random() * 50);
            }
        }

    }
}

Then on the stage

var foo:Foo = new Foo(this);

foo.generateBoxes(10);

// Gets called by Foo
function addBox(x:Number, y:Number):void {
    var b:Box = new Box();
    b.x = x;
    b.y = y;
    addChild(b);
}

In the library there needs to be an graphic exported for actionscript called Box (the class name)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜