Introduction Part 3: Basic Code Template

Go back to Introduction

Starting the basic template

Go to "Project -> New Project" and choose an “AS3 Project”.
Browse to the games folder and go to src subfolder:
Extract the flixel engine there, which is in the "org" folder.

Now, put this code on the Main.as:
package
{
    /**
    * ...
    * @author pisces_eyes
    */
    
    import org.flixel.*;
    import com.game.MenuState;
    
    [SWF(width = "640", height = "480", backgroundColor = "#000000")]
    [Frame(factoryClass="Preloader")]
    
    public class Main extends FlxGame
    {
    
        public function Main():void
        {
        super(640, 480, MenuState, 1);
        }
    
    }

}


Put this code on Preloader.as:
package
{
    /**
    * ...
    * @author pisces_eyes
    */
    
    import org.flixel.FlxPreloader;
    
    public class Preloader extends FlxPreloader
    {
    
        public function Preloader()
        {
        className = "Main";
        super();
        }
    
    }
    
}

Create a new folder inside the same "src" folder named "com\game".
Under the com\game folder, create 2 new files:"MenuState.as and PlayState.as":

MenuState.as:
package com.game
{
    /**
    * ...
    * @author pisces_eyes
    */
    
    import flash.ui.Mouse;
    import org.flixel.*;
    
    public class MenuState extends FlxState
    {
    
        override public function create():void
        {
            var txt:FlxText
            txt = new FlxText(0, (FlxG.height / 2) - 32, FlxG.width, "Flixel Empty Game\n(MenuState)")
            txt.setFormat(null,32,0xFFFFFFFF,"center")
            this.add(txt);
            
            txt = new FlxText(0, FlxG.height - 32, FlxG.width, "CLICK OR PRESS X TO START")
            txt.setFormat(null, 16, 0xFFFFFFFF, "center");
            this.add(txt);
        }
    
        override public function update():void
        {
            if (FlxG.keys.pressed("X") || FlxG.mouse.justPressed())
            {
                FlxG.flash.start(0xffffffff, 0.75);
                FlxG.fade.start(0xff000000, 1, onFade);
            }
        
            Mouse.show();
            
            super.update();
        }
    
        private function onFade():void
        {
            FlxG.state = new PlayState();
        
        }
    
    }

}

PlayState.as:
package com.game
{
    /**
    * ...
    * @author pisces_eyes
    */
    
    import flash.ui.Mouse;
    import org.flixel.*;
    
    public class PlayState extends FlxState
    {
    
        override public function create():void
        {
            var txt:FlxText
            txt = new FlxText(0, (FlxG.height / 2) - 32, FlxG.width, "(PlayState)")
            txt.setFormat(null,32,0xFFFFFFFF,"center")
            this.add(txt);
        }
        
        override public function update():void
        {
            super.update();
            
            Mouse.show();
        }
    
    }

}

Now set Main.as as your startup object and run.

Go back to Introduction

2 comments:

Unknown said...

Fantastic post. This has really helped me to set-up my FlashDevelop files to a default template sort of state that I can use for all my work. Cheers!

Dominus Imperator said...

I think the library has changed since this article was posted. I found the following issues.

1 override public function update():void

2 {

3 if (FlxG.keys.pressed("X") || FlxG.mouse.justPressed())

4 {

5 FlxG.flash.start(0xffffffff, 0.75);

6 FlxG.fade.start(0xff000000, 1, onFade);

7 }

8

9 Mouse.show();

10

11 super.update();

12 }

13

14 private function onFade():void

15 {

16 FlxG.state = new PlayState();

17 }

on line 5 flash is a function, it has no property start, so, remove the .

on line 6 fade ia a function, do the same as above. In this same line onFade is a callback and is a method of this class, change it to this.onFade, if the game does not change states.

on line 16 state is a read-only property, so in order for the code to work replace this line with Flx.switchState(new PlayState());

That's it...