Question

I am completely new to Actionscript and Adobe Flash CS6 and for a little bit of fun I have decided to try and make a little game. I had a few newbie (or noob-y) questions to ask about a general implementation approach.

The documentation I've been reading so far suggests creating a new flash project, and then create a document class so:

package  {

    import flash.display.MovieClip;

    public class MyMainClass extends MovieClip {


        public function MyMainClass() {

        }

    }

}

and I am wondering if I use this MainClass to code the whole game or include actionscript within a scene and have multiple scenes, or some combination of both.

Lets say I had a wanted 5 Levels in my game, would I do something like:

package  {

    import flash.display.MovieClip;

    public class MyMainClass extends MovieClip {


        public function MyMainClass() {
            StartLevel1();
            StartLevel2();
            StartLevel3();
            StartLevel4();
            StartLevel5();
        }

        public function StartLevel1() {
            // Do something
        }
        public function StartLevel2() {
            // Do something
        }
        public function StartLevel3() {
            // Do something
        }
        public function StartLevel4() {
            // Do something
        }
        public function StartLevel5() {
            // Do something
        }

    }

}

or create 5 scenes with actionscript in each scene? Can anyone provide me with a bit of a starting point? Thanks

Was it helpful?

Solution

I don't know of anyone who has anything good to say about scenes.

However, as you intuit, the timeline itself is a wonderful tool for managing the state of your Flash assets over time. If you use it, you also get the hidden advantage that you don't have to download 100% of your file to be able to use it (so you can reduce or even eliminate the need for a preloader by unchecking "Export in frame N" on your library symbols.

Lars has quite rightly pointed out that there are very few developers who understand this technique, and I know of exactly one who can and will help people who are interested in exploring this technique. That person is helping you right now. So if you choose to go that way, keep in mind you are mostly on your own except if I happen to notice your post and respond to it.

I am not in favor of timeline scripts, with a very few exceptions. What I suggest is a "both and" approach, where you use a Document Class to control timeline instances.

Your document Class might look something like this:

public class Game extends MovieClip {
    protected var _level:ILevel;//Interface your Level MovieClips will implement
    protected var levelController:LevelController = new LevelControler();
    protected var currentLevel:int;
    protected var maxLevels:int = 5;

    public function Game() {
        levelController.addEventListener(LevelEventKind.LEVEL_COMPLETE, nextLevel);
        levelController.addEventListener(LevelEventKind.LEVEL_FAILED, gameOver);
        startLevel(currentLevel);
    }

    public function startLevel(levelNumber:int):void {
        goToLabel('Level' + String(levelNumber));
    }
    public function get level():ILevel {
        return _level;
    }
    public function set level(value:ILevel):void {
       _level = value;
       //internally, this should release all listeners to the last 
       //level object (if any) so you don't get a memory leak
       levelController.level = _level;
    }
    protected function nextLevel(e:Event):void {
        if (currentLevel < maxLevels) {
            startLevel(++currentLevel);
        } else {
            //do you won logic here
        }
    }
    protected function gameOver(e:Event):void {
        //do bombed out logic here
    }
    protected function goToLabel(label:String):void {
        for each (var frameLabel:FrameLabel in currentLabels) {
            if (frameLabel.name==label) {    
                //if your swf is media-heavy, may want to check that frame
                //is loaded if you chose to reduce/eliminate preloader
                goToAndStop(label);
                return;
            }
        }
        trace('no such label as', label);
    }
}

What this gets you is a game where you can change how the different levels look without changing a single line of ActionScript, and you can change how they work by assigning different Base Classes that implement ILevel slightly differently. You can also change your functionality by swapping out different flavors of LevelController, but your Main Document Class (Game in this instance) would be aware of this change (wheras the other changes could be made without altering Game at all).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top