使用FLEX和 Actionscript 开发FLASH游戏(三)-1

来源:互联网 发布:网上赚钱的软件 编辑:程序博客网 时间:2024/04/30 06:32

使用FLEX 和 Actionscript开发FLASH 游戏—嵌入资源和增加游戏元素
“使用FLEX开发游戏”系列之三
写自Matthew Casperson Casperson
2008年10月31日出版
在第三部分我们将增加一些图像资源并把它们显示在屏幕上
标签:Flex,Flash,tutorial,Actionscript,game

转到:第一部分

在写一个新程序的时候,总存在这么一个点,从这儿你可以看见你的劳动成果。通过实现状态和绘制的管道,我们现在可以开始做有趣的事情了,比如在游戏中添加图像,把它们显示在屏幕上。但在我们这么做之前先让我们看看我们得在main.mxml文件中做些什么改变。

<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
width="600"
height="400"
frameRate="100"
creationComplete="creationComplete()"
enterFrame="enterFrame(event)"
currentState="MainMenu">
    <mx:states>
        <mx:State
            name="Game"
            enterState="enterGame(event)"
            exitState="exitGame(event)">
        </mx:State>
        <mx:State name="MainMenu">
            <mx:AddChild relativeTo="{myCanvas}"position="lastChild">
                <mx:Button x="525" y="368" label="Start" id="btnStart"
                click="startGameClicked(event)"/>
            </mx:AddChild>
        </mx:State>
    </mx:states>
    <mx:Canvas x="0" y="0" width="100%" height="100%" id="myCanvas"/>
    <mx:Script>
    <![CDATA[
        protected var inGame:Boolean=false;
        public function creationComplete():void
        {
        }
        public function enterFrame(event:Event):void
        {
            if(inGame)
            {
                GameObjectManager.Instance.enterFrame();
                myCanvas.graphics.clear();
                myCanvas.graphics.beginBitmapFill                                            

     (GameObjectManager.Instance.backBuffer,null,false,false);
                myCanvas.graphics.drawRect(0,0,this.width,this.height);
                myCanvas.graphics.endFill();
            }
        }
        public function startGameClicked(event:Event):void
        {
            currentState="Game"
        }
        protected function enterGame(event:Event):void
        {
            GameObjectManager.Instance.startup();
            inGame=ture;
        }
        protected funciong exitGame(event:Event):void
        {
            GameObjectManager.Instance.shutdown();
            inGame=false;
        }
    ]]>
    </mx:Script>
</mx:Application>  
只在exitGame函数做了一个改变,该函数调用GameObjectManager的shutdown(关闭)函数。这将允许我们在离开游戏状态时GameObjectManager清除资源。现在让我们来看看在GameObjectManager中做的改变。

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/literza/archive/2009/06/04/4241413.aspx