开发者

How to import a document class file and play it out on a frame

I have 2 flash files, one with an intro and the second that just has a document cla开发者_如何转开发ss file that plays out a snake game. How could i import that document flash file and make it play out on like frame 100 off my other flash file.

Thankyou for any help.


You can instantiate the Snake class file in your intro, using code like the following on frame 100:

var game:Snake = new Snake();
addChild(game);

assuming your document class is called SnakeGame. You may have to make a few changes in SnakeGame for this to work, however. The stage and root properties of SnakeGame will be null in the constructor when using this method. This is different behavior from it's was the document class (or placed directly on the stage) -- stage and root are already initialized. So if you do any actions to either root or stage in the constructor, you must change this or you will get an error. To fix this, listen for the ADDED_TO_STAGE event in Snake:

public function Snake() {
    addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}

public function onAddedToStage(event:Event):void {
    removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
    // stage+root are now valid, put your init code here
}

Alterantively, you could link the Snake class to an empty MovieClip symbol and place that directly on the stage on frame 100. First, create an empty MovieClip by going to Window->Library and clicking the +. In the Create New Symbol dialog, check "Export for ActionScript" and type in Snake in the Class field under Linkage. Now you can drag that MovieClip directly onto the stage on frame 100 and it hopefully will work without changes to the code.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜