lex rpg游戏的简单构建与分析

    技术2022-05-19  17

          闲来无事,打算做个flex开发的简单网页游戏。首先分析一个简单的小游戏源码。

    首先来看下整个项目的目录,bin-dedug ,html-template,libs为系统自动生成。 

      src下为编译源文件 project 存放as类文件:GamerInfo,GamerInfoB,GetUser,GuanRandom,maPaths,View。GamerInfo为玩家信息类,包含基本属性初始化方法和getInfo方法,下面为其代码 import mx.containers.Canvas; /** * 玩家信息类 * * */ public class GamerInfo extends Canvas { public static var ci:GamerInfo=null; public var herolId:int=0; public var heroName:String; public var hero1Blood:int=1000;//初始生命 public var hero1BloodNow:int=1000;//现在生命 public var hero1Attack:int=20;//攻击力 public var hero1Defense:int=20;//防御力 public var hero1Gold:int=0;//金币 public var hero1YellowKey:int=0;//黄钥匙数量 public var hero1BlueKey:int=0;//蓝钥匙数 public var hero1RedKey:int;//红钥匙数 public function GamerInfo() { heroName=""; herolId=0; hero1Blood=1000; hero1BloodNow=1000; hero1Attack=20; hero1Defense=20; hero1Gold=0; hero1YellowKey=0; hero1BlueKey=0; hero1RedKey=0; } public static function getGamerInfo1():GamerInfo { if(ci==null) { ci=new GamerInfo(); ci.heroName=""; ci.herolId=0; ci.hero1Blood=1000; ci.hero1BloodNow=1000; ci.hero1Attack=20; ci.hero1Defense=20; ci.hero1Gold=0; ci.hero1YellowKey=0; ci.hero1BlueKey=0; ci.hero1RedKey=0; } return ci; } }

    GamerInfoB是GamerInfo的一个拷贝,用于玩家角色2。GetUser用于获取初始化的玩家角色

    import mx.collections.ArrayCollection; public class GetUser { public function GetUser() { //this.userlist=new ArrayCollection(); //this.titleList=new ArrayCollection(); } private static var U:GetUser=null; public var userlist:ArrayCollection; public var TileListdp:ArrayCollection; public var gId:int;//攻击玩家ID public var fId:int;//防御玩家ID public var startbool:Boolean; public static function getUser():GetUser { if(U==null) { U=new GetUser(); U.userlist=new ArrayCollection(); U.gId=0; U.fId=0; U.startbool=false; //U.titleList=new ArrayCollection(); } return U; } }

     

     GuanRandom用于游戏中观世音的随机属性。   import mx.containers.Canvas; public class GuanRandom extends Canvas { public var ran:int=1; public static var guan:GuanRandom=null; public function GuanRandom() { ran=1; } public static function getGuanRandom():GuanRandom { if(guan==null) { guan = new GuanRandom(); guan.ran=1; } return guan; } } maPaths 为加载游戏地图的路径,所对应的是xml文件。

    public class maPaths { public var mapath:String; public function maPaths() { mapath="xml/1.xml"; } }

     

    View类为当前游戏视图对象。 import mx.containers.Canvas; public class View extends Canvas { private static var V:View = null; public var viewIndex:int;//当前楼层 public var DiffculyLevel:int;//当前级别 public var IsCrash:Boolean;//当前是否碰撞上行 public var IsCrashUp:Boolean;//当前是否碰撞下行 public function View() { viewIndex=0; DiffculyLevel=0; IsCrash=true; IsCrashUp=true; } public static function getView():View { if(V == null) { V = new View(); V.viewIndex = 0; V.DiffculyLevel=0; V.IsCrash=true; V.IsCrashUp=true; } return V; } } project文件夹内还包含一个核心的功能文件Checkpost.mxml.包括地图加载,碰撞检测。 <?xml version="1.0" encoding="utf-8"?> <mx:Canvas creationComplete="init()" width="416" height="416" xmlns:mx="http://www.adobe.com/2006/mxml"> <mx:Script> <!--[CDATA[ import mx.events.IndexChangedEvent; import mx.states.RemoveChild; import flash.utils.setTimeout; import mx.managers.PopUpManager; import mx.collections.*; import mx.controls.*; import mx.effects.Move; import mx.rpc.events.ResultEvent; import mx.rpc.http.HTTPService; //观音的随机数 public var ran:GuanRandom=GuanRandom.getGuanRandom(); //获取当前视图索引 public var vie:View= View.getView(); //获取得到玩家及怪物的攻击防御 public var gu:GetUser=GetUser.getUser(); //模式消息窗口 public var message:ShowMess = new ShowMess(); public var boolShowMess:Boolean = true; public var mess:BuyMess = new BuyMess(); public var boolShowBuy:Boolean = true; public var sorry:SorryMess = new SorryMess(); public var guan:GuanShow = new GuanShow(); public var die:DieMess = new DieMess(); public var boolDie:Boolean=true; //绑定地图数据 [Bindable] public var mapPath : String = new String(); [Bindable] public var mp : maPaths = new maPaths(); //获取当前玩家的血液值 public var ci:GamerInfo=GamerInfo.getGamerInfo1(); public var ci2:GamerInfoB = GamerInfoB.getGamerInfo2(); //判断是否被碰撞到一次 public var boo:Boolean=true; public var boo1:Boolean=true; //全局变量,杀蓝色巨人数 public var killBlueNum:int=0; //全局变量,杀黄色巨人数 public var killYellowNum:int=0; //全局变量,红色钥匙 public var imgRedKey:Image = new Image(); public var attack:int; public var defense:int; //当前怪物的攻击防御基数 private var at:int=10; private var de:int=10; public var gridWidth : int; public var gridHeight : int; public var gridWidthCount : int = 13; public var gridHeightCount : int = 13; public var floor:int = 1; public var monstorI:int = 1; public var mapService:HTTPService = new HTTPService(); public var run:Move = new Move(); public var mapXMLList:ArrayCollection; public var eachGridList : ArrayCollection = new ArrayCollection(); public var monstorList:ArrayCollection = new ArrayCollection(); //怪物集合 public var textList:ArrayCollection = new ArrayCollection(); //文本信息集合 public var intA:int=1; //解决BUG小手段A public var intB:int=1; //解决BUG小手段B public var mapTimer:Timer; public var hero1:Image; public var hero2:Image; public var collideTimer : Timer; public var direction:String; public var s:Sound=new Sound(); //检测是否碰撞A public function judgeCollideA(event : TimerEvent):void { for(var loop :int = 0; loop<eachGridList.length; loop++) { if(eachGridList[loop] as Image) { var img:Image = eachGridList[loop]; //碰到黄钥匙 if(img.source=="image/image9.png"&&hero1.x==img.x&&hero1.y==img.y) { if(img.parent==this) { eachGridList.removeItemAt(loop); this.removeChild(img); } else { eachGridList.removeItemAt(loop); this.removeChild(img); } s=new Sound(new URLRequest("sound/sound6.mp3"),null); s.play(); ci.hero1YellowKey++; } //碰到蓝钥匙 else if(img.source=="image/image12.png"&&hero1.x==img.x&&hero1.y==img.y) { if(img.parent==this) { eachGridList.removeItemAt(loop); this.removeChild(img); } else { eachGridList.removeItemAt(loop); this.removeChild(img); } s=new Sound(new URLRequest("sound/sound6.mp3"),null); s.play(); ci.hero1BlueKey++; } //碰到红钥匙 else if(img.source=="image/image15.png"&&hero1.x==img.x&&hero1.y==img.y) { if(img.parent==this) { eachGridList.removeItemAt(loop); this.removeChild(img); } else { eachGridList.removeItemAt(loop); this.removeChild(img); } s=new Sound(new URLRequest("sound/sound6.mp3"),null); s.play(); ci.hero1RedKey++; } //碰到墙 else if(img.source=="image/image25.png") { if(direction=="left"&&hero1.x==img.x&&hero1.y==img.y) { hero1.x=img.x+gridWidth; } else if(direction=="up"&&hero1.x==img.x&&hero1.y==img.y) { hero1.y=img.y+gridHeight; } else if(direction=="right"&&hero1.x==img.x&&hero1.y==img.y) { hero1.x=img.x-gridWidth; } else if(direction=="down"&&hero1.x==img.x&&hero1.y==img.y) { hero1.y=img.y-gridHeight; } } //碰到围栏 else if(img.source=="image/image112.png") { if(direction=="left"&&hero1.x==img.x&&hero1.y==img.y) { hero1.x=img.x+gridWidth; } else if(direction=="up"&&hero1.x==img.x&&hero1.y==img.y) { hero1.y=img.y+gridHeight; } else if(direction=="right"&&hero1.x==img.x&&hero1.y==img.y) { hero1.x=img.x-gridWidth; } else if(direction=="down"&&hero1.x==img.x&&hero1.y==img.y) { hero1.y=img.y-gridHeight; } } //大血瓶 else if(img.source=="image/image78.png"&&hero1.x==img.x&&hero1.y==img.y) { if(img.parent==this) { eachGridList.removeItemAt(loop); this.removeChild(img); } else { eachGridList.removeItemAt(loop); this.removeChild(img); } s=new Sound(new URLRequest("sound/sound6.mp3"),null); s.play(); if((ci.hero1BloodNow+200)<=ci.hero1Blood) { ci.hero1BloodNow+=100; } else { ci.hero1BloodNow=ci.hero1Blood; } } //小血瓶 else if(img.source=="image/image81.png"&&hero1.x==img.x&&hero1.y==img.y) { if(img.parent==this) { eachGridList.removeItemAt(loop); this.removeChild(img); } else { eachGridList.removeItemAt(loop); this.removeChild(img); } s=new Sound(new URLRequest("sound/sound6.mp3"),null); s.play(); if((ci.hero1BloodNow+100)<=ci.hero1Blood) { ci.hero1BloodNow=ci.hero1BloodNow+50; } else { ci.hero1BloodNow=ci.hero1Blood; } } //大宝石 else if(img.source=="image/image84.png"&&hero1.x==img.x&&hero1.y==img.y) { if(img.parent==this) { eachGridList.removeItemAt(loop); this.removeChild(img); } else { eachGridList.removeItemAt(loop); this.removeChild(img); } s=new Sound(new URLRequest("sound/sound6.mp3"),null); s.play(); ci.hero1Gold+=10; ci.hero1Attack+=1; } //小宝石 else if(img.source=="image/image87.png"&&hero1.x==img.x&&hero1.y==img.y) { if(img.parent==this) { eachGridList.removeItemAt(loop); this.removeChild(img); } else { eachGridList.removeItemAt(loop); this.removeChild(img); } s=new Sound(new URLRequest("sound/sound6.mp3"),null); s.play(); ci.hero1Gold+=5; ci.hero1Defense+=1; } //小绿球 else if(img.source=="image/image48.png"||img.source=="image/image50.png") { if(hero1.x==img.x&&hero1.y==img.y) { if(ci.hero1Attack>at&&ci.hero1Defense>de) { ci.hero1BloodNow=ci.hero1BloodNow; } else { ci.hero1BloodNow=ci.hero1BloodNow-20; } if(ci.hero1BloodNow>0) { img.source="image/image480.png"; s=new Sound(new URLRequest("sound/sound1.mp3"),null); s.play(); if(img.parent==this) { eachGridList.removeItemAt(loop); this.removeChild(img); } else { eachGridList.removeItemAt(loop); this.removeChild(img); } } else { if(boolDie) { die=PopUpManager.createPopUp(this,DieMess,true,null) as DieMess; PopUpManager.centerPopUp(die); boolDie=false; die.addEventListener(KeyboardEvent.KEY_DOWN,exit); } } } } //小红球 else if(img.source=="image/image53.png"||img.source=="image/image55.png") { if(hero1.x==img.x&&hero1.y==img.y) { if(ci.hero1Attack>(at+10)&&ci.hero1Defense>(de+10)) { ci.hero1BloodNow=ci.hero1BloodNow; } else { ci.hero1BloodNow=ci.hero1BloodNow-30; } if(ci.hero1BloodNow>0) { img.source="image/image480.png"; s=new Sound(new URLRequest("sound/sound1.mp3"),null); s.play(); if(img.parent==this) { eachGridList.removeItemAt(loop); this.removeChild(img); } else { eachGridList.removeItemAt(loop); this.removeChild(img); } } else { if(boolDie) { die=PopUpManager.createPopUp(this,DieMess,true,null) as DieMess; PopUpManager.centerPopUp(die); boolDie=false; die.addEventListener(KeyboardEvent.KEY_DOWN,exit); } } } } //小蝙蝠 else if(img.source=="image/image58.png"||img.source=="image/image60.png") { if(hero1.x==img.x&&hero1.y==img.y) { if(ci.hero1Attack>(at+15)&&ci.hero1Defense>(de+15)) { ci.hero1BloodNow=ci.hero1BloodNow; } else { ci.hero1BloodNow=ci.hero1BloodNow-40; } if(ci.hero1BloodNow>0) { img.source="image/image480.png"; s=new Sound(new URLRequest("sound/sound1.mp3"),null); s.play(); if(img.parent==this) { eachGridList.removeItemAt(loop); this.removeChild(img); } else { eachGridList.removeItemAt(loop); this.removeChild(img); } } else { if(boolDie) { die=PopUpManager.createPopUp(this,DieMess,true,null) as DieMess; PopUpManager.centerPopUp(die); boolDie=false; die.addEventListener(KeyboardEvent.KEY_DOWN,exit); } } } } //巫婆 else if(img.source=="image/image63.png"||img.source=="image/image65.png") { if(hero1.x==img.x&&hero1.y==img.y) { if(ci.hero1Attack>(at+20)&&ci.hero1Defense>(de+20)) { ci.hero1BloodNow=ci.hero1BloodNow; } else { ci.hero1BloodNow=ci.hero1BloodNow-30; } if(ci.hero1BloodNow>0) { img.source="image/image480.png"; s=new Sound(new URLRequest("sound/sound1.mp3"),null); s.play(); if(img.parent==this) { eachGridList.removeItemAt(loop); this.removeChild(img); } else { eachGridList.removeItemAt(loop); this.removeChild(img); } } else { if(boolDie) { die=PopUpManager.createPopUp(this,DieMess,true,null) as DieMess; PopUpManager.centerPopUp(die); boolDie=false; die.addEventListener(KeyboardEvent.KEY_DOWN,exit); } } } } //小骷髅 else if(img.source=="image/image68.png"||img.source=="image/image70.png") { if(hero1.x==img.x&&hero1.y==img.y) { if(ci.hero1Attack>(at+25)&&ci.hero1Defense>(de+25)) { ci.hero1BloodNow=ci.hero1BloodNow; } else { ci.hero1BloodNow=ci.hero1BloodNow-35; } if(ci.hero1BloodNow>0) { img.source="image/image480.png"; s=new Sound(new URLRequest("sound/sound1.mp3"),null); s.play(); if(img.parent==this) { eachGridList.removeItemAt(loop); this.removeChild(img); } else { eachGridList.removeItemAt(loop); this.removeChild(img); } } else { if(boolDie) { die=PopUpManager.createPopUp(this,DieMess,true,null) as DieMess; PopUpManager.centerPopUp(die); boolDie=false; die.addEventListener(KeyboardEvent.KEY_DOWN,exit); } } } } //武器骷髅 else if(img.source=="image/image73.png"||img.source=="image/image75.png") { if(hero1.x==img.x&&hero1.y==img.y) { if(ci.hero1Attack>(at+30)&&ci.hero1Defense>(de+30)) { ci.hero1BloodNow=ci.hero1BloodNow; } else { ci.hero1BloodNow=ci.hero1BloodNow-40; } if(ci.hero1BloodNow>0) { img.source="image/image480.png"; s=new Sound(new URLRequest("sound/sound1.mp3"),null); s.play(); if(img.parent==this) { eachGridList.removeItemAt(loop); this.removeChild(img); } else { eachGridList.removeItemAt(loop); this.removeChild(img); } } else { if(boolDie) { die=PopUpManager.createPopUp(this,DieMess,true,null) as DieMess; PopUpManager.centerPopUp(die); boolDie=false; die.addEventListener(KeyboardEvent.KEY_DOWN,exit); } } } } //Boss骷髅 else if(img.source=="image/image197.png"||img.source=="image/image199.png") { if(hero1.x==img.x&&hero1.y==img.y) { if(ci.hero1Attack>attack&&ci.hero1Defense>defense) { ci.hero1BloodNow=ci.hero1BloodNow; } else { ci.hero1BloodNow=ci.hero1BloodNow-200; } if(ci.hero1BloodNow>0) { img.source="image/image480.png"; s=new Sound(new URLRequest("sound/sound1.mp3"),null); s.play(); if(img.parent==this) { eachGridList.removeItemAt(loop); this.removeChild(img); } else { eachGridList.removeItemAt(loop); this.removeChild(img); } } else { if(boolDie) { die=PopUpManager.createPopUp(this,DieMess,true,null) as DieMess; PopUpManager.centerPopUp(die); boolDie=false; die.addEventListener(KeyboardEvent.KEY_DOWN,exit); } } } } //蓝色巨人 else if(img.source=="image/image92.png"||img.source=="image/image94.png") { if(hero1.x==img.x&&hero1.y==img.y) { if(ci.hero1Attack>(at+60)&&ci.hero1Defense>(de+60)) { ci.hero1BloodNow=ci.hero1BloodNow; } else { ci.hero1BloodNow=ci.hero1BloodNow-100; } if(ci.hero1BloodNow>0) { img.source="image/image480.png"; s=new Sound(new URLRequest("sound/sound1.mp3"),null); s.play(); if(img.parent==this) { eachGridList.removeItemAt(loop); this.removeChild(img); killBlueNum++; } else { eachGridList.removeItemAt(loop); this.removeChild(img); } } else { if(boolDie) { die=PopUpManager.createPopUp(this,DieMess,true,null) as DieMess; PopUpManager.centerPopUp(die); boolDie=false; die.addEventListener(KeyboardEvent.KEY_DOWN,exit); } } if(killBlueNum==4) { for(var lp :int = 0; lp<eachGridList.length; lp++) { var img1:Image = eachGridList[lp]; if(img1.source=="image/image112.png") { if(img.parent==this) { eachGridList.removeItemAt(lp); this.removeChild(img1); } else { eachGridList.removeItemAt(lp); this.removeChild(img1); } } } } } } //黄色巨人 else if(img.source=="image/image97.png"||img.source=="image/image99.png") { if(hero1.x==img.x&&hero1.y==img.y) { if(ci.hero1Attack>(at+60)&&ci.hero1Defense>(de+60)) { ci.hero1BloodNow=ci.hero1BloodNow; } else { ci.hero1BloodNow=ci.hero1BloodNow-150; } if(ci.hero1BloodNow>0) { img.source="image/image480.png"; s=new Sound(new URLRequest("sound/sound1.mp3"),null); s.play(); if(img.parent==this) { eachGridList.removeItemAt(loop); this.removeChild(img); killYellowNum++; } else { eachGridList.removeItemAt(loop); this.removeChild(img); } } else { if(boolDie) { die=PopUpManager.createPopUp(this,DieMess,true,null) as DieMess; PopUpManager.centerPopUp(die); boolDie=false; die.addEventListener(KeyboardEvent.KEY_DOWN,exit); } } if(killYellowNum==4) { this.addChild(imgRedKey); eachGridList.addItem(imgRedKey); } } } //蓝老头 else if(img.source=="image/image102.png"||img.source=="image/image104.png") { if(hero1.x==img.x&&hero1.y==img.y) { if(boolShowMess) { message = PopUpManager.createPopUp(this, ShowMess, true, null) as ShowMess; PopUpManager.centerPopUp(message); boolShowMess=false; } if(message!=null) { this.addEventListener(KeyboardEvent.KEY_DOWN,exit); } if(img.parent==this) { eachGridList.removeItemAt(loop); this.removeChild(img); } else { eachGridList.removeItemAt(loop); this.removeChild(img); } } } //红老头 else if(img.source=="image/image107.png"||img.source=="image/image109.png") { if(hero1.x==img.x&&hero1.y==img.y) { if(boolShowBuy) { mess = PopUpManager.createPopUp(this, BuyMess, true, null) as BuyMess; PopUpManager.centerPopUp(mess); boolShowBuy=false; } if(mess!=null) { this.addEventListener(KeyboardEvent.KEY_DOWN,exitR); } if(img.parent==this) { eachGridList.removeItemAt(loop); this.removeChild(img); } else { eachGridList.removeItemAt(loop); this.removeChild(img); } } } //观音 else if(img.source=="image/image407.jpg") { if(hero1.x==img.x&&hero1.y==img.y) { if(boolShowMess) { guan = PopUpManager.createPopUp(this, GuanShow, true, null) as GuanShow; PopUpManager.centerPopUp(guan); boolShowMess=false; } if(guan!=null) { this.addEventListener(KeyboardEvent.KEY_DOWN,exit); } if(img.parent==this) { eachGridList.removeItemAt(loop); this.removeChild(img); } else { eachGridList.removeItemAt(loop); this.removeChild(img); } ran.ran=Math.random()*3+1; if(ran.ran==1) { ci.hero1Attack+=10; } else if(ran.ran==2) { ci.hero1Defense+=10; } else if(ran.ran==3) { ci.hero1Blood+=50; ci.hero1BloodNow+=50; } } } //上楼 else if(img.source=="image/image45.png"&&hero1.x==img.x&&hero1.y==img.y) { if(direction=="left") { hero1.x=img.x+gridWidth; } else if(direction=="up") { hero1.y=img.y+gridHeight; } else if(direction=="right") { hero1.x=img.x-gridWidth; } else if(direction=="down") { hero1.y=img.y-gridHeight; } if(vie.IsCrash) { if(vie.viewIndex >0) { vie.viewIndex+=1; } else { vie.viewIndex=1; } vie.IsCrash=false; } else { break; } //Alert.show(viewIndex.toString()); } //下楼 else if(img.source=="image/image42.png"&&hero1.x==img.x&&hero1.y==img.y) { if(direction=="left") { hero1.x=img.x+gridWidth; } else if(direction=="up") { hero1.y=img.y+gridHeight; } else if(direction=="right") { hero1.x=img.x-gridWidth; } else if(direction=="down") { hero1.y=img.y-gridHeight; } if(vie.IsCrashUp) { if(vie.viewIndex==0) { vie.viewIndex=1; } else { vie.viewIndex=vie.viewIndex-1; //vie.IsCrash=false; } vie.IsCrashUp=false; } else { break; } //break; } } //黄门 else if(eachGridList[loop] as Door) { var open:Door = eachGridList[loop]; if(hero1.x==open.x&&hero1.y==open.y&&ci.hero1YellowKey>0) { s=new Sound(new URLRequest("sound/sound2.mp3"),null); s.play(); if(open.parent == this) { eachGridList.removeItemAt(loop); this.removeChild(open); ci.hero1YellowKey--; } else { eachGridList.removeItemAt(loop); this.removeChild(open); } var opDoor:OpenDoor = new OpenDoor(); opDoor.width=gridWidth; opDoor.height=gridHeight; opDoor.x=open.x; opDoor.y=open.y; this.addChild(opDoor); } else if(hero1.x==open.x&&hero1.y==open.y&&ci.hero1YellowKey==0) { if(direction=="left"&&hero1.x==open.x&&hero1.y==open.y) { hero1.x=open.x+gridWidth; } else if(direction=="up"&&hero1.x==open.x&&hero1.y==open.y) { hero1.y=open.y+gridHeight; } else if(direction=="right"&&hero1.x==open.x&&hero1.y==open.y) { hero1.x=open.x-gridWidth; } else if(direction=="down"&&hero1.x==open.x&&hero1.y==open.y) { hero1.y=open.y-gridHeight; } } } //蓝门 else if(eachGridList[loop] as BlueDoor) { var openBlue:BlueDoor = eachGridList[loop]; if(hero1.x==openBlue.x&&hero1.y==openBlue.y&&ci.hero1BlueKey>0) { s=new Sound(new URLRequest("sound/sound2.mp3"),null); s.play(); if(openBlue.parent == this) { eachGridList.removeItemAt(loop); this.removeChild(openBlue); ci.hero1BlueKey--; } else { eachGridList.removeItemAt(loop); this.removeChild(openBlue); } var opBule:OpenBlueDoor = new OpenBlueDoor(); opBule.width=gridWidth; opBule.height=gridHeight; opBule.x=openBlue.x; opBule.y=openBlue.y; this.addChild(opBule); } else if(hero1.x==openBlue.x&&hero1.y==openBlue.y&&ci.hero1BlueKey==0) { if(direction=="left"&&hero1.x==openBlue.x&&hero1.y==openBlue.y) { hero1.x=openBlue.x+gridWidth; } else if(direction=="up"&&hero1.x==openBlue.x&&hero1.y==openBlue.y) { hero1.y=openBlue.y+gridHeight; } else if(direction=="right"&&hero1.x==openBlue.x&&hero1.y==openBlue.y) { hero1.x=openBlue.x-gridWidth; } else if(direction=="down"&&hero1.x==openBlue.x&&hero1.y==openBlue.y) { hero1.y=openBlue.y-gridHeight; } } } //红门 else if(eachGridList[loop] as RedDoor) { var openRed:RedDoor = eachGridList[loop]; if(hero1.x==openRed.x&&hero1.y==openRed.y&&ci.hero1RedKey>0) { s=new Sound(new URLRequest("sound/sound2.mp3"),null); s.play(); if(openRed.parent == this) { eachGridList.removeItemAt(loop); this.removeChild(openRed); ci.hero1RedKey--; } else { eachGridList.removeItemAt(loop); this.removeChild(openRed); } var opRed:OpenRedDoor = new OpenRedDoor(); opRed.width=gridWidth; opRed.height=gridHeight; opRed.x=openRed.x; opRed.y=openRed.y; this.addChild(opRed); } else if(hero1.x==openRed.x&&hero1.y==openRed.y&&ci.hero1RedKey==0) { if(direction=="left"&&hero1.x==openRed.x&&hero1.y==openRed.y) { hero1.x=openRed.x+gridWidth; } else if(direction=="up"&&hero1.x==openRed.x&&hero1.y==openRed.y) { hero1.y=openRed.y+gridHeight; } else if(direction=="right"&&hero1.x==openRed.x&&hero1.y==openRed.y) { hero1.x=openRed.x-gridWidth; } else if(direction=="down"&&hero1.x==openRed.x&&hero1.y==openRed.y) { hero1.y=openRed.y-gridHeight; } } } } } //检测是否碰撞B public function judgeCollideB(event : TimerEvent):void { for(var loop :int = 0; loop<eachGridList.length; loop++) { if(eachGridList[loop] as Image) { var img:Image = eachGridList[loop]; //碰到黄钥匙 if(img.source=="image/image9.png"&&hero2.x==img.x&&hero2.y==img.y) { if(img.parent==this) { eachGridList.removeItemAt(loop); this.removeChild(img); } else { eachGridList.removeItemAt(loop); this.removeChild(img); } s=new Sound(new URLRequest("sound/sound6.mp3"),null); s.play(); ci2.hero2YellowKey++; } //碰到蓝钥匙 else if(img.source=="image/image12.png"&&hero2.x==img.x&&hero2.y==img.y) { if(img.parent==this) { eachGridList.removeItemAt(loop); this.removeChild(img); } else { eachGridList.removeItemAt(loop); this.removeChild(img); } s=new Sound(new URLRequest("sound/sound6.mp3"),null); s.play(); ci2.hero2BlueKey++; } //碰到红钥匙 else if(img.source=="image/image15.png"&&hero2.x==img.x&&hero2.y==img.y) { if(img.parent==this) { eachGridList.removeItemAt(loop); this.removeChild(img); } else { eachGridList.removeItemAt(loop); this.removeChild(img); } s=new Sound(new URLRequest("sound/sound6.mp3"),null); s.play(); ci2.hero2RedKey++; } //碰到墙 else if(img.source=="image/image25.png") { if(direction=="left"&&hero2.x==img.x&&hero2.y==img.y) { hero2.x=img.x+gridWidth; } else if(direction=="up"&&hero2.x==img.x&&hero2.y==img.y) { hero2.y=img.y+gridHeight; } else if(direction=="right"&&hero2.x==img.x&&hero2.y==img.y) { hero2.x=img.x-gridWidth; } else if(direction=="down"&&hero2.x==img.x&&hero2.y==img.y) { hero2.y=img.y-gridHeight; } } //碰到围栏 else if(img.source=="image/image112.png") { if(direction=="left"&&hero2.x==img.x&&hero2.y==img.y) { hero2.x=img.x+gridWidth; } else if(direction=="up"&&hero2.x==img.x&&hero2.y==img.y) { hero2.y=img.y+gridHeight; } else if(direction=="right"&&hero2.x==img.x&&hero2.y==img.y) { hero2.x=img.x-gridWidth; } else if(direction=="down"&&hero2.x==img.x&&hero2.y==img.y) { hero2.y=img.y-gridHeight; } } //大血瓶 else if(img.source=="image/image78.png"&&hero2.x==img.x&&hero2.y==img.y) { if(img.parent==this) { eachGridList.removeItemAt(loop); this.removeChild(img); } else { eachGridList.removeItemAt(loop); this.removeChild(img); } s=new Sound(new URLRequest("sound/sound6.mp3"),null); s.play(); if((ci2.hero2BloodNow+200)<=ci2.hero2Blood) { ci2.hero2BloodNow+=100; } else { ci2.hero2BloodNow=ci2.hero2Blood; } } //小血瓶 else if(img.source=="image/image81.png"&&hero2.x==img.x&&hero2.y==img.y) { if(img.parent==this) { eachGridList.removeItemAt(loop); this.removeChild(img); } else { eachGridList.removeItemAt(loop); this.removeChild(img); } s=new Sound(new URLRequest("sound/sound6.mp3"),null); s.play(); if((ci2.hero2BloodNow+100)<=ci2.hero2Blood) { ci2.hero2BloodNow=ci2.hero2BloodNow+50; } else { ci2.hero2BloodNow=ci2.hero2Blood; } } //大宝石 else if(img.source=="image/image84.png"&&hero2.x==img.x&&hero2.y==img.y) { if(img.parent==this) { eachGridList.removeItemAt(loop); this.removeChild(img); } else { eachGridList.removeItemAt(loop); this.removeChild(img); } s=new Sound(new URLRequest("sound/sound6.mp3"),null); s.play(); ci2.hero2Gold+=10; ci2.hero2Attack+=1; } //小宝石 else if(img.source=="image/image87.png"&&hero2.x==img.x&&hero2.y==img.y) { if(img.parent==this) { eachGridList.removeItemAt(loop); this.removeChild(img); } else { eachGridList.removeItemAt(loop); this.removeChild(img); } s=new Sound(new URLRequest("sound/sound6.mp3"),null); s.play(); ci2.hero2Gold+=5; ci2.hero2Defense+=1; } //小绿球 else if(img.source=="image/image48.png"||img.source=="image/image50.png") { if(hero2.x==img.x&&hero2.y==img.y) { if(ci2.hero2Attack>at&&ci2.hero2Defense>de) { ci2.hero2BloodNow=ci2.hero2BloodNow; } else { ci2.hero2BloodNow=ci2.hero2BloodNow-20; } if(ci2.hero2BloodNow>0) { img.source="image/image480.png"; s=new Sound(new URLRequest("sound/sound1.mp3"),null); s.play(); if(img.parent==this) { eachGridList.removeItemAt(loop); this.removeChild(img); } else { eachGridList.removeItemAt(loop); this.removeChild(img); } } else { if(boolDie) { die=PopUpManager.createPopUp(this,DieMess,true,null) as DieMess; PopUpManager.centerPopUp(die); boolDie=false; die.addEventListener(KeyboardEvent.KEY_DOWN,exit); } } } } //小红球 else if(img.source=="image/image53.png"||img.source=="image/image55.png") { if(hero2.x==img.x&&hero2.y==img.y) { if(ci2.hero2Attack>(at+10)&&ci2.hero2Defense>(de+10)) { ci2.hero2BloodNow=ci2.hero2BloodNow; } else { ci2.hero2BloodNow=ci2.hero2BloodNow-30; } if(ci2.hero2BloodNow>0) { img.source="image/image480.png"; s=new Sound(new URLRequest("sound/sound1.mp3"),null); s.play(); if(img.parent==this) { eachGridList.removeItemAt(loop); this.removeChild(img); } else { eachGridList.removeItemAt(loop); this.removeChild(img); } } else { if(boolDie) { die=PopUpManager.createPopUp(this,DieMess,true,null) as DieMess; PopUpManager.centerPopUp(die); boolDie=false; die.addEventListener(KeyboardEvent.KEY_DOWN,exit); } } } } //小蝙蝠 else if(img.source=="image/image58.png"||img.source=="image/image60.png") { if(hero2.x==img.x&&hero2.y==img.y) { if(ci2.hero2Attack>(at+15)&&ci2.hero2Defense>(de+15)) { ci2.hero2BloodNow=ci2.hero2BloodNow; } else { ci2.hero2BloodNow=ci2.hero2BloodNow-40; } if(ci2.hero2BloodNow>0) { img.source="image/image480.png"; s=new Sound(new URLRequest("sound/sound1.mp3"),null); s.play(); if(img.parent==this) { eachGridList.removeItemAt(loop); this.removeChild(img); } else { eachGridList.removeItemAt(loop); this.removeChild(img); } } else { if(boolDie) { die=PopUpManager.createPopUp(this,DieMess,true,null) as DieMess; PopUpManager.centerPopUp(die); boolDie=false; die.addEventListener(KeyboardEvent.KEY_DOWN,exit); } } } } //巫婆 else if(img.source=="image/image63.png"||img.source=="image/image65.png") { if(hero2.x==img.x&&hero2.y==img.y) { if(ci2.hero2Attack>(at+20)&&ci2.hero2Defense>(de+20)) { ci2.hero2BloodNow=ci2.hero2BloodNow; } else { ci2.hero2BloodNow=ci2.hero2BloodNow-30; } if(ci2.hero2BloodNow>0) { img.source="image/image480.png"; s=new Sound(new URLRequest("sound/sound1.mp3"),null); s.play(); if(img.parent==this) { eachGridList.removeItemAt(loop); this.removeChild(img); } else { eachGridList.removeItemAt(loop); this.removeChild(img); } } else { if(boolDie) { die=PopUpManager.createPopUp(this,DieMess,true,null) as DieMess; PopUpManager.centerPopUp(die); boolDie=false; die.addEventListener(KeyboardEvent.KEY_DOWN,exit); } } } } //小骷髅 else if(img.source=="image/image68.png"||img.source=="image/image70.png") { if(hero2.x==img.x&&hero2.y==img.y) { if(ci2.hero2Attack>(at+25)&&ci2.hero2Defense>(de+25)) { ci2.hero2BloodNow=ci2.hero2BloodNow; } else { ci2.hero2BloodNow=ci2.hero2BloodNow-35; } if(ci2.hero2BloodNow>0) { img.source="image/image480.png"; s=new Sound(new URLRequest("sound/sound1.mp3"),null); s.play(); if(img.parent==this) { eachGridList.removeItemAt(loop); this.removeChild(img); } else { eachGridList.removeItemAt(loop); this.removeChild(img); } } else { if(boolDie) { die=PopUpManager.createPopUp(this,DieMess,true,null) as DieMess; PopUpManager.centerPopUp(die); boolDie=false; die.addEventListener(KeyboardEvent.KEY_DOWN,exit); } } } } //武器骷髅 else if(img.source=="image/image73.png"||img.source=="image/image75.png") { if(hero2.x==img.x&&hero2.y==img.y) { if(ci2.hero2Attack>(at+30)&&ci2.hero2Defense>(de+30)) { ci2.hero2BloodNow=ci2.hero2BloodNow; } else { ci2.hero2BloodNow=ci2.hero2BloodNow-40; } if(ci2.hero2BloodNow>0) { img.source="image/image480.png"; s=new Sound(new URLRequest("sound/sound1.mp3"),null); s.play(); if(img.parent==this) { eachGridList.removeItemAt(loop); this.removeChild(img); } else { eachGridList.removeItemAt(loop); this.removeChild(img); } } else { if(boolDie) { die=PopUpManager.createPopUp(this,DieMess,true,null) as DieMess; PopUpManager.centerPopUp(die); boolDie=false; die.addEventListener(KeyboardEvent.KEY_DOWN,exit); } } } } //Boss骷髅 else if(img.source=="image/image197.png"||img.source=="image/image199.png") { if(hero2.x==img.x&&hero2.y==img.y) { if(ci2.hero2Attack>attack&&ci2.hero2Defense>defense) { ci2.hero2BloodNow=ci2.hero2BloodNow; } else { ci2.hero2BloodNow=ci2.hero2BloodNow-200; } if(ci2.hero2BloodNow>0) { img.source="image/image480.png"; s=new Sound(new URLRequest("sound/sound1.mp3"),null); s.play(); if(img.parent==this) { eachGridList.removeItemAt(loop); this.removeChild(img); } else { eachGridList.removeItemAt(loop); this.removeChild(img); } } else { if(boolDie) { die=PopUpManager.createPopUp(this,DieMess,true,null) as DieMess; PopUpManager.centerPopUp(die); boolDie=false; die.addEventListener(KeyboardEvent.KEY_DOWN,exit); } } } } //蓝色巨人 else if(img.source=="image/image92.png"||img.source=="image/image94.png") { if(hero2.x==img.x&&hero2.y==img.y) { if(ci2.hero2Attack>(at+60)&&ci2.hero2Defense>(de+60)) { ci2.hero2BloodNow=ci2.hero2BloodNow; } else { ci2.hero2BloodNow=ci2.hero2BloodNow-100; } if(ci2.hero2BloodNow>0) { img.source="image/image480.png"; s=new Sound(new URLRequest("sound/sound1.mp3"),null); s.play(); if(img.parent==this) { eachGridList.removeItemAt(loop); this.removeChild(img); killBlueNum++; } else { eachGridList.removeItemAt(loop); this.removeChild(img); } } else { if(boolDie) { die=PopUpManager.createPopUp(this,DieMess,true,null) as DieMess; PopUpManager.centerPopUp(die); boolDie=false; die.addEventListener(KeyboardEvent.KEY_DOWN,exit); } } if(killBlueNum==4) { for(var lp :int = 0; lp<eachGridList.length; lp++) { var img1:Image = eachGridList[lp]; if(img1.source=="image/image112.png") { if(img.parent==this) { eachGridList.removeItemAt(lp); this.removeChild(img1); } else { eachGridList.removeItemAt(lp); this.removeChild(img1); } } } } } } //黄色巨人 else if(img.source=="image/image97.png"||img.source=="image/image99.png") { if(hero2.x==img.x&&hero2.y==img.y) { if(ci2.hero2Attack>(at+60)&&ci2.hero2Defense>(de+60)) { ci2.hero2BloodNow=ci2.hero2BloodNow; } else { ci2.hero2BloodNow=ci2.hero2BloodNow-150; } if(ci2.hero2BloodNow>0) { img.source="image/image480.png"; s=new Sound(new URLRequest("sound/sound1.mp3"),null); s.play(); if(img.parent==this) { eachGridList.removeItemAt(loop); this.removeChild(img); killYellowNum++; } else { eachGridList.removeItemAt(loop); this.removeChild(img); } } else { if(boolDie) { die=PopUpManager.createPopUp(this,DieMess,true,null) as DieMess; PopUpManager.centerPopUp(die); boolDie=false; die.addEventListener(KeyboardEvent.KEY_DOWN,exit); } } if(killYellowNum==4) { this.addChild(imgRedKey); eachGridList.addItem(imgRedKey); } } } //蓝老头 else if(img.source=="image/image102.png"||img.source=="image/image104.png") { if(hero2.x==img.x&&hero2.y==img.y) { if(boolShowMess) { message = PopUpManager.createPopUp(this, ShowMess, true, null) as ShowMess; PopUpManager.centerPopUp(message); boolShowMess=false; } if(message!=null) { this.addEventListener(KeyboardEvent.KEY_DOWN,exit); } if(img.parent==this) { eachGridList.removeItemAt(loop); this.removeChild(img); } else { eachGridList.removeItemAt(loop); this.removeChild(img); } } } //红老头 else if(img.source=="image/image107.png"||img.source=="image/image109.png") { if(hero2.x==img.x&&hero2.y==img.y) { if(boolShowBuy) { mess = PopUpManager.createPopUp(this, BuyMess, true, null) as BuyMess; PopUpManager.centerPopUp(mess); boolShowBuy=false; } if(mess!=null) { this.addEventListener(KeyboardEvent.KEY_DOWN,exitR); } if(img.parent==this) { eachGridList.removeItemAt(loop); this.removeChild(img); } else { eachGridList.removeItemAt(loop); this.removeChild(img); } } } //观音 else if(img.source=="image/image407.jpg") { if(hero2.x==img.x&&hero2.y==img.y) { if(boolShowMess) { guan = PopUpManager.createPopUp(this, GuanShow, true, null) as GuanShow; PopUpManager.centerPopUp(guan); boolShowMess=false; } if(guan!=null) { this.addEventListener(KeyboardEvent.KEY_DOWN,exit); } if(img.parent==this) { eachGridList.removeItemAt(loop); this.removeChild(img); } else { eachGridList.removeItemAt(loop); this.removeChild(img); } ran.ran=Math.random()*3+1; if(ran.ran==1) { ci2.hero2Attack+=10; } else if(ran.ran==2) { ci2.hero2Defense+=10; } else if(ran.ran==3) { ci2.hero2Blood+=50; ci2.hero2BloodNow+=50; } } } //上楼 else if(img.source=="image/image45.png"&&hero2.x==img.x&&hero2.y==img.y) { if(direction=="left") { hero2.x=img.x+gridWidth; } else if(direction=="up") { hero2.y=img.y+gridHeight; } else if(direction=="right") { hero2.x=img.x-gridWidth; } else if(direction=="down") { hero2.y=img.y-gridHeight; } if(vie.IsCrash) { if(vie.viewIndex >0) { vie.viewIndex+=1; } else { vie.viewIndex=1; } vie.IsCrash=false; } else { break; } //Alert.show(viewIndex.toString()); } //下楼 else if(img.source=="image/image42.png"&&hero1.x==img.x&&hero1.y==img.y) { if(direction=="left") { hero2.x=img.x+gridWidth; } else if(direction=="up") { hero2.y=img.y+gridHeight; } else if(direction=="right") { hero2.x=img.x-gridWidth; } else if(direction=="down") { hero2.y=img.y-gridHeight; } if(vie.IsCrashUp) { if(vie.viewIndex==0) { vie.viewIndex=1; } else { vie.viewIndex=vie.viewIndex-1; //vie.IsCrash=false; } vie.IsCrashUp=false; } else { break; } //break; } } //黄门 else if(eachGridList[loop] as Door) { var open:Door = eachGridList[loop]; if(hero2.x==open.x&&hero2.y==open.y&&ci2.hero2YellowKey>0) { s=new Sound(new URLRequest("sound/sound2.mp3"),null); s.play(); if(open.parent == this) { eachGridList.removeItemAt(loop); this.removeChild(open); ci2.hero2YellowKey--; } else { eachGridList.removeItemAt(loop); this.removeChild(open); } var opDoor:OpenDoor = new OpenDoor(); opDoor.width=gridWidth; opDoor.height=gridHeight; opDoor.x=open.x; opDoor.y=open.y; this.addChild(opDoor); } else if(hero2.x==open.x&&hero2.y==open.y&&ci2.hero2YellowKey==0) { if(direction=="left"&&hero2.x==open.x&&hero2.y==open.y) { hero2.x=open.x+gridWidth; } else if(direction=="up"&&hero2.x==open.x&&hero2.y==open.y) { hero2.y=open.y+gridHeight; } else if(direction=="right"&&hero2.x==open.x&&hero2.y==open.y) { hero2.x=open.x-gridWidth; } else if(direction=="down"&&hero2.x==open.x&&hero2.y==open.y) { hero2.y=open.y-gridHeight; } } } //蓝门 else if(eachGridList[loop] as BlueDoor) { var openBlue:BlueDoor = eachGridList[loop]; if(hero2.x==openBlue.x&&hero2.y==openBlue.y&&ci2.hero2BlueKey>0) { s=new Sound(new URLRequest("sound/sound2.mp3"),null); s.play(); if(openBlue.parent == this) { eachGridList.removeItemAt(loop); this.removeChild(openBlue); ci2.hero2BlueKey--; } else { eachGridList.removeItemAt(loop); this.removeChild(openBlue); } var opBule:OpenBlueDoor = new OpenBlueDoor(); opBule.width=gridWidth; opBule.height=gridHeight; opBule.x=openBlue.x; opBule.y=openBlue.y; this.addChild(opBule); } else if(hero2.x==openBlue.x&&hero2.y==openBlue.y&&ci2.hero2BlueKey==0) { if(direction=="left"&&hero2.x==openBlue.x&&hero2.y==openBlue.y) { hero2.x=openBlue.x+gridWidth; } else if(direction=="up"&&hero2.x==openBlue.x&&hero2.y==openBlue.y) { hero2.y=openBlue.y+gridHeight; } else if(direction=="right"&&hero2.x==openBlue.x&&hero2.y==openBlue.y) { hero2.x=openBlue.x-gridWidth; } else if(direction=="down"&&hero2.x==openBlue.x&&hero2.y==openBlue.y) { hero2.y=openBlue.y-gridHeight; } } } //红门 else if(eachGridList[loop] as RedDoor) { var openRed:RedDoor = eachGridList[loop]; if(hero2.x==openRed.x&&hero2.y==openRed.y&&ci2.hero2RedKey>0) { s=new Sound(new URLRequest("sound/sound2.mp3"),null); s.play(); if(openRed.parent == this) { eachGridList.removeItemAt(loop); this.removeChild(openRed); ci2.hero2RedKey--; } else { eachGridList.removeItemAt(loop); this.removeChild(openRed); } var opRed:OpenRedDoor = new OpenRedDoor(); opRed.width=gridWidth; opRed.height=gridHeight; opRed.x=openRed.x; opRed.y=openRed.y; this.addChild(opRed); } else if(hero2.x==openRed.x&&hero2.y==openRed.y&&ci2.hero2RedKey==0) { if(direction=="left"&&hero2.x==openRed.x&&hero2.y==openRed.y) { hero2.x=openRed.x+gridWidth; } else if(direction=="up"&&hero2.x==openRed.x&&hero2.y==openRed.y) { hero2.y=openRed.y+gridHeight; } else if(direction=="right"&&hero2.x==openRed.x&&hero2.y==openRed.y) { hero2.x=openRed.x-gridWidth; } else if(direction=="down"&&hero2.x==openRed.x&&hero2.y==openRed.y) { hero2.y=openRed.y-gridHeight; } } } } } //监听碰撞 public function collideTimerStart():void{ collideTimer = new Timer(1,0); if(vie.DiffculyLevel==0) { collideTimer.addEventListener(TimerEvent.TIMER,judgeCollideA); collideTimer.addEventListener(TimerEvent.TIMER,judgeCollideB); } else { collideTimer.addEventListener(TimerEvent.TIMER,judgeCollideA); } collideTimer.start(); } //初始化分辨率 public function initResolution():void { gridWidth=this.width/gridWidthCount; gridHeight=this.height/gridHeightCount; } //退出模式窗口 public function exit(event:KeyboardEvent):void { PopUpManager.removePopUp(message); PopUpManager.removePopUp(sorry); PopUpManager.removePopUp(guan); PopUpManager.removePopUp(die); } public function exitR(event:KeyboardEvent):void { if(event.keyCode==89) { if((vie.viewIndex+1)==2&&ci.hero1Gold>=500) { ci.hero1YellowKey+=5; ci.hero1Gold-=500; PopUpManager.removePopUp(mess); } else if((vie.viewIndex+1)==6&&ci.hero1Gold>=500) { ci.hero1BlueKey+=1; ci.hero1Gold-=300; PopUpManager.removePopUp(mess); } else { PopUpManager.removePopUp(mess); sorry=PopUpManager.createPopUp(this,SorryMess,true,null) as SorryMess; PopUpManager.centerPopUp(sorry); this.addEventListener(KeyboardEvent.KEY_DOWN,exit); boolShowBuy=true; } } else { PopUpManager.removePopUp(mess); boolShowBuy=true; } } //界面初始化 public function init():void { //Alert.show(vie.viewIndex.toString()); initResolution(); getMap(); this.addEventListener(MouseEvent.CLICK,clickHander); intilGame(); collideTimerStart(); monstorTimerStart(); } /* *加载游戏关卡 */ public function intilGame():void { if(vie.DiffculyLevel==0) { this.addEventListener(KeyboardEvent.KEY_DOWN,moveHeroA); this.addEventListener(KeyboardEvent.KEY_DOWN,moveHeroB); } else if(vie.DiffculyLevel==1) { this.addEventListener(KeyboardEvent.KEY_DOWN,moveHeroA); } else { //获取防御方 var obj:Object; for(var i:int=0,loop:int=gu.userlist.length;i<loop;i++) { var obg:Object=gu.userlist[i]; if(obg["uID"]==gu.fId) { obj=obg; break; } } attack=obj["uAttack"]; defense=obj["uDefense"]; if(attack>20 && attack<40) { at=15; } else if(attack>40 && attack<60) { at=20; } else if(attack>60 && attack<80) { at=25; } if(defense>20 && defense<40) { de=15; } else if(defense>40 && defense<60) { de=20; } else if(defense>60 && defense<80) { de=25; } this.addEventListener(KeyboardEvent.KEY_DOWN,moveHeroA); } } public function clickHander(e:MouseEvent):void { stage.focus=this; } //怪物图标Timer public function monstorTimerStart():void { var tt:Timer = new Timer(300,0); tt.addEventListener(TimerEvent.TIMER,monstorTimer); tt.start(); } //地图怪物跳动 public function monstorTimer(event:TimerEvent):void { monstorI++; for(var loop :int = 0; loop<monstorList.length; loop++) { if(monstorList[loop] as Image&&loop%2==0) { var img:Image = monstorList[loop]; var img2:Image = monstorList[loop+1]; if(monstorI%2==0) { img.alpha=0;img2.alpha=100; } else if(monstorI%2!=0) { img.alpha=100;img2.alpha=0; } } } } //键盘移动 public function moveHeroA(event:KeyboardEvent):void { intA++; if((event.keyCode==37||event.keyCode==38||event.keyCode==39||event.keyCode==40)) { if(event.keyCode==37) { if(intA%2==1) { hero1.x=hero1.x-hero1.width; direction="left"; hero1.source="image/image462.png"; } else { hero1.source="image/image462.png"; } } else if(event.keyCode==38) { if(intA%2==1) { hero1.y=hero1.y-hero1.height; direction="up"; hero1.source="image/image478.png"; } else { hero1.source="image/image478.png"; } } else if(event.keyCode==39) { if(intA%2==1) { hero1.x=hero1.x+hero1.width; direction="right"; hero1.source="image/image470.png"; } else { hero1.source="image/image470.png"; } } else if(event.keyCode==40) { if(intA%2==1) { hero1.y=hero1.y+hero1.height; direction="down"; hero1.source="image/image18.png"; } else { hero1.source="image/image18.png"; } } } else if(event.keyCode==27) { } if(intA>100) { intA=0; } } //键盘移动 public function moveHeroB(event:KeyboardEvent):void { intB++; if(event.keyCode==65||event.keyCode==87||event.keyCode==68||event.keyCode==83) { if(event.keyCode==65) { if(intB%2==1) { hero2.x=hero2.x-hero2.width; direction="left"; hero2.source="image/image464.png"; } else { hero2.source="image/image464.png"; } } else if(event.keyCode==87) { if(intB%2==1) { hero2.y=hero2.y-hero2.height; direction="up"; hero2.source="image/image474.png"; } else { hero2.source="image/image474.png"; } } else if(event.keyCode==68) { if(intB%2==1) { hero2.x=hero2.x+hero2.width; direction="right"; hero2.source="image/image468.png"; } else { hero2.source="image/image468.png"; } } else if(event.keyCode==83) { if(intB%2==1) { hero2.y=hero2.y+hero2.height; direction="down"; hero2.source="image/image19.png"; } else { hero2.source="image/image19.png"; } } } if(intB>100) { intB=0; } } /* *地图加载 */ public function getMap():void { //var mp:maPaths=new maPaths(); mapService.url=mp.mapath; mapService.method="post"; mapService.addEventListener(ResultEvent.RESULT,showMap); mapService.send(); } /* *加载地图 */ public function showMap(event:ResultEvent):void { if(event.result.maps.map as ArrayCollection) { mapXMLList = event.result.maps.map; } else { mapXMLList = new ArrayCollection(); mapXMLList.addItem(event.result.maps.map); } //Alert.show(vie.DiffculyLevel.toString()); var mapRowList:ArrayCollection = mapXMLList[vie.DiffculyLevel].row; for(var row:int=0;row<mapRowList.length;row++) { var colStr:String = mapRowList[row]; for(var col:int=0;col<colStr.length;col++) { if(colStr.charAt(col)=="5") { var img2:Image = new Image(); img2.width=gridWidth; img2.height=gridHeight; img2.x=gridWidth*col; img2.y=gridHeight*row; img2.source="image/image25.png"; this.addChild(img2); eachGridList.addItem(img2); } else if(colStr.charAt(col)=="4") { var img1:Image = new Image(); img1.width=gridWidth; img1.height=gridHeight; img1.x=gridWidth*col; img1.y=gridHeight*row; img1.source="image/image21.png"; eachGridList.addItem(img1); } else if(colStr.charAt(col)=="D") { var img4:Image = new Image(); img4.width=gridWidth; img4.height=gridHeight; img4.x=gridWidth*col; img4.y=gridHeight*row; img4.source="image/image45.png"; this.addChild(img4); eachGridList.addItem(img4); } else if(colStr.charAt(col)=="C") { var img15:Image = new Image(); img15.width=gridWidth; img15.height=gridHeight; img15.x=gridWidth*col; img15.y=gridHeight*row; img15.source="image/image42.png"; this.addChild(img15); eachGridList.addItem(img15); } else if(colStr.charAt(col)=="1") { var img5:Image = new Image(); img5.width=gridWidth; img5.height=gridHeight; img5.x=gridWidth*col; img5.y=gridHeight*row; img5.source="image/image9.png"; this.addChild(img5); eachGridList.addItem(img5); } else if(colStr.charAt(col)=="h") { var img6:Image = new Image(); img6.width=gridWidth; img6.height=gridHeight; img6.x=gridWidth*col; img6.y=gridHeight*row; img6.source="image/image81.png"; this.addChild(img6); eachGridList.addItem(img6); } else if(colStr.charAt(col)=="H") { var img7:Image = new Image(); img7.width=gridWidth; img7.height=gridHeight; img7.x=gridWidth*col; img7.y=gridHeight*row; img7.source="image/image78.png"; this.addChild(img7); eachGridList.addItem(img7); } else if(colStr.charAt(col)=="m") { var img13:Image = new Image(); img13.width=gridWidth; img13.height=gridHeight; img13.x=gridWidth*col; img13.y=gridHeight*row; img13.source="image/image87.png"; this.addChild(img13); eachGridList.addItem(img13); } else if(colStr.charAt(col)=="M") { var img14:Image = new Image(); img14.width=gridWidth; img14.height=gridHeight; img14.x=gridWidth*col; img14.y=gridHeight*row; img14.source="image/image84.png"; this.addChild(img14); eachGridList.addItem(img14); } else if(colStr.charAt(col)=="6") { var dr:Door = new Door(); dr.width=gridWidth; dr.height=gridHeight; dr.x=gridWidth*col; dr.y=gridHeight*row; this.addChild(dr); eachGridList.addItem(dr); } else if(colStr.charAt(col)=="A") { hero1 = new Image(); hero1.source="image/image18.png"; hero1.x=gridWidth*col; hero1.y=gridHeight*row; hero1.width=gridWidth; hero1.height=gridHeight; this.addChild(hero1); eachGridList.addItem(hero1); } else if(colStr.charAt(col)=="B") { hero2 = new Image(); hero2.source="image/image19.png"; hero2.x=gridWidth*col; hero2.y=gridHeight*row; hero2.width=gridWidth; hero2.height=gridHeight; this.addChild(hero2); eachGridList.addItem(hero2); } else if(colStr.charAt(col)=="E") { var img3:Image = new Image(); img3.width=gridWidth; img3.height=gridHeight; img3.x=gridWidth*col; img3.y=gridHeight*row; img3.source="image/image50.png"; this.addChild(img3); eachGridList.addItem(img3); var img3b:Image = new Image(); img3b.width=gridWidth; img3b.height=gridHeight; img3b.x=gridWidth*col; img3b.y=gridHeight*row; img3b.source="image/image48.png"; img3b.alpha=0; this.addChild(img3b); eachGridList.addItem(img3b); monstorList.addItem(img3); monstorList.addItem(img3b); } else if(colStr.charAt(col)=="F") { var img8:Image = new Image(); img8.width=gridWidth; img8.height=gridHeight; img8.x=gridWidth*col; img8.y=gridHeight*row; img8.source="image/image53.png"; this.addChild(img8); eachGridList.addItem(img8); var img8b:Image = new Image(); img8b.width=gridWidth; img8b.height=gridHeight; img8b.x=gridWidth*col; img8b.y=gridHeight*row; img8b.source="image/image55.png"; img8b.alpha=0; this.addChild(img8b); eachGridList.addItem(img8b); monstorList.addItem(img8); monstorList.addItem(img8b); } else if(colStr.charAt(col)=="G") { var img9:Image = new Image(); img9.width=gridWidth; img9.height=gridHeight; img9.x=gridWidth*col; img9.y=gridHeight*row; img9.source="image/image58.png"; this.addChild(img9); eachGridList.addItem(img9); var img9b:Image = new Image(); img9b.width=gridWidth; img9b.height=gridHeight; img9b.x=gridWidth*col; img9b.y=gridHeight*row; img9b.source="image/image60.png"; img9b.alpha=0; this.addChild(img9b); eachGridList.addItem(img9b); monstorList.addItem(img9); monstorList.addItem(img9b); } else if(colStr.charAt(col)=="I") { var img10:Image = new Image(); img10.width=gridWidth; img10.height=gridHeight; img10.x=gridWidth*col; img10.y=gridHeight*row; img10.source="image/image63.png"; this.addChild(img10); eachGridList.addItem(img10); var img10b:Image = new Image(); img10b.width=gridWidth; img10b.height=gridHeight; img10b.x=gridWidth*col; img10b.y=gridHeight*row; img10b.source="image/image65.png"; img10b.alpha=0; this.addChild(img10b); eachGridList.addItem(img10b); monstorList.addItem(img10); monstorList.addItem(img10b); } else if(colStr.charAt(col)=="J") { var img11:Image = new Image(); img11.width=gridWidth; img11.height=gridHeight; img11.x=gridWidth*col; img11.y=gridHeight*row; img11.source="image/image68.png"; this.addChild(img11); eachGridList.addItem(img11); var img11b:Image = new Image(); img11b.width=gridWidth; img11b.height=gridHeight; img11b.x=gridWidth*col; img11b.y=gridHeight*row; img11b.source="image/image70.png"; img11b.alpha=0; this.addChild(img11b); eachGridList.addItem(img11b); monstorList.addItem(img11); monstorList.addItem(img11b); } else if(colStr.charAt(col)=="K") { var img12:Image = new Image(); img12.width=gridWidth; img12.height=gridHeight; img12.x=gridWidth*col; img12.y=gridHeight*row; img12.source="image/image73.png"; this.addChild(img12); eachGridList.addItem(img12); var img12b:Image = new Image(); img12b.width=gridWidth; img12b.height=gridHeight; img12b.x=gridWidth*col; img12b.y=gridHeight*row; img12b.source="image/image75.png"; img12b.alpha=0; this.addChild(img12b); eachGridList.addItem(img12b); monstorList.addItem(img12); monstorList.addItem(img12b); } //红老头 else if(colStr.charAt(col)=="2") { var pic3:Image = new Image(); pic3.width=gridWidth; pic3.height=gridHeight; pic3.x=gridWidth*col; pic3.y=gridHeight*row; pic3.source="image/image107.png"; this.addChild(pic3); eachGridList.addItem(pic3); var pic3b:Image = new Image(); pic3b.width=gridWidth; pic3b.height=gridHeight; pic3b.x=gridWidth*col; pic3b.y=gridHeight*row; pic3b.source="image/image109.png"; pic3b.alpha=0; this.addChild(pic3b); eachGridList.addItem(pic3b); monstorList.addItem(pic3); monstorList.addItem(pic3b); } //蓝老头 else if(colStr.charAt(col)=="3") { var pic4:Image = new Image(); pic4.width=gridWidth; pic4.height=gridHeight; pic4.x=gridWidth*col; pic4.y=gridHeight*row; pic4.source="image/image102.png"; this.addChild(pic4); eachGridList.addItem(pic4); var pic4b:Image = new Image(); pic4b.width=gridWidth; pic4b.height=gridHeight; pic4b.x=gridWidth*col; pic4b.y=gridHeight*row; pic4b.source="image/image104.png"; pic4b.alpha=0; this.addChild(pic4b); eachGridList.addItem(pic4b); monstorList.addItem(pic4); monstorList.addItem(pic4b); } //观音 else if(colStr.charAt(col)=="g") { var pic1:Image = new Image(); pic1.source="image/image407.jpg"; pic1.x=gridWidth*col; pic1.y=gridHeight*row; pic1.width=gridWidth; pic1.height=gridHeight; this.addChild(pic1); eachGridList.addItem(pic1); } //栅栏门 else if(colStr.charAt(col)=="S") { var pic2:Image = new Image(); pic2.source="image/image112.png"; pic2.x=gridWidth*col; pic2.y=gridHeight*row; pic2.width=gridWidth; pic2.height=gridHeight; this.addChild(pic2); eachGridList.addItem(pic2); } //紫门 else if(colStr.charAt(col)=="L") { var dr1:BlueDoor = new BlueDoor(); dr1.width=gridWidth; dr1.height=gridHeight; dr1.x=gridWidth*col; dr1.y=gridHeight*row; this.addChild(dr1); eachGridList.addItem(dr1); } //紫钥匙 else if(colStr.charAt(col)=="l") { var pic5:Image = new Image(); pic5.source="image/image12.png"; pic5.x=gridWidth*col; pic5.y=gridHeight*row; pic5.width=gridWidth; pic5.height=gridHeight; this.addChild(pic5); eachGridList.addItem(pic5); } //红门 else if(colStr.charAt(col)=="N") { var dr2:RedDoor = new RedDoor(); dr2.width=gridWidth; dr2.height=gridHeight; dr2.x=gridWidth*col; dr2.y=gridHeight*row; this.addChild(dr2); eachGridList.addItem(dr2); } //红钥匙 else if(colStr.charAt(col)=="n") { imgRedKey.source="image/image15.png"; imgRedKey.x=gridWidth*col; imgRedKey.y=gridHeight*row; imgRedKey.width=gridWidth; imgRedKey.height=gridHeight; } //j 紫机器人 else if(colStr.charAt(col)=="j") { var pic15:Image = new Image(); pic15.width=gridWidth; pic15.height=gridHeight; pic15.x=gridWidth*col; pic15.y=gridHeight*row; pic15.source="image/image92.png"; this.addChild(pic15); eachGridList.addItem(pic15); var pic15b:Image = new Image(); pic15b.width=gridWidth; pic15b.height=gridHeight; pic15b.x=gridWidth*col; pic15b.y=gridHeight*row; pic15b.source="image/image94.png"; pic15b.alpha=0; this.addChild(pic15b); eachGridList.addItem(pic15b); monstorList.addItem(pic15); monstorList.addItem(pic15b); } //黄骷髅 else if(colStr.charAt(col)=="k") { var pic16:Image = new Image(); pic16.width=gridWidth; pic16.height=gridHeight; pic16.x=gridWidth*col; pic16.y=gridHeight*row; pic16.source="image/image197.png"; this.addChild(pic16); eachGridList.addItem(pic16); var pic16b:Image = new Image(); pic16b.width=gridWidth; pic16b.height=gridHeight; pic16b.x=gridWidth*col; pic16b.y=gridHeight*row; pic16b.source="image/image199.png"; pic16b.alpha=0; this.addChild(pic16b); eachGridList.addItem(pic16b); monstorList.addItem(pic16); monstorList.addItem(pic16b); } //o 黄机器人 else if(colStr.charAt(col)=="o") { var pic17:Image = new Image(); pic17.width=gridWidth; pic17.height=gridHeight; pic17.x=gridWidth*col; pic17.y=gridHeight*row; pic17.source="image/image97.png"; this.addChild(pic17); eachGridList.addItem(pic17); var pic17b:Image = new Image(); pic17b.width=gridWidth; pic17b.height=gridHeight; pic17b.x=gridWidth*col; pic17b.y=gridHeight*row; pic17b.source="image/image99.png"; pic17b.alpha=0; this.addChild(pic17b); eachGridList.addItem(pic17b); monstorList.addItem(pic17); monstorList.addItem(pic17b); } } } } ]]--> </mx:Script> </mx:Canvas> service内包含Constant,LoginService两个AS文件,用于处理用户登录. public class Constant { public static const BASE_URL:String="http://localhost:8081/mmt_Web"; public function Constant() { } } import mx.rpc.http.HTTPService; /* *登陆服务类 */ public class LoginService { public static var http:HTTPService=new HTTPService(); public function LoginService() { } public static function intil():void { http.url= Constant.BASE_URL+"/LoginUserAction"; http.method="post"; } } 下面来分析整个flex应用的入口文件:test.mxml <?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" borderColor="#ECF3F5" color="#F3F7F8" themeColor="#EFF5F8"> <mx:Style> ToolTip { font-size:16; } Alert { font-size:18; } </mx:Style> <mx:Script> <!--[CDATA[ import flash.net.navigateToURL; import mx.managers.PopUpManager; import mx.effects.*; public function labelMouseOver(event:MouseEvent,id:int):void { var g:Glow=new Glow(); g.target=event.target; g.blurXTo=300; g.duration=200; g.play(); var blur:Blur=new Blur(); blur.target=event.target; blur.blurXTo=50; blur.duration=200; blur.play(); var blur2: Blur=new Blur(); blur2.target=event.target; blur2.blurXTo=0; blur2.play(); if(id==1) { a.setStyle("color","#23E5DA"); } else if(id==2) { b.setStyle("color","#23E5DA"); } else if(id==3) { c.setStyle("color","#23E5DA"); } else if(id==4) { d.setStyle("color","#23E5DA"); } var s:Sound=new Sound(new URLRequest("sound/Wubba.mp3"),null); s.play(); } public function labelMouseOut(event:MouseEvent,id:int):void { var g:Glow=new Glow(); g.target=event.target; g.blurXTo=300; g.duration=200; g.play(); var blur:Blur=new Blur(); blur.target=event.target; blur.blurXTo=50; blur.duration=200; blur.play(); var blur2: Blur=new Blur(); blur2.target=event.target; blur2.blurXTo=0; blur2.play(); if(id==1) { a.setStyle("color","white"); } else if(id==2) { b.setStyle("color","white"); } else if(id==3) { c.setStyle("color","white"); } else if(id==4) { d.setStyle("color","white"); } var s:Sound=new Sound(new URLRequest("sound/Wubba.mp3"),null); s.play(); } public function display(i:int):void { if(i==1) { trace("1111"); var pt:PlayType = new PlayType(); this.bg.visible=false; this.addChild(pt); } else if(i==3) { var os:OrderShow=new OrderShow(); os=PopUpManager.createPopUp(this,OrderShow,true,null) as OrderShow; PopUpManager.centerPopUp(os); } } ]]--> </mx:Script> <mx:Canvas id="bg" width="672" height="416" backgroundColor="#3B3B41" horizontalCenter="9" verticalCenter="0"> <mx:Label text="魔 塔" width="312" height="62" fontFamily="Verdan a" fontSize="36" textAlign="center" color="#E30822" fontWeight="bold" horizontalCenter="0" verticalCenter="-122"/> <mx:VBox width="118" height="154" horizontalCenter="14" verticalCenter="9" textAlign="center"> <mx:Label text="开始游戏" id="a" mouseOver="labelMouseOver(event,1)" mouseOut="labelMouseOut(event,1)" click="display(1)" textAlign="center" width="90" fontSize="20" color="white"/> <mx:Label text="读取进度" id="b" mouseOver="labelMouseOver(event,2)" mouseOut="labelMouseOut(event,2)" textAlign="center" width="90" fontSize="20" color="white"/> <mx:Label text="排行榜" id="c" mouseOver="labelMouseOver(event,3)" mouseOut="labelMouseOut(event,3)" textAlign="center" width="90" fontSize="20" color="white"/> <mx:Label text="游戏说明" id="d" mouseOver="labelMouseOver(event,4)" mouseOut="labelMouseOut(event,4)" textAlign="center" width="90" fontSize="20" color="white"/> </mx:VBox> <mx:Label x="512" y="359" text="出品人:许涛组" width="128" height="35" textAlign="left" fontSize="16" color="#39CAEE"/> </mx:Canvas> </mx:Application> 当用户点击 开始游戏 的label,调用display(1) var pt:PlayType = new PlayType(); this.bg.visible=false; this.addChild(pt); 该函数首先创建一个新的视图PlayType,然后将当前视图背景置为不可见,然后将新视图添加到sprite容器内。 PlayType: <?xml version="1.0" encoding="utf-8"?> <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="100%" height="100%"> <mx:Script> <!--[CDATA[ import project.View; import mx.effects.*; import mx.controls.*; public var vi:View=View.getView(); public function mouseOver(event:MouseEvent,id:int):void { var g:Glow=new Glow(); g.target=event.target; g.blurXTo=300; g.duration=200; g.play(); var blur:Blur=new Blur(); blur.target=event.target; blur.blurXTo=50; blur.duration=200; blur.play(); var blur2: Blur=new Blur(); blur2.target=event.target; blur2.blurXTo=0; blur2.play(); if(id==1) { a.setStyle("color","#23E5DA"); } else if(id==2) { b.setStyle("color","#23E5DA"); } else if(id==3) { c.setStyle("color","#23E5DA"); } var s:Sound=new Sound(new URLRequest("sound/Wubba.mp3"),null); s.play(); } public function mouseOut(event:MouseEvent,id:int):void { var g:Glow=new Glow(); g.target=event.target; g.blurXTo=300; g.duration=200; g.play(); var blur:Blur=new Blur(); blur.target=event.target; blur.blurXTo=50; blur.duration=200; blur.play(); var blur2: Blur=new Blur(); blur2.target=event.target; blur2.blurXTo=0; blur2.play(); if(id==1) { a.setStyle("color","white"); } else if(id==2) { b.setStyle("color","white"); } else if(id==3) { c.setStyle("color","red"); } var s:Sound=new Sound(new URLRequest("sound/Wubba.mp3"),null); s.play(); } public function cl(index:int):void { if(index==0) { vi.DiffculyLevel=0; var C:Game = new Game(); this.addChild(C); } else if(index==1) { vi.DiffculyLevel=1; var cm:Game=new Game(); this.addChild(cm); } else if(index==2) { var us:UserLogin=new UserLogin(); this.bg.visible=false; this.addChild(us); } } ]]--> </mx:Script> <mx:Canvas id="bg" width="672" height="416" backgroundColor="#3B3B41" horizontalCenter="9" verticalCenter="0"> <mx:Label text="游戏方式" textAlign="center" fontSize="36" color="#DA092C" horizontalCenter="7" verticalCenter="-103"/> <mx:VBox width="118" height="154" horizontalCenter="23" verticalCenter="35" textAlign="center"> <mx:Label text="单人模式" id="a" mouseOver="mouseOver(event,1)" mouseOut="mouseOut(event,1)" click="cl(1)" textAlign="center" width="90" fontSize="20" color="white"/> <mx:Label text="多人模式" id="b" mouseOver="mouseOver(event,2)" mouseOut="mouseOut(event,2)" click="cl(0)" textAlign="center" width="90" fontSize="20" color="white"/> <mx:Label text="攻塔模式" id="c" mouseOver="mouseOver(event,3)" mouseOut="mouseOut(event,3)" click="cl(2)" textAlign="center" width="90" fontSize="20" color="white"/> </mx:VBox> </mx:Canvas> </mx:Canvas> 当用户点击 单人模式label时调用cl(0)函数, if(index==0) { vi.DiffculyLevel=0; var C:Game = new Game(); this.addChild(C); } 该函数首先将游戏视图索引置为0层,然后创建Game视图并添加到sprite容器内 Game为进入游戏的主界面: <?xml version="1.0" encoding="utf-8"?> <mx:Canvas creationComplete="init()" width="100%" height="100%" xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:project="project.*"> <mx:Style> .text { text-align:left; font-size:24; color:blue; } </mx:Style> <mx:Script> <!--[CDATA[ import project.*; import project.View; import project.maPaths; import mx.managers.PopUpManager; import project.*; import mx.events.IndexChangedEvent; import mx.collections.*; import mx.controls.*; import mx.effects.Move; import mx.rpc.events.ResultEvent; import mx.rpc.http.HTTPService; public var vie:View= View.getView(); public var ci:GamerInfo=GamerInfo.getGamerInfo1(); public var ci2:GamerInfoB=GamerInfoB.getGamerInfo2(); public var mp:maPaths=new maPaths(); public var gridWidth : int; public var gridHeight : int; public var gridWidthCount : int = 13; public var gridHeightCount : int = 13; public var floor:int = 1; public var monstorI:int = 1; public var mapService:HTTPService = new HTTPService(); public var run:Move = new Move();//实现move接口 public var mapXMLList:ArrayCollection;//地图集合 public var eachGridList : ArrayCollection = new ArrayCollection();//每个框架集合 public var monstorList:ArrayCollection = new ArrayCollection(); //怪物集合 public var textList:ArrayCollection = new ArrayCollection(); //文本信息集合 public var intA:int=1; //解决BUG小手段A public var intB:int=1; //解决BUG小手段B public var mapTimer:Timer;//地图计时器 public var hero1:Image;//玩家1 public var hero2:Image;//玩家2 public var collideTimer : Timer;//碰撞计时器 public var direction:String;//方向 public var tim:Timer=new Timer(10,0);//改变背景与楼层对应 //界面初始化 public function init():void { getNum(); //getLR(); if(vie.DiffculyLevel==1||vie.DiffculyLevel==2) { show.visible=true; } else { show.visible=false; } //创建 var tim2:Timer=new Timer(1000,0); tim2.addEventListener(TimerEvent.TIMER,changIndex); tim2.start(); tim.addEventListener(TimerEvent.TIMER,addLayer); this.addEventListener(KeyboardEvent.KEY_DOWN,exit); } /* *开始监听是否上下 并且获得角色基本信息 */ public function getNum():void { var t:Timer=new Timer(10,0); t.addEventListener(TimerEvent.TIMER,getLR); t.start(); } //由tim2来检测楼层与背景对应情况 发现不对应立即调用tim计时器 public function changIndex(e:TimerEvent):void { if(this.bg.selectedIndex!=vie.viewIndex) { //Alert.show("aaaa"+"---"+vie.viewIndex); tim.start(); } } public function exit(e:KeyboardEvent):void { if(e.keyCode==27) { var se:ShowExit=new ShowExit(); se=PopUpManager.createPopUp(this,ShowExit,true,null) as ShowExit; PopUpManager.centerPopUp(se); } } public function getLR(e:TimerEvent):void { showFloor.text = "第"+(vie.viewIndex+1)+"层"; //英雄A信息 showBloodA.text = "生命:"+ci.hero1BloodNow.toString(); showAttackA.text = "攻击:"+ci.hero1Attack.toString(); showDefenseA.text = "防御:"+ci.hero1Defense.toString(); showGoldA.text = "金币:"+ci.hero1Gold.toString(); showKeyA.text = "钥匙:"; showYellowKeyA.text = ci.hero1YellowKey.toString(); showBlueKeyA.text = ci.hero1BlueKey.toString(); showRedKeyA.text = ci.hero1RedKey.toString(); //英雄B信息 showBloodB.text = "生命:"+ci2.hero2BloodNow.toString(); showAttackB.text = "攻击:"+ci2.hero2Attack.toString(); showDefenseB.text = "防御:"+ci2.hero2Defense.toString(); showGoldB.text = "金币:"+ci2.hero2Gold.toString(); showKeyB.text = "钥匙:"; showYellowKeyB.text = ci2.hero2YellowKey.toString(); showBlueKeyB.text = ci2.hero2BlueKey.toString(); showRedKeyB.text = ci2.hero2RedKey.toString(); } public function addLayer(e:TimerEvent):void { vie.IsCrash=true; vie.IsCrashUp=true; var cCurrent:Checkpost = null;//当前checkpost if(this.bg.selectedIndex<vie.viewIndex)//对应上楼 { if(this.bg.contains(this.bg.getChildAt(vie.viewIndex))) { var ca:Checkpost=new Checkpost(); mp.mapath="xml/"+(vie.viewIndex+1)+".xml";//更换楼层地图 ca.mp = mp; bg.addChild(ca); this.stage.focus=ca; bg.selectedIndex = bg.selectedIndex + 1; cCurrent = this.bg.selectedChild as Checkpost; cCurrent.setFocus(); } else { this.bg.selectedIndex=this.bg.selectedIndex+1; cCurrent = this.bg.selectedChild as Checkpost; cCurrent.setFocus(); } } else//对应下楼 { this.bg.selectedIndex=this.bg.selectedIndex-1; this.stage.focus=this.bg.selectedChild; cCurrent = this.bg.selectedChild as Checkpost; cCurrent.setFocus(); } tim.stop(); //模式窗口 //PopUpManager.createPopUp(this,car1,true,null); } ]]--> </mx:Script> <mx:ViewStack backgroundImage="image/imgBG.png" id="bg" width="416" height="416" horizontalCenter="7" verticalCenter="0"> <project:Checkpost id="car1" label="第一层"> </project:Checkpost> </mx:ViewStack> <mx:Canvas backgroundImage="image/imageLR.jpg" id="left" width="128" height="416" horizontalCenter="-263" verticalCenter="0"> <mx:Text styleName="text" id="showFloor" text="" width="82" height="39" textAlign="center" horizontalCenter="-12" verticalCenter="-79"/> <mx:Text width="62" height="28" text="2P:" fontSize="22" textAlign="center" horizontalCenter="0" verticalCenter="-80"/> <mx:Text id="showBloodB" x="33" y="160" width="85" height="22" fontSize="14"/> <mx:Text id="showAttackB" x="33" y="180" width="85" height="22" fontSize="14"/> <mx:Text id="showDefenseB" x="33" y="200" width="85" height="22" fontSize="14"/> <mx:Text id="showGoldB" x="33" y="220" width="85" height="22" fontSize="14"/> <mx:Text id="showKeyB" x="33" y="240" width="85" height="22" fontSize="14"/> <mx:Canvas width="96" height="136" backgroundColor="#443E3E" alpha="0.88" horizontalCenter="6" verticalCenter="130"> <mx:Image x="10" y="10" source="image/image9.png"/> <mx:Image x="10" y="50" source="image/image12.png"/> <mx:Image x="10" y="94" source="image/image15.png"/> <mx:Label id="showYellowKeyB" x="50" y="24" text="" fontSize="14" color="#8EE2F4"/> <mx:Label id="showBlueKeyB" x="50" y="64" text="" fontSize="14" color="#8EE2F4"/> <mx:Label id="showRedKeyB" x="50" y="108" text="" fontSize="14" color="#8EE2F4"/> </mx:Canvas> <mx:Image id="show" x="0" y="114" width="128" height="302" source="image/splash.png"/> </mx:Canvas> <mx:Canvas backgroundImage="image/imageLR.jpg" id="right" width="135" height="416" horizontalCenter="280" verticalCenter="0"> <mx:Text width="62" height="28" text="1P:" fontSize="22" textAlign="center" horizontalCenter="0" verticalCenter="-80"/> <mx:Text id="showBloodA" x="33" y="150" width="85" height="32" fontSize="14"/> <mx:Text id="showAttackA" x="33" y="180" width="85" height="22" fontSize="14"/> <mx:Text id="showDefenseA" x="33" y="200" width="85" height="22" fontSize="14"/> <mx:Text id="showGoldA" x="33" y="220" width="85" height="22" fontSize="14"/> <mx:Text id="showKeyA" x="33" y="240" width="85" height="22" fontSize="14"/> <mx:Canvas width="96" height="136" backgroundColor="#443E3E" horizontalCenter="9" verticalCenter="110" alpha="0.88"> <mx:Image x="10" y="10" source="image/image9.png"/> <mx:Image x="10" y="50" source="image/image12.png"/> <mx:Image x="10" y="94" source="image/image15.png"/> <mx:Label id="showYellowKeyA" x="50" y="24" text="" fontSize="14" color="#8EE2F4"/> <mx:Label id="showBlueKeyA" x="50" y="64" text="" fontSize="14" color="#8EE2F4"/> <mx:Label id="showRedKeyA" x="50" y="108" text="" fontSize="14" color="#8EE2F4"/> </mx:Canvas> </mx:Canvas> <!---<mx:Canvas id="bottom" width="128" height="50" x="500" y="550"> <mx:LinkBar color="#0000ff" fontWeight="bold" dataProvider="{bg}"/> </mx:Canvas>--> </mx:Canvas> 当系统完成创建该视图的时候会调用creationComplete="init()"方法

    public function init():void { getNum(); //getLR(); if(vie.DiffculyLevel==1||vie.DiffculyLevel==2) { show.visible=true; } else { show.visible=false; } //创建 var tim2:Timer=new Timer(1000,0); tim2.addEventListener(TimerEvent.TIMER,changIndex); tim2.start(); tim.addEventListener(TimerEvent.TIMER,addLayer); this.addEventListener(KeyboardEvent.KEY_DOWN,exit); }

      该初始化方法内首先调用getNum()方法 public function getNum():void { var t:Timer=new Timer(10,0); t.addEventListener(TimerEvent.TIMER,getLR); t.start(); } 该方法手下一个延时10毫秒促发事件的定时器t,事件为调用getLR()函数来为编辑控件定时刷新玩家基本信息.然后检测视图索引,如果是第一层或第二层,将背景置为可见,其他情况置为不可见。然后创建tim2计时器,每过1S调用changIndex()方法,由tim2来检测楼层与背景对应情况 发现不对应立即调用tim计时器,而tim计时器又每过10ms调用addLayer()方法,将背景索引和当前视图索引比较,如发现不对应则将bg.selectedIndex 改变,再装换为Checkpost视图对象 public function addLayer(e:TimerEvent):void { vie.IsCrash=true; vie.IsCrashUp=true; var cCurrent:Checkpost = null;//当前checkpost if(this.bg.selectedIndex<vie.viewIndex)//对应上楼 { if(this.bg.contains(this.bg.getChildAt(vie.viewIndex))) { var ca:Checkpost=new Checkpost(); mp.mapath="xml/"+(vie.viewIndex+1)+".xml";//更换楼层地图 ca.mp = mp; bg.addChild(ca); this.stage.focus=ca; bg.selectedIndex = bg.selectedIndex + 1; cCurrent = this.bg.selectedChild as Checkpost; cCurrent.setFocus(); } else { this.bg.selectedIndex=this.bg.selectedIndex+1; cCurrent = this.bg.selectedChild as Checkpost; cCurrent.setFocus(); } } else//对应下楼 { this.bg.selectedIndex=this.bg.selectedIndex-1; this.stage.focus=this.bg.selectedChild; cCurrent = this.bg.selectedChild as Checkpost; cCurrent.setFocus(); } tim.stop(); //模式窗口 //PopUpManager.createPopUp(this,car1,true,null); }  游戏地图的加载使用xml解析并将图片元件赋值给一个以屏幕分辨率作为单元的二维数组:   public function showMap(event:ResultEvent):void { if(event.result.maps.map as ArrayCollection) { mapXMLList = event.result.maps.map; } else { mapXMLList = new ArrayCollection(); mapXMLList.addItem(event.result.maps.map); } //Alert.show(vie.DiffculyLevel.toString()); var mapRowList:ArrayCollection = mapXMLList[vie.DiffculyLevel].row; for(var row:int=0;row<mapRowList.length;row++) { var colStr:String = mapRowList[row]; for(var col:int=0;col<colStr.length;col++) { if(colStr.charAt(col)=="5") { var img2:Image = new Image(); img2.width=gridWidth; img2.height=gridHeight; img2.x=gridWidth*col; img2.y=gridHeight*row; img2.source="image/image25.png"; this.addChild(img2); eachGridList.addItem(img2); } else if(colStr.charAt(col)=="4") { var img1:Image = new Image(); img1.width=gridWidth; img1.height=gridHeight; img1.x=gridWidth*col; img1.y=gridHeight*row; img1.source="image/image21.png"; eachGridList.addItem(img1); } else if(colStr.charAt(col)=="D") { var img4:Image = new Image(); img4.width=gridWidth; img4.height=gridHeight; img4.x=gridWidth*col; img4.y=gridHeight*row; img4.source="image/image45.png"; this.addChild(img4); eachGridList.addItem(img4); } else if(colStr.charAt(col)=="C") { var img15:Image = new Image(); img15.width=gridWidth; img15.height=gridHeight; img15.x=gridWidth*col; img15.y=gridHeight*row; img15.source="image/image42.png"; this.addChild(img15); eachGridList.addItem(img15); } else if(colStr.charAt(col)=="1") { var img5:Image = new Image(); img5.width=gridWidth; img5.height=gridHeight; img5.x=gridWidth*col; img5.y=gridHeight*row; img5.source="image/image9.png"; this.addChild(img5); eachGridList.addItem(img5); } else if(colStr.charAt(col)=="h") { var img6:Image = new Image(); img6.width=gridWidth; img6.height=gridHeight; img6.x=gridWidth*col; img6.y=gridHeight*row; img6.source="image/image81.png"; this.addChild(img6); eachGridList.addItem(img6); } else if(colStr.charAt(col)=="H") { var img7:Image = new Image(); img7.width=gridWidth; img7.height=gridHeight; img7.x=gridWidth*col; img7.y=gridHeight*row; img7.source="image/image78.png"; this.addChild(img7); eachGridList.addItem(img7); } else if(colStr.charAt(col)=="m") { var img13:Image = new Image(); img13.width=gridWidth; img13.height=gridHeight; img13.x=gridWidth*col; img13.y=gridHeight*row; img13.source="image/image87.png"; this.addChild(img13); eachGridList.addItem(img13); } else if(colStr.charAt(col)=="M") { var img14:Image = new Image(); img14.width=gridWidth; img14.height=gridHeight; img14.x=gridWidth*col; img14.y=gridHeight*row; img14.source="image/image84.png"; this.addChild(img14); eachGridList.addItem(img14); } else if(colStr.charAt(col)=="6") { var dr:Door = new Door(); dr.width=gridWidth; dr.height=gridHeight; dr.x=gridWidth*col; dr.y=gridHeight*row; this.addChild(dr); eachGridList.addItem(dr); } else if(colStr.charAt(col)=="A") { hero1 = new Image(); hero1.source="image/image18.png"; hero1.x=gridWidth*col; hero1.y=gridHeight*row; hero1.width=gridWidth; hero1.height=gridHeight; this.addChild(hero1); eachGridList.addItem(hero1); } else if(colStr.charAt(col)=="B") { hero2 = new Image(); hero2.source="image/image19.png"; hero2.x=gridWidth*col; hero2.y=gridHeight*row; hero2.width=gridWidth; hero2.height=gridHeight; this.addChild(hero2); eachGridList.addItem(hero2); } else if(colStr.charAt(col)=="E") { var img3:Image = new Image(); img3.width=gridWidth; img3.height=gridHeight; img3.x=gridWidth*col; img3.y=gridHeight*row; img3.source="image/image50.png"; this.addChild(img3); eachGridList.addItem(img3); var img3b:Image = new Image(); img3b.width=gridWidth; img3b.height=gridHeight; img3b.x=gridWidth*col; img3b.y=gridHeight*row; img3b.source="image/image48.png"; img3b.alpha=0; this.addChild(img3b); eachGridList.addItem(img3b); monstorList.addItem(img3); monstorList.addItem(img3b); } else if(colStr.charAt(col)=="F") { var img8:Image = new Image(); img8.width=gridWidth; img8.height=gridHeight; img8.x=gridWidth*col; img8.y=gridHeight*row; img8.source="image/image53.png"; this.addChild(img8); eachGridList.addItem(img8); var img8b:Image = new Image(); img8b.width=gridWidth; img8b.height=gridHeight; img8b.x=gridWidth*col; img8b.y=gridHeight*row; img8b.source="image/image55.png"; img8b.alpha=0; this.addChild(img8b); eachGridList.addItem(img8b); monstorList.addItem(img8); monstorList.addItem(img8b); } else if(colStr.charAt(col)=="G") { var img9:Image = new Image(); img9.width=gridWidth; img9.height=gridHeight; img9.x=gridWidth*col; img9.y=gridHeight*row; img9.source="image/image58.png"; this.addChild(img9); eachGridList.addItem(img9); var img9b:Image = new Image(); img9b.width=gridWidth; img9b.height=gridHeight; img9b.x=gridWidth*col; img9b.y=gridHeight*row; img9b.source="image/image60.png"; img9b.alpha=0; this.addChild(img9b); eachGridList.addItem(img9b); monstorList.addItem(img9); monstorList.addItem(img9b); } else if(colStr.charAt(col)=="I") { var img10:Image = new Image(); img10.width=gridWidth; img10.height=gridHeight; img10.x=gridWidth*col; img10.y=gridHeight*row; img10.source="image/image63.png"; this.addChild(img10); eachGridList.addItem(img10); var img10b:Image = new Image(); img10b.width=gridWidth; img10b.height=gridHeight; img10b.x=gridWidth*col; img10b.y=gridHeight*row; img10b.source="image/image65.png"; img10b.alpha=0; this.addChild(img10b); eachGridList.addItem(img10b); monstorList.addItem(img10); monstorList.addItem(img10b); } else if(colStr.charAt(col)=="J") { var img11:Image = new Image(); img11.width=gridWidth; img11.height=gridHeight; img11.x=gridWidth*col; img11.y=gridHeight*row; img11.source="image/image68.png"; this.addChild(img11); eachGridList.addItem(img11); var img11b:Image = new Image(); img11b.width=gridWidth; img11b.height=gridHeight; img11b.x=gridWidth*col; img11b.y=gridHeight*row; img11b.source="image/image70.png"; img11b.alpha=0; this.addChild(img11b); eachGridList.addItem(img11b); monstorList.addItem(img11); monstorList.addItem(img11b); } else if(colStr.charAt(col)=="K") { var img12:Image = new Image(); img12.width=gridWidth; img12.height=gridHeight; img12.x=gridWidth*col; img12.y=gridHeight*row; img12.source="image/image73.png"; this.addChild(img12); eachGridList.addItem(img12); var img12b:Image = new Image(); img12b.width=gridWidth; img12b.height=gridHeight; img12b.x=gridWidth*col; img12b.y=gridHeight*row; img12b.source="image/image75.png"; img12b.alpha=0; this.addChild(img12b); eachGridList.addItem(img12b); monstorList.addItem(img12); monstorList.addItem(img12b); } //红老头 else if(colStr.charAt(col)=="2") { var pic3:Image = new Image(); pic3.width=gridWidth; pic3.height=gridHeight; pic3.x=gridWidth*col; pic3.y=gridHeight*row; pic3.source="image/image107.png"; this.addChild(pic3); eachGridList.addItem(pic3); var pic3b:Image = new Image(); pic3b.width=gridWidth; pic3b.height=gridHeight; pic3b.x=gridWidth*col; pic3b.y=gridHeight*row; pic3b.source="image/image109.png"; pic3b.alpha=0; this.addChild(pic3b); eachGridList.addItem(pic3b); monstorList.addItem(pic3); monstorList.addItem(pic3b); } //蓝老头 else if(colStr.charAt(col)=="3") { var pic4:Image = new Image(); pic4.width=gridWidth; pic4.height=gridHeight; pic4.x=gridWidth*col; pic4.y=gridHeight*row; pic4.source="image/image102.png"; this.addChild(pic4); eachGridList.addItem(pic4); var pic4b:Image = new Image(); pic4b.width=gridWidth; pic4b.height=gridHeight; pic4b.x=gridWidth*col; pic4b.y=gridHeight*row; pic4b.source="image/image104.png"; pic4b.alpha=0; this.addChild(pic4b); eachGridList.addItem(pic4b); monstorList.addItem(pic4); monstorList.addItem(pic4b); } //观音 else if(colStr.charAt(col)=="g") { var pic1:Image = new Image(); pic1.source="image/image407.jpg"; pic1.x=gridWidth*col; pic1.y=gridHeight*row; pic1.width=gridWidth; pic1.height=gridHeight; this.addChild(pic1); eachGridList.addItem(pic1); } //栅栏门 else if(colStr.charAt(col)=="S") { var pic2:Image = new Image(); pic2.source="image/image112.png"; pic2.x=gridWidth*col; pic2.y=gridHeight*row; pic2.width=gridWidth; pic2.height=gridHeight; this.addChild(pic2); eachGridList.addItem(pic2); } //紫门 else if(colStr.charAt(col)=="L") { var dr1:BlueDoor = new BlueDoor(); dr1.width=gridWidth; dr1.height=gridHeight; dr1.x=gridWidth*col; dr1.y=gridHeight*row; this.addChild(dr1); eachGridList.addItem(dr1); } //紫钥匙 else if(colStr.charAt(col)=="l") { var pic5:Image = new Image(); pic5.source="image/image12.png"; pic5.x=gridWidth*col; pic5.y=gridHeight*row; pic5.width=gridWidth; pic5.height=gridHeight; this.addChild(pic5); eachGridList.addItem(pic5); } //红门 else if(colStr.charAt(col)=="N") { var dr2:RedDoor = new RedDoor(); dr2.width=gridWidth; dr2.height=gridHeight; dr2.x=gridWidth*col; dr2.y=gridHeight*row; this.addChild(dr2); eachGridList.addItem(dr2); } //红钥匙 else if(colStr.charAt(col)=="n") { imgRedKey.source="image/image15.png"; imgRedKey.x=gridWidth*col; imgRedKey.y=gridHeight*row; imgRedKey.width=gridWidth; imgRedKey.height=gridHeight; } //j 紫机器人 else if(colStr.charAt(col)=="j") { var pic15:Image = new Image(); pic15.width=gridWidth; pic15.height=gridHeight; pic15.x=gridWidth*col; pic15.y=gridHeight*row; pic15.source="image/image92.png"; this.addChild(pic15); eachGridList.addItem(pic15); var pic15b:Image = new Image(); pic15b.width=gridWidth; pic15b.height=gridHeight; pic15b.x=gridWidth*col; pic15b.y=gridHeight*row; pic15b.source="image/image94.png"; pic15b.alpha=0; this.addChild(pic15b); eachGridList.addItem(pic15b); monstorList.addItem(pic15); monstorList.addItem(pic15b); } //黄骷髅 else if(colStr.charAt(col)=="k") { var pic16:Image = new Image(); pic16.width=gridWidth; pic16.height=gridHeight; pic16.x=gridWidth*col; pic16.y=gridHeight*row; pic16.source="image/image197.png"; this.addChild(pic16); eachGridList.addItem(pic16); var pic16b:Image = new Image(); pic16b.width=gridWidth; pic16b.height=gridHeight; pic16b.x=gridWidth*col; pic16b.y=gridHeight*row; pic16b.source="image/image199.png"; pic16b.alpha=0; this.addChild(pic16b); eachGridList.addItem(pic16b); monstorList.addItem(pic16); monstorList.addItem(pic16b); } //o 黄机器人 else if(colStr.charAt(col)=="o") { var pic17:Image = new Image(); pic17.width=gridWidth; pic17.height=gridHeight; pic17.x=gridWidth*col; pic17.y=gridHeight*row; pic17.source="image/image97.png"; this.addChild(pic17); eachGridList.addItem(pic17); var pic17b:Image = new Image(); pic17b.width=gridWidth; pic17b.height=gridHeight; pic17b.x=gridWidth*col; pic17b.y=gridHeight*row; pic17b.source="image/image99.png"; pic17b.alpha=0; this.addChild(pic17b); eachGridList.addItem(pic17b); monstorList.addItem(pic17); monstorList.addItem(pic17b); } } }    对应地图的xml文件为 <?xml version="1.0" encoding="utf-8"?> <maps> <map> <row>5555555555555</row> <row>5D4EFE4444445</row> <row>5555555555545</row> <row>5h44645m14545</row> <row>54J4545Mh4545</row> <row>5565545556545</row> <row>5144546GIG545</row> <row>54K4545555545</row> <row>5565544444445</row> <row>5444556555655</row> <row>5h41514354G45</row> <row>5hB154A45EHE5</row> <row>5555555555555</row> </map> <map> <row>5555555555555</row> <row>5D4EFE4444445</row> <row>5555555555545</row> <row>5h44645m14545</row> <row>54J4545Mh4545</row> <row>5565545556545</row> <row>5144546GIG545</row> <row>54K4545555545</row> <row>5565544444445</row> <row>5444556555655</row> <row>5h41514354G45</row> <row>5h4154A45EHE5</row> <row>5555555555555</row> </map> <map> <row>5555555555555</row> <row>5D4EFE4444445</row> <row>5555555555545</row> <row>5h44645m14545</row> <row>54J4545Mh4545</row> <row>5565545556545</row> <row>5144546GIG545</row> <row>54K4545555545</row> <row>5565544444445</row> <row>5444556555655</row> <row>5h41514354G45</row> <row>5h4154A45EHE5</row> <row>5555555555555</row> </map> </maps> 这样一个简单的RPG就完成了。

    最新回复(0)