Skip to main content
Mr. Baker the Shoe Maker
Inspiring
August 19, 2014
Question

Problem with Game states using ISTATE

  • August 19, 2014
  • 0 replies
  • 235 views

I am studying Lee Brimelow's approach to game states. In his tutorials he uses an Interface and then create different classes for each of the game states. In my case, I am using the following game states:

  • MainMenu - The main screen where the user first enters the app.
  • Game - Where the user plays the game.
  • Transition - This is where the game ends and the user's results are presented (score, bonus points etc.)

The problem I am having is that when I trace the current game state, it says that I am in the Transition state or [object Transition] when I should be in the MainGame state. In my init function in the MainGame class I specifically set the initial state to be the MainGame class with statement, changeState(MAIN_MENU_STATE);. FYI, I am using Starling so the first class initiates the Starling object. I've also color coded each class in blue.Any thoughts on what I'm doing wrong? Below is the code:


Starling Initiation

package

{

    import flash.display.Sprite;

   

   

    import starling.core.Starling;

    [SWF (width= "1024", height = "768", framerate = "60", backgroundColor ="0xffffff")]

   

    public class BB extends Sprite

    {

        private var myStarling:Starling;

        public function BB()

        {

            //stage.scaleMode = StageScaleMode.NO_SCALE;

            //stage.align = StageAlign.TOP_LEFT;

            myStarling = new Starling (MainGame, stage);

            myStarling.antiAliasing = 1;

            myStarling.start();

            myStarling.showStats = true;

        }

    }

}

MainGame Code

package

{

    import interfaces.IState;

   

    import starling.display.Sprite;

    import starling.events.Event;

   

    import states.MainMenu;

    import states.Play;

    import states.Transition;

   

    public class MainGame extends Sprite

    {

        //3 constants for the 3 different game states

        public static const MAIN_MENU_STATE:int = 0;

        public static const PLAY_STATE:int = 1;

        public static const TRANSITION_MENU_STATE:int = 2;

       

   

        private var current_state:IState;

        //private var backgroundMain:Image;

       

        public function MainGame()

        {

            Assets.init();

            addEventListener(Event.ADDED_TO_STAGE, init);

        }

       

        private function init(event:Event):void

        {

       

            changeState(MAIN_MENU_STATE);

            addEventListener(Event.ENTER_FRAME, update);

       

        }

       

        //changes the state of the game. changeState Looks at what is passed in and creates a new instant of the current state.

        public function changeState(state:int):void

        {

        if (current_state != null)

        {

            current_state.destroy();

            current_state=null;

        }

       

        switch (state)

        {

            case MAIN_MENU_STATE:

                current_state = new MainMenu(this);

               

            case PLAY_STATE:

                current_state = new Play(this);

               

            case TRANSITION_MENU_STATE:

                current_state = new Transition(this);

        }

       

        //adds the current state. Cast in sprite because current_state is of type IState

        addChild(Sprite(current_state));

        }

       

        //call the update method of whatever state is currently active

        private function update():void

        {

            current_state.update();

            trace(current_state);

           

           

                           

        }       

       

       

    }

}

ISTATE Code

package interfaces

{

    public interface IState

    {

        function update():void;

        function destroy():void;

    }

}

MainMenu CODE

package states

{

    import interfaces.IState;

   

    import starling.display.Image;

    import starling.display.Sprite;

    import starling.events.Event;

   

    public class MainMenu extends Sprite implements IState

    {

        private var game:MainGame;

           

       

        public function MainMenu(game:MainGame)

        {

            this.game = game;

            addEventListener(Event.ADDED_TO_STAGE, init);

        }

       

        private function init(event:Event):void

        {

            trace("This is working!");

           

        }

       

        public function update():void

        {

        }

       

        public function destroy():void

        {

        }

    }

}

Play Code

package states

{

    import interfaces.IState;

    import starling.display.Sprite;

    import starling.events.Event;

   

    public class Play extends Sprite implements IState

    {

        private var game:MainGame;

       

        public function Play(game:MainGame)

        {

            this.game = game;

            addEventListener(Event.ADDED_TO_STAGE, init);

        }

       

        private function init(event:Event):void

        {

           

        }

       

        public function update():void

        {

        }

       

        public function destroy():void

        {

        }

    }

}

Transition Code

package states

{

    import interfaces.IState;

    import starling.display.Sprite;

    import starling.events.Event;

   

    public class Transition extends Sprite implements IState

    {

        private var game:MainGame;

       

        public function Transition(game:MainGame)

        {

            this.game = game;

            addEventListener(Event.ADDED_TO_STAGE, init);

        }

       

        private function init(event:Event):void

        {

           

        }

       

        public function update():void

        {

        }

       

        public function destroy():void

        {

        }

    }

}

This topic has been closed for replies.