我也是刚刚接触到Flex的游戏开发, 继j2me的手机游戏开发后,再现学现卖,发一个Flash版的贪吃蛇游戏开发的Step by Step教程,希望对新手有些帮助. :).
Step 1.
下载J2SE和Flex 3.1 SDK(http://download.macromedia.com/pub/flex/sdk/flex_sdk_3.zip,免费的,哈哈),安装好Java,解压 Flex SDK到路径G:/javaspace/flex_sdk_3
Step 2.
到我的电脑->属性->高级->环境变量,设置PATH路径加上: C:/j2sdk1.4.2_08/bin;G:/javaspace/flex_sdk_3/bin
Step 3.
创建G:/javaspace/workspace/SnakeGameFlash和G:/javaspace/workspace/SnakeGameFlash/asx两个目录.
Step 4.
创建G:/javaspace/workspace/SnakeGameFlash/SnakeGame.mxml文件,内容如下:
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application name="SnakeGame"
- xmlns:mx="http://www.adobe.com/2006/mxml"
- xmlns:custom="asx.*"
- width="600" height="400"
- frameRate="8"
- creationComplete="creationCompleteHandler(event);"
- >
- <mx:Script>
- <![CDATA[
- import flash.events.MouseEvent;
- import mx.events.FlexEvent;
- import asx.SnakeMc;
- private function creationCompleteHandler(event:FlexEvent):void
- {
- var mc:SnakeMc = new SnakeMc();
- swfldr.addChild(mc);
- }
- ]]>
- </mx:Script>
- <mx:SWFLoader id="swfldr" width="512" height="280"/>
- </mx:Application>
Step 5.
创建G:/javaspace/workspace/SnakeGameFlash/asx/SnakeBlock.as文件,内容如下:
- package asx
- {
- import flash.display.Sprite;
- public class SnakeBlock extends Sprite
- {
- public var r:uint=2;
- public var xx:uint=0;
- public var yy:uint=0;
- public function SnakeBlock()
- {
- super();
- }
- }
- }
Step 6.
创建G:/javaspace/workspace/SnakeGameFlash/asx/SnakeMc.as文件,内容如下:
- package asx
- {
- import flash.display.MovieClip;
- import flash.events.Event;
- import flash.events.KeyboardEvent;
- import flash.text.TextField;
- import flash.ui.Keyboard;
- public class SnakeMc extends MovieClip
- {
- public const blocksize:uint = 8;
- public const blWidth:uint = 64
- public const blHeight:uint = 32;
- public const numFood:uint = 20;
- public var t:TextField = null;
- public var t2:TextField = null;
- public var ss:SnakeBlock = null;
- public var sa:Array = new Array();
- public var kbq:Array = new Array();
- public var rr:Array = new Array();
- public var food:Array = new Array();
- public var scores:uint = 0;
- public var bonus:uint = 0;
- public var tickcount:Number =0;
- public function CreateSprite(w:uint=0):SnakeBlock
- {
- var s:SnakeBlock = new SnakeBlock();
- this.addChild(s);
- with(s)
- {
- if (w==0) // blue snake color the other times
- graphics.beginFill(0x555588);
- else
- graphics.beginFill(255<<16); // red food color first time
- graphics.lineTo((blocksize-1),0 );
- graphics.lineTo((blocksize-1), (blocksize-1));
- graphics.lineTo(0, (blocksize-1));
- graphics.endFill(); // draw a square
- }
- return s;
- }
- public function AppendSprite(w:uint=0):SnakeBlock
- {
- var s:SnakeBlock = CreateSprite(w);
- sa.push(s);
- if(sa.length>1)
- {
- s.x = sa[sa.length-2].x;
- s.y = sa[sa.length-2].y;
- s.xx = sa[sa.length-2].xx;
- s.yy = sa[sa.length-2].yy;
- s.r = (sa[sa.length-2].r+2)%4;
- UpdateSnakePos(s);
- s.r = sa[sa.length-2].r;
- }
- else
- {
- s.xx = s.x/blocksize;
- s.yy = s.y/blocksize
- }
- return s;
- }
- public function UpdateSnakePos(s:SnakeBlock):void
- {
- with(s)
- {
- switch(r)
- {
- case 0:
- x -=blocksize;
- xx--;
- break;
- case 1:
- y -=blocksize;
- yy--;
- break;
- case 2:
- x +=blocksize;
- xx++;
- break;
- case 3:
- y +=blocksize;
- yy++;
- break;
- }
- }
- }
- public function upateSnakesPos():void
- {
- var i:int =0;
- for(i=0;i<sa.length;i++)
- {
- var s:SnakeBlock =sa[i];
- for(var j:int=0;j<rr.length;j++)
- {
- if(i==(rr[j]>>16))
- {
- s.r = (rr[j]0xffff)%4;
- }
- }
- UpdateSnakePos(s);
- }
- var tt:Array = new Array();
- for(i=0;i<rr.length;i++)
- {
- var tmp:uint = (rr[i]>>16);
- if(tmp <sa.length+10)
- {
- rr[i] =((tmp+1)<<16)|(rr[i]0xffff);
- tt.push(rr[i]);
- }
- }
- rr = null;
- rr = tt;
- }
- public function onkeydown(evt:KeyboardEvent):void
- {
- if(evt.keyCode==Keyboard.SPACE)
- {
- if(ss==null)
- {
- cleanup();
- ss=AppendSprite();
- for(var i:uint;i<0;i++)
- {
- AppendSprite();
- }
- genFood();
- }
- }
- else if(ss!=null)
- {
- switch(evt.keyCode)
- {
- case Keyboard.LEFT:
- case 65:
- kbq.unshift(0);
- break;
- case Keyboard.UP:
- case 87:
- kbq.unshift(1);
- break;
- case Keyboard.RIGHT:
- case 68:
- kbq.unshift(2);
- break;
- case Keyboard.DOWN:
- case 83:
- kbq.unshift(3);
- break;
- default:
- break;
- };
- }
- }
- public function IntersectSnake(xx:int, yy:int):Boolean
- {
- var i:uint=0;
- for(i=0;i<sa.length;i++)
- {
- if(xx==sa[i].xx && yy==sa[i].yy)
- {
- return true;
- }
- }
- return false;
- }
- public function IsGameOver():Boolean
- {
- if(ss.xx<0 || ss.xx >blWidth-1 || ss.yy<0 || ss.yy >blHeight-1)
- {
- return true;
- }
- var i:uint =0;
- for(i=1;i<sa.length;i++)
- {
- if(ss.xx==sa[i].xx && ss.yy==sa[i].yy)
- {
- return true;
- }
- }
- return false;
- }
- public function getFood(xx:int,yy:int):SnakeBlock
- {
- for(var i:int=0;i<food.length;i++)
- {
- if(food[i].xx ==xx && food[i].yy ==yy)
- {
- return food[i];
- }
- }
- return null;
- }
- public function genFood():void
- {
- if(food.length!=0)
- return;
- for(;food.length <numFood;)
- {
- var xx:int= Math.random()*(blWidth-1);
- var yy:int= Math.random()*(blHeight-1);
- if(getFood(xx,yy) ==null &&!IntersectSnake(xx,yy) )
- {
- var s:SnakeBlock = CreateSprite(1);
- s.x = xx*blocksize;
- s.y = yy*blocksize;
- s.xx = xx;
- s.yy = yy;
- food.push(s);
- }
- }
- }
- public function eatFood():void
- {
- if(ss!=null)
- {
- for(var i:int=0;i<food.length;i++)
- {
- if(food[i].xx ==ss.xx && food[i].yy ==ss.yy)
- {
- AppendSprite();
- if(sa.length>2)
- {
- bonus +=4*(sa.length-2);
- if(tickcount>0)
- {
- bonus +=sa.length/tickcount;
- }
- }
- scores += 10+bonus;
- t.text = "Score: "+scores;
- this.removeChild(food[i]);
- food.splice(i,1);
- return;
- }
- }
- }
- }
- public function cleanup():void
- {
- while(sa.length>0)
- {
- this.removeChild(sa.pop());
- }
- while(food.length>0)
- {
- this.removeChild(food.pop());
- }
- rr.length=0;
- kbq.length=0;
- ss = null;
- scores = 0;
- bonus = 0;
- tickcount = 0;
- }
- public function GameFrame():void
- {
- if(ss!=null)
- {
- tickcount++;
- if(kbq.length>0)
- {
- var c:uint=0;
- c = kbq.pop(); // pick the next turn in the queue (may be undefined if queue is empty)
- // and check that it is not undefined and not a 180 degree turn
- // (annoying to be able to turn into the snake with one key press)
- //
- if (c%2!=ss.r%2)
- {
- rr.push(c); // change current direction to the new value
- }
- }
- upateSnakesPos();
- if(IsGameOver())
- {
- t.text = "Game Over/t-/tPress SPACE";
- ss = null;
- t2.text = food.length.toString();
- return;
- }
- eatFood();
- genFood();
- t2.text = food.length.toString();
- }
- }
- public function onEnterFrame(evt:Event):void
- {
- GameFrame();
- }
- public function onAddtoStage(evt:Event):void
- {
- this.stage.addEventListener(KeyboardEvent.KEY_DOWN,onkeydown);
- this.addEventListener(Event.ENTER_FRAME,onEnterFrame);
- }
- public function SnakeMc()
- {
- addEventListener(Event.ADDED_TO_STAGE ,onAddtoStage);
- super();
- t = new TextField();
- t.x = 1;
- t.y = 255;
- t.width = 511;
- t.height = 16;
- t.text = "Snake Game/t-/tPress SPACE";
- addChild(t);
- t2 = new TextField();
- t2 = new TextField();
- t2.x = 1;
- t2.y = 271;
- t2.width = 511;
- t2.height = 16;
- t2.text = food.length.toString();
- addChild(t2);
- this.graphics.beginFill(0xeeeeee);
- this.graphics.lineStyle(1);
- this.graphics.lineTo(511, 0);
- this.graphics.lineTo(511, 256);
- this.graphics.lineTo(0, 256);
- }
- }
- }
Step 7.
创建G:/javaspace/workspace/SnakeGameFlash/build.bat文件(用把*.as和SnakeGame.mxml编译和生成Flash),内容如下:
- mxmlc --strict=true --file-specs SnakeGame.mxml
- pause
Step 8.
运行G:/javaspace/workspace/SnakeGameFlash/build.bat,生成SnakeGame.swf
Step 9
运行测试SnakeGame.swf.
教程结束