Starling 游戏 太空大战1 游戏引擎

这是一个基于Starling 框架做简单的空战游戏,游戏大致分三个界面。
第一:游戏欢迎界面 Menu
第二:游戏操作界面 Play
第三:游戏结束界面 GameOver

搭建游戏引擎架构,
1:创建一个接口类IState.as

package interfaces
{
    public interface IState
    {
        function update():void;
        function destroy():void;
    }
}


2:创建游戏环境界面类

package states
{
    import interfaces.IState;

    import starling.display.Sprite;
    import starling.events.Event;
    
    public class Menu extends Sprite implements IState
    {
        private var game:Game;
        
        public function Menu(game:Game)
        {
            this.game = game;
            addEventListener(Event.ADDED_TO_STAGE,init);
        }
        
        private function init(event:Event):void
        {

        }
        
        private function onPlay(event:Event):void
        {
         }
        
        public function update():void
        {
        
        }
        
        public function destroy():void
        {
        
        }
    }
}

3:创建基础游戏界面Play

package states
{
    import interfaces.IState;

    import starling.display.Sprite;
    import starling.events.Event;
    
    public class Play extends Sprite implements IState
    {
        private var game:Game;
        
        public function Play(game:Game)
        {
            this.game = game;
            addEventListener(Event.ADDED_TO_STAGE,init);
        }
        
        private function init(event:Event):void
        {

        }
        
        private function onPlay(event:Event):void
        {
         }
        
        public function update():void
        {
        
        }
        
        public function destroy():void
        {
        
        }
    }
}

 

4:创建游戏结束界面

package states
{
    import interfaces.IState;

    import starling.display.Sprite;
    import starling.events.Event;
    
    public class GameOver extends Sprite implements IState
    {
        private var game:Game;
        
        public function GameOver(game:Game)
        {
            this.game = game;
            addEventListener(Event.ADDED_TO_STAGE,init);
        }
        
        private function init(event:Event):void
        {

        }
        
        private function onPlay(event:Event):void
        {
         }
        
        public function update():void
        {
        
        }
        
        public function destroy():void
        {
        
        }
    }
}

到此我们就设计好了游戏的基本引擎架构,我们通过Game类来把他们串接起来

5:Game.as

package core
{
    import starling.events.Event;
    import interfaces.IState;import starling.display.Sprite;
    
    public class Game extends Sprite
    {
        public static const MENU_STATE:int = 0;
        public static const PLAY_STATE:int = 1;
        public static const GAME_OVER_STATE:int = 2;
        
        private var current_state:IState;
        
        public function Game()
        {
            addEventListener(Event.ADDED_TO_STAGE,init);
        }
        
        private function init(event:Event):void
        {
            //---  舞台初始化  首先增加菜单界面 ----
            changeState(MENU_STATE);
            //---  逐帧事件 -----
            addEventListener(Event.ENTER_FRAME,update);
        }
        
        //---  改变舞台界面时使用  ----
        public function changeState(state:int):void
        {
           //首先将原有的界面对象清除
            if(current_state != null){
               current_state.destroy();
               current_state = null;
           }
           //根据状态名切换
           switch(state){
           
               case MENU_STATE:
                   current_state = new Menu(this);
                   break;
               case PLAY_STATE:
                   current_state = new Play(this);
                   break;
               case GAME_OVER_STATE:
                   current_state = new GameOver(this);
                   break;
           }
           //添加舞台
           addChild(Sprite(current_state));
        }
        
        //----  更新  ------
        private function update(event:Event):void
        {
           //执行当前界面的update函数
            current_state.update();
        }
    }
}

 

posted @ 2013-12-25 14:22  无名盗闪  阅读(199)  评论(0)    收藏  举报