哎呦要想让你在格斗游戏里操控的主角动起来可不简单,今天就让我告诉大家怎么通过编程解决!比如说,移动、跳跃、攻击什么的全都是用代码控制的呀。还要配合键盘操作,才能让你尽情地玩个痛快!首先,弄一张好看的背景图片,然后用LBitmap加载到黑色的矩形上,那感觉立马不一样了哟~
//背景层 backLayer = new LSprite(); //backLayer.graphics.drawRect(1,"#000",[0,0,LGlobal.width,LGlobal.height],true,"#000"); baseLayer.addChild(backLayer); var background = new LBitmap(new LBitmapData(imglist["back"])); backLayer.addChild(background);
角色动作设计
keyCtrl[e.keyCode] = true;
游戏角色动作可不能马虎,它们影响游戏体验!咱们得考虑到各种小动作,包括站姿、走动、奔跑、跳跃和打击等。只需精心处理每一个动作,你的角色就能栩栩如生了。
keyCtrl[e.keyCode] = false;
移动与行走
Character.prototype.move = function (){ var self = this, mx = self.mx, my = self.my; if(self.action == ACTION.MOVE || self.action == ACTION.JUMP || self.action == ACTION.JUMP_ATTACK){ mx *= MOVE_STEP; my *= MOVE_STEP; }else if(self.action == ACTION.RUN){ mx *= MOVE_STEP*2; my *= MOVE_STEP*2; }else if(self.action == ACTION.HIT){ mx = MOVE_STEP*2*(self.direction == DIRECTION.RIGHT ? 1 : -1); my = 0; }else{ mx = my = 0; } if(mx == 0 && my == 0)return; self.x += mx; self.y += my; if(self.y 448){ self.y = 448; } };
Player.prototype.move = function (){ var self = this, mx = 0, my = 0; if(keyCtrl[KEY.LEFT]){ mx = -1; }else if(keyCtrl[KEY.RIGHT]){ mx = 1; }else if(keyCtrl[KEY.UP]){ my = -1; }else if(keyCtrl[KEY.DOWN]){ my = 1; } self.mx = mx; self.my = my; self.callParent("move",arguments); };
记得,在游戏中,操控角色行动很关键!首先,你需要按着方向键帮他找路,就像咱们平时玩的那样。如果想让角色蹦跶,那就在Character类的onframe函数那里呼叫move函数这个小伙伴帮忙,再根据主角动态算出适宜的步伐长度。别忘了,还要注意键盘的碰撞检测,这样就能切换主角的行进方式,这么一来,咱的角色就可以奔跑了呀。
var keyList = [{keyCode:0,time:0},{keyCode:0,time:0},{keyCode:0,time:0}];
跳跃与跳跃攻击
var keyThis = {keyCode:e.keyCode,time:(new Date()).getTime()}; var keyLast01 = keyList[0]; var keyLast02 = keyList[1]; keyList.unshift(keyThis); keyList.pop();
哈喽,按下shift键,角色就能跳起来哦;你瞧,画面上的小人儿都往上跳了。想要调整高度,只需要把屏幕后边那个y轴图案略微调亮点。但记住,跳着进攻时别在瞎折腾别的事儿,不然游戏就会卡住或者变得没那么流畅哩~
switch(e.keyCode){ case KEY.LEFT: if(keyLast01.keyCode == KEY.LEFT && keyThis.time - keyLast01.time < 200){ hero.setAction(ACTION.RUN,DIRECTION.LEFT); }else{ hero.setAction(ACTION.MOVE,DIRECTION.LEFT); } break; case KEY.RIGHT: if(keyLast01.keyCode == KEY.RIGHT && keyThis.time - keyLast01.time < 200){ hero.setAction(ACTION.RUN,DIRECTION.RIGHT); }else{ hero.setAction(ACTION.MOVE,DIRECTION.RIGHT); } break;
连续攻击与特殊技能
说到游戏的爽感,你们可能觉得就是打架打得痛快,手底下使劲儿按就行了,但我要说,这款秧歌三国里,有个超级酷炫的技巧,你得先按住→,再往上推,然后紧接着再来一下猛攻,马上就能看见大侠闪亮登场这可不是碰运气,还得通过我们学习到的编程知识来监测和操作这些按键动作,这样才能提高触发特殊事件的机会。
Player.prototype.onframe = function (){ var self = this; self.callParent("onframe",arguments); if(self.action == ACTION.JUMP){ self.onjump(); }else if(self.action == ACTION.JUMP_ATTACK){ self.onjump_attack(); } };
绝招技能设计
嘿?你们知道吗,游戏里有种叫做“有线技能”的东西,简直就是个神秘武器!但是使用它还挺有讲究的,文章说了要同时按攻击键和跳跃键才能使出来。这不是为了让战斗更公平,用技能可得深思熟虑,别瞎按。
Player.prototype.onjump = function (){ var self = this; self.setLocation(); var index = self.anime.colIndex; self.yArr = [0,-10,-20,-30,-40,-40,-30,-20,-10,0]; self.anime.y += self.yArr[index]; }; Player.prototype.onjump_attack = function (){ var self = this; self.setLocation(); var index = self.anime.colIndex; if(index >= self.yArr.length)return; self.anime.y += self.yArr[index]; };
优化与完善
setTimeout("keylock = true;",50); hero.setAction(ACTION.ATTACK,hero.direction);
搞定角色动作设定可不是轻松活儿!要经常改改代码,让游戏运行起来流畅自如,符合我们的预期效果哈。比如说得把几个部分联系在一起的技能图像按照顺序排列好;还有记得合理控制连招次数和间隔时间;最重要的就是,每完成一次动作都能立马切换到下一个动作。
源码下载与测试
case KEY.ATTACK: if(keyLast01.keyCode == KEY.ATTACK && keyLast02.keyCode == KEY.ATTACK && keyThis.time - keyLast02.time < 1000){ keyList = [{keyCode:0,time:0},{keyCode:0,time:0},{keyCode:0,time:0}]; keylock = true; hero.setAction(ACTION.BIG_ATTACK,hero.direction); }
来咯来这篇文章的代码下载链接就在下面,大家快去学学看要是有啥不明白的地方或者想深入研究下的话,就去试试每个角色的动态效果!
case KEY.ATTACK: if(keyLast01.keyCode == KEY.ATTACK && keyLast02.keyCode == KEY.ATTACK && keyThis.time - keyLast02.time < 1000){ keyList = [{keyCode:0,time:0},{keyCode:0,time:0},{keyCode:0,time:0}]; keylock = true; hero.setAction(ACTION.BIG_ATTACK,hero.direction); }else if(hero.action == ACTION.JUMP){ hero.setAction(ACTION.JUMP_ATTACK,hero.direction); }else{ setTimeout("keylock = true;",50); hero.setAction(ACTION.ATTACK,hero.direction); }
。
评论0