最近买了android,在电车上挺无聊的,给android做了个小游戏玩玩,顺便弄了个flash版
游戏画面如下:
游戏测试URL如下:
http://fsanguo.comoj.com/flash/jump/index.php
这个游戏实现起来很简单,代码也很少,首先需要几个碎图:
因为游戏简单,直接把相应的图做成MC来的比较快
一共以下几个MC
游戏精灵,普通地板,可旋转地板,左移地板,右移地板,弹跳地板,减HP地板
在各个MC内部添加几行代码
/*********可旋转地板**************/
//是否可站立,游戏精灵存在,则开始播放,在第五贞设定为false,并且开始翻转,表示不可站立
var st:Boolean = true;
var fun:Function;
stop();
/*********左移地板**************/
import flash.display.Sprite;
var st:Boolean = true;
var charaIsMove:Boolean = true;
//游戏精灵
var chara:Sprite;
onMove();
function onMove():void{
if(chara != null){
//游戏精灵不为空的时候,将游戏精灵向左移
if(chara.x < this.x || chara.x > this.x + this.width){
chara = null;
st = false;
}else{
chara.x -= 2;
}
}
}
/*********右移地板**************/
import flash.display.Sprite;
var st:Boolean = true;
var charaIsMove:Boolean = true;
//游戏精灵
var chara:Sprite;
onMove();
function onMove():void{
if(chara != null){
//游戏精灵不为空,将精灵向右移
if(chara.x < this.x || chara.x > this.x + this.width){
chara = null;
st = false;
}else{
chara.x += 2;
}
}
}
/*********弹跳地板**************/
import flash.display.Sprite;
var st:Boolean = true;
var charaIsMove:Boolean = true;
var chara:Object;
stop();
function onJump():void{
if(chara != null){
//游戏精灵不为空,则弹跳
chara.jumpSpeed = -15;
chara.jump = true;
chara = null;
}
}
/*********减HP地板**************/
//表示可站立
var st:Boolean = true;
//减HP
var minsHp:Boolean = true;
游戏实现部分,只需要一个类Main,下面是所有代码
package game { import flash.display.Sprite; import flash.utils.getDefinitionByName; import flash.events.Event; import flash.events.MouseEvent; import flash.text.TextField; import flash.events.KeyboardEvent; import flash.display.Shape; import flash.display.DisplayObjectContainer; import flash.system.fscommand; //import flash.desktop.NativeApplication; public class Main extends Sprite{ private var _chara:chara = new chara(); private var _wall:Sprite = new Sprite(); private var _bak:Sprite = new Sprite(); private var _bakImg:bak = new bak(); private var _mai:mai = new mai(); private var _ue:ue = new ue(); private var _hp:hp = new hp(); private var _chara_ctrl:Boolean = false; private var _sectend:int = 0; private var _speed:int = 2; private var _mcList:Array = new Array(); private var _leftCtrl:Boolean = false; private var _rightCtrl:Boolean = false; private var _mcIndex:int = 0; private var _mcArray:Array = new Array(); private var _txt:TextField = new TextField(); private var _hpIndex:int = 1; private var _charaOld:int = 1; private var _shape:Shape = new Shape(); private var _showText:Sprite = new Sprite(); private var _gameStart:Boolean = false; private var _sound:jiao = new jiao(); private var _hpAdd:int = 0; //加入地板 private function getMC(my:int = 480,mx:int = -1):void{ var i:int = Math.floor(Math.random()*_mcArray.length); var ClassReference:Class; ClassReference = getDefinitionByName(_mcArray[i]) as Class; var nmc:Sprite = new ClassReference as Sprite; nmc.y = my - this._wall.y; if(mx < 0){ nmc.x = Math.floor(Math.random()*270) - 50; }else{ nmc.x = mx; } this._mcList.push(nmc); this._bak.addChild(nmc); } public function Main() { //将各种地板加入数组 //普通地板 _mcArray.push("mc"); //可旋转地板 _mcArray.push("toumc"); //左移地板 _mcArray.push("leftMc"); //右移地板 _mcArray.push("rightMc"); //弹跳地板 _mcArray.push("tan"); //减HP地板 _mcArray.push("sMc"); //添加各个画板层 this._bak.addChild(_bakImg); this._wall.addChild(this._bak); this._wall.addChild(this._chara); this._chara.x = 160; this._chara.y = 100; this.addChild(this._wall); this.addChild(_mai); this.addChild(_ue); _hp.x = 20; _hp.y = 460; this.addChild(_hp); this.addChild(_shape); _txt.selectable = false; _txt.x = 20; _txt.y = 430; _txt.width = 200; _txt.height = 200; this.addChild(_txt); this.addChild(_showText); this.addEventListener(MouseEvent.MOUSE_DOWN,onDown); this.addEventListener(MouseEvent.MOUSE_UP,onUp); stage.addEventListener(KeyboardEvent.KEY_DOWN,isKeyDown,false,0,true); stage.addEventListener(KeyboardEvent.KEY_UP,isKeyUp,false,0,true); stage.showDefaultContextMenu = false //fscommand("showmenu", "false"); init(); } //游戏初始化 private function init():void{ this._bak.y = 0; this._wall.y = 0; var i:int; for(i=0;i<_mcList.length;i++){ this._bak.removeChild(_mcList[i]); } _mcList.splice(0,_mcList.length); this._chara.x = 160; this._chara.y = 100; //添加游戏说明框 _shape.graphics.lineStyle(5, 0x000000,0.8); _shape.graphics.beginFill(0x999999,0.6); _shape.graphics.drawRoundRect(50,100,220,300,20,20); _shape.graphics.lineStyle(5, 0x000000,0.8); _shape.graphics.beginFill(0x999999,0.6); _shape.graphics.drawRoundRect(70,300,180,40,10,10); _shape.graphics.lineStyle(5, 0x000000,0.8); _shape.graphics.beginFill(0x999999,0.6); _shape.graphics.drawRoundRect(70,350,180,40,10,10); var txt:TextField = new TextField(); txt.selectable = false; txt.width = 200; txt.height = 400; txt.x = 60; txt.y = 110; txt.htmlText = "<font color='#FFFFFF' size='20'><b>" + " 100階にチャレンジ/n/nあなたができますか?/n/n"+ "操作:/n</b></font>" + "<font color='#FFFFFF' size='15'><b>「←」キーをクリック:/n ・左に移動/n" + "<font color='#FFFFFF' size='15'><b>「→」キーをクリック:/n ・右に移動/n" + "</b></font>"; _showText.addChild(txt); txt = new TextField(); txt.selectable = false; txt.x = 120; txt.y = 305; txt.htmlText = "<font color='#FFFFFF' size='20'><b>" + "スタート"+ "</b></font>"; _showText.addChild(txt); txt = new TextField(); txt.selectable = false; txt.x = 120; txt.y = 355; txt.htmlText = "<font color='#FFFFFF' size='20'><b>" + " 閉じる"+ "</b></font>"; _showText.addChild(txt); //gameStart(); } //游戏结束 private function gameOver():void{ this._chara.jump = false; _leftCtrl = false; _rightCtrl = false; _sound.play(); _hpIndex = 1; _hp.gotoAndStop(_hpIndex); _gameStart = false; _sectend = 0; this.removeEventListener(Event.ENTER_FRAME,onFrame); this._bak.y = 0; this._wall.y = 0; this._bakImg.y = 0; var i:int; for(i=0;i<_mcList.length;i++){ this._bak.removeChild(_mcList[i]); } _mcList.splice(0,_mcList.length); this._chara.x = 160; this._chara.y = 100; _shape.graphics.lineStyle(5, 0x000000,0.8); _shape.graphics.beginFill(0x999999,0.6); _shape.graphics.drawRoundRect(50,100,220,300,20,20); _shape.graphics.lineStyle(5, 0x000000,0.8); _shape.graphics.beginFill(0x999999,0.6); _shape.graphics.drawRoundRect(70,300,180,40,10,10); _shape.graphics.lineStyle(5, 0x000000,0.8); _shape.graphics.beginFill(0x999999,0.6); _shape.graphics.drawRoundRect(70,350,180,40,10,10); var txt:TextField = new TextField(); txt.selectable = false; txt.width = 200; txt.height = 400; txt.x = 60; txt.y = 110; txt.htmlText = "<font color='#FFFFFF' size='20'><b>" + " GAME OVER/n/n"+ "階数:" + _txt.text + "/n称号:" + getName(int(_txt.text)) + "/n</b></font>"; _showText.addChild(txt); txt = new TextField(); txt.selectable = false; txt.x = 120; txt.y = 305; txt.htmlText = "<font color='#FFFFFF' size='20'><b>" + "スタート"+ "</b></font>"; _showText.addChild(txt); txt = new TextField(); txt.selectable = false; txt.x = 120; txt.y = 355; txt.htmlText = "<font color='#FFFFFF' size='20'><b>" + " 閉じる"+ "</b></font>"; _showText.addChild(txt); } //游戏称号 private function getName(cnt:int):String{ if(cnt < 10){ return "赤ちゃん"; }else if(cnt < 20){ return "子供"; }else if(cnt < 30){ return "小学生"; }else if(cnt < 40){ return "中学生"; }else if(cnt < 50){ return "高校生"; }else if(cnt < 60){ return "大学生"; }else if(cnt < 70){ return "大学院生"; }else if(cnt < 80){ return "専門家"; }else if(cnt < 90){ return "高級専門家"; }else{ return "神"; } } //游戏开始 private function gameStart():void{ //在画板上添加3个地板 getMC(300,160); getMC(350); getMC(400); this.addEventListener(Event.ENTER_FRAME,onFrame,false,0,true); } //键盘按下 private function isKeyDown(event:KeyboardEvent):void{ if(!_gameStart){ return; } if(event.keyCode == 37){ if(_leftCtrl){ return; } _leftCtrl = true; _rightCtrl = false; this._chara.gotoAndPlay("left"); }else if(event.keyCode == 39){ if(_rightCtrl){ return; } _rightCtrl = true; _leftCtrl = false; this._chara.gotoAndPlay("right"); } } //键盘弹起 private function isKeyUp(event:KeyboardEvent):void{ if(event.keyCode == 37){ _leftCtrl = false; }else if(event.keyCode == 39){ _rightCtrl = false; } } //鼠标按下 private function onDown(event:MouseEvent):void{ var intX:Number = event.currentTarget.mouseX; var intY:Number = event.currentTarget.mouseY; if(_gameStart){ if(intX <= 160){ if(_leftCtrl){ return; } _leftCtrl = true; _rightCtrl = false; this._chara.gotoAndPlay("left"); }else{ if(_rightCtrl){ return; } _rightCtrl = true; _leftCtrl = false; this._chara.gotoAndPlay("right"); } }else if(intX > 70 && intX < 250 && intY > 300 && intY < 340){ _gameStart = true; _shape.graphics.clear(); this.removeAllChildren(_showText); gameStart(); }else if(intX > 70 && intX < 250 && intY > 350 && intY < 390){ fscommand("quit"); //stage.nativeWindow.close(); //NativeApplication.nativeApplication.exit(); } stage.focus = this; } //删除地板方法 public function removeAllChildren( container:DisplayObjectContainer ):void { var count:int = container.numChildren; for ( var i:int = 0; i < count; i++ ) { container.removeChildAt( 0 ); } } //鼠标弹起 private function onUp(event:MouseEvent):void{ var intX:Number = event.currentTarget.mouseX; if(intX <= 160){ _leftCtrl = false; }else{ _rightCtrl = false; } } //逻辑部分 private function onFrame(event:Event):void{ var speedNum:Number = 0.03; var downlength:Number; var _child:Object; //找到画面外可删除地板,将其删除 if(_mcList.length > 0 && _mcList[0].y - this._wall.y < 40){ _child = _mcList[0] as Object; if(_child.charaIsMove != null && _child.chara != null){ _child.chara = null; } this._bak.removeChild(_mcList[0]); _mcList.splice(0,1); } var i:int; this._wall.y -= _speed; //游戏精灵上升下降,以及上升下降幅度控制 if(!_chara_ctrl){ this._chara.y += _speed; downlength = speedNum*_sectend*_sectend; if(downlength > 37){ downlength = 37; } this._chara.y += downlength; _sectend++; }else if(this._chara.jump){ this._chara.y += _speed; this._chara.y -= 0.06*this._chara.jumpSpeed*this._chara.jumpSpeed; //charaOld = this._chara.y; this._chara.jumpSpeed++; if(this._chara.jumpSpeed >= 0){ this._chara.jump = false; } } //是否处在弹跳阶段 if(!this._chara.jump){ _chara_ctrl = false; }else{ if(this._chara.jumpSpeed >= 0){ this._chara.jump = false; } } //循环地板数组 for(i=0;!this._chara.jump && i<this._mcList.length;i++){ _child = _mcList[i] as Object; //找到精灵接触的地板,这里也可以用hitTestObject来判断 if(this._chara.y >= _child.y && this._chara.x >= _child.x && _child.st && this._chara.x <= _child.x + _child.width && _charaOld <= _child.y+1){ if(_hpAdd > 0 && _charaOld != this._chara.y){ _hpAdd--; if(_hpAdd == 0 && _hpIndex > 1){ _hpIndex--; _hp.gotoAndStop(_hpIndex); } } if(_child.charaIsMove != null && _child.charaIsMove){ _child.chara = this._chara; } if(_child.minsHp != null && _child.minsHp){ _child.minsHp = false; _hpIndex++; _hpAdd = 5; _hp.gotoAndStop(_hpIndex); } this._chara.y = _child.y +1; _sectend = 1; _chara_ctrl = true; _child.play(); break; } } //是否接触到上方的刺针 if(this._chara.hitTestObject(_ue)){ _chara_ctrl = false; this._chara.jump = false; this._chara.y += 30; _sectend = 3; _hpIndex++; _hpAdd = 5; _hp.gotoAndStop(_hpIndex); } //循环背景图片,达到连贯显示 if(this._wall.y % 130 == 0){ _bakImg.y += 130; } //精灵移动 if(_leftCtrl){ this._chara.x -= _speed*2; } if(_rightCtrl){ this._chara.x += _speed*2; } if(this._chara.x<25){ this._chara.x = 25; } if(this._chara.x>295){ this._chara.x = 295; } if(_mcIndex <= 0){ this.getMC(); _mcIndex = Math.floor(Math.random()*60) + 30; } _mcIndex--; //显示楼层 _txt.htmlText = "<font color='#FFFFFF' size='20'>" + Math.floor((-this._wall.y)/480) + "</font>"; _charaOld = this._chara.y; //判断游戏是否结束 if(_hpIndex >= 4){ gameOver(); }else if(this._chara.y + this._wall.y > 540){ gameOver(); } } } }
小游戏完成了,简单吧?这就是flash,开发小游戏,有着绝对的优势
源码放出,加图片,加音效
http://fsanguo.comoj.com/download.php?i=jump.rar