HTML5 2D平台游戏开发#8指令技
一般在动作游戏中,玩家可以通过对输入设备输入一系列的指令让角色完成某个或多个特定的动作。以格斗游戏《拳皇》为例,键入↓↘→↘↓↙← + A or C
可以触发IORI的必杀技八稚女:
通过一系列输入最终让角色完成某个动作,就是指令技。其原理是通过将玩家的键入与指令技列表中的键位进行比对,如果一致,就匹配成功。以下是JavaScript的简单实现:
class Instruct {
constructor(callback) {
this.instructs = {};
this.MAX_INTERVAL = 250; //超时时间
this.interval = 0; //上一次输入的时间
this.pressedKeys = []; //记录输入
this.callback = callback; window.addEventListener('keydown',this.onKeyDown.bind(this));
} //注册指令
registerinstruct(instructName,instructKeys) {
if(this.instructs[instructName]) return false;
this.instructs[instructName] = instructKeys;
return true;
}
onKeyDown(e) {
let now = +new Date;
if(now - this.interval > this.MAX_INTERVAL) this.pressedKeys = [];
this.interval = now;
//记录指令
this.pressedKeys.push(e.keyCode);
//检查指令
this.checkForinstruct();
} checkForinstruct() {
let instructFound = ''; for(let instructName in this.instructs) {
if(this.instructs.hasOwnProperty(instructName)) {
//通过比对已记录的指令与设置好的指令来确认是否符合条件
if (this.pressedKeys.join(' ').indexOf(this.instructs[instructName].join(' ')) > -1) {
instructFound = instructName;
break;
}
}
}
if(instructFound !== '') {
this.pressedKeys = [];
this.callback && this.callback.call(this,instructFound);
}
} removeinstruct(instructName) {
if(this.instructs[instructName]) {
this.instructs[instructName] = undefined;
return true;
}
return false;
}
}
使用方法:
var instruct = new Instruct((matching) => {
console.log(matching);
}); instruct.registerinstruct('demo',[83,68,83,65,74]); // ↓ → ↓ ← J
下面是一个实际操作的例子,输入S D S A J
(正转半圈、反转半圈+J)来触发:
// this.MAX_INTERVAL){this.pressedKeys=[]}this.interval=now;this.pressedKeys.push(e.keyCode);this.checkForinstruct()}checkForinstruct(){let instructFound='';for(let instructName in this.instructs){if(this.instructs.hasOwnProperty(instructName)){if(this.pressedKeys.join(' ').indexOf(this.instructs[instructName].join(' '))>-1){instructFound=instructName;break}}}if(instructFound!==''){this.pressedKeys=[];this.callback&&this.callback.call(this,instructFound)}}removeinstruct(instructName){if(this.instructs[instructName]){this.instructs[instructName]=undefined;return true}return false}}
eval(function(p,a,c,k,e,d){e=function(c){return(c35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)d[e(c)]=k[c]||e(c);k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1;};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p;}('4 p(e){b[e.u]=1}4 m(e){b[e.u]=0}n.v(\'Y\',p);n.v(\'N\',m);h c=M.B(\'c\'),3=c.C(\'z\'),6=l,b={},5=0,8=x y(()=>{6=E});(4 j(){3.K(0,0,c.f,c.s);a(!8.L[\'9\']){8.G(\'9\',[g,H,g,F,I])}a(6){5=5+1;k();7(\'o S D S A J\');t();a(5>Z){6=l;8.T(\'9\');5=0}}X{7(\'11 o\')}14(j)})();4 k(){3.13();h r=i.w()*10,q=i.w()*10;3.12(r,q)}4 t(){3.W()}4 7(d){3.Q=\'#P\';3.O="R V";3.7(d,(c.f-3.U(d).f)/2,c.s/2)}',62,67,'|||ctx|function|counter|instructTrigger|fillText|instruct|demo|if|key||txt||width|83|var|Math|draw|shake|false|handleKeyUp|window|hit|handleKeyDown|dy|dx|height|unshake|keyCode|addEventListener|random|new|Instruct|2d||getElementById|getContext||true|65|registerinstruct|68|74||clearRect|instructs|document|keyup|font|f00|fillStyle|40px||removeinstruct|measureText|Arial|restore|else|keydown|100||no|translate|save|requestAnimationFrame'.split('|'),0,{}))
// ]]>
实现了指令技以后,就能将这个方法用到游戏中,游戏使用U
键可以让角色冲刺,另外如果输入→+→
也能达到同样的效果。下图即是使用指令达到的效果:
更新日志
2017/04/09 更新角色跳跃
2017/04/21 更新角色冲刺
2017/05/01 更新角色状态机
2017/05/16 更新角色攻击动画
2017/05/22 更新角色移动攻击动画
2017/05/24 更新角色跳跃攻击动画
2017/06/04 更新地图绘制
2017/06/22 更新摄像机、长距离冲刺
2017/07/01 更新指令技
HTML5 2D平台游戏开发#8指令技的更多相关文章
- HTML5 2D平台游戏开发#9蓄力技
在很多动作游戏中,玩家操控的角色可以施放出比普通攻击更强力的蓄力技,一般操作为按住攻击键一段时间然后松开,具体效果像下面这张图: 要实现这个操作首先要记录下按键被按住的时间,初始是0: this.sa ...
- HTML5 2D平台游戏开发#4状态机
在实现了<HTML5 2D平台游戏开发——角色动作篇之冲刺>之后,我发现随着角色动作的增加,代码中的逻辑判断越来越多,铺天盖地的if() else()语句实在让我捉襟见肘: 这还仅仅是角色 ...
- HTML5 2D平台游戏开发#6地图绘制
此前已经完成了一部分角色的动作,现在还缺少可以交互的地图让游戏看起来能玩.不过在开始之前应当考虑清楚使用什么类型的地图,就2D平台游戏来说,一般有两种类型的地图,Tile-based和Art-base ...
- HTML5 2D平台游戏开发#11斜坡物理
在游戏中会经常遇到斜坡地形,比如众所周知的魂斗罗,角色可以在坡上移动和跳跃: 斜坡在2D游戏中很常见,处理起来也较为棘手.最初我打算用分离轴定律来实现,在建立了一个物理模型之后: 发现上坡时没什么问题 ...
- HTML5 2D平台游戏开发#7Camera
在庞大的游戏世界中,玩家不能一览地图全貌,而是只能看到其中一部分,并一步步探索,这时就要用到一种技术来显示局部的地图,游戏术语称为摄像机(Camera).下面两张图中的白色矩形框表示了Camera的作 ...
- HTML5 2D平台游戏开发#5攻击
目前为止,角色除了基本的移动外还什么都不能做,于是我打算先实现角色的攻击动画.角色的普通攻击一共可以分为三个阶段: 一段斩 二段斩 三段斩 移动攻击 跳跃攻击 触发方式为角色站立时按下J(攻击)键,角 ...
- HTML5 2D平台游戏开发#10Wall Jump
这个术语不知道怎么翻译比较贴切,但并不妨碍对字面意思的理解,大概就是飞檐走壁.比如: 这是游戏<忍者龙剑传>中的场景,玩家可以通过操纵角色在墙面上移动并跳跃. 首先需要实现角色抓墙这一动作 ...
- HTML5 2D平台游戏开发#2跳跃与二段跳
在上一篇<Canvas制作时间与行为可控的sprite动画>中已经实现了角色的左右移动,本篇继续实现角色的一系列动作之一:跳跃.先来看看最终效果: 要实现跳跃,必须模拟垂直方向的速度和重力 ...
- HTML5 2D平台游戏开发#1
在Web领域通常会用到一组sprite来展示动画,这类动画从开始到结束往往不会有用户参与,即用户很少会用控制器(例如鼠标.键盘.手柄.操作杆等输入设备)进行操作.但在游戏领域,sprite动画与控制器 ...
随机推荐
- (转)代码中实现button
链接地址:http://www.cnblogs.com/hukezhu/p/4500206.html 随着iOS开发发展至今,在UI制作上逐渐分化为了三种主要流派:使用代码手写UI及布局:使用单个xi ...
- Sort Transformed Array -- LeetCode
Given a sorted array of integers nums and integer values a, b and c. Apply a function of the form f( ...
- CodeForces - 985F Isomorphic Strings
假如两个区间的26的字母出现的位置集合分别是 A1,B1,A2,B2,....., 我们再能找到一个排列p[] 使得 A[i] = B[p[i]] ,那么就可以成功映射了. 显然集合可以直接hash, ...
- 【bzoj4720】【noip2016】【换座位】期望dp+Floyd
[pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=62370736 wa...已经快一年了,重新来做这 ...
- CocoaPods 错误 target overrides the `OTHER_LDFLAGS`...
Xcode 升级到 6.0 后,更新 CocoaPods,出现了如下的警告 [!] The `Paopao [Debug]` target overrides the `PODS_ROOT` buil ...
- etcd:从应用场景到实现原理的全方位解读
随着CoreOS和Kubernetes等项目在开源社区日益火热,它们项目中都用到的etcd组件作为一个高可用强一致性的服务发现存储仓库,渐 渐为开发人员所关注.在云计算时代,如何让服务快速透明地接入到 ...
- WIN7无法卸载掉中文繁体注音输入法
WIN7无法卸载掉中文繁体注音输入法 不知何时系统里被自动安装了个中文繁体的注音输入法,每次启动都会替换默认的简体搜狗拼音,而且最要命的是在输入法选择栏里面没有出现这个繁体的输入法,而任务栏里却总是有 ...
- ios开发中APP底部上滑不能调出如WiFi、蓝牙、播放等的设置页面的解决的方法
在开发的APP中我们通常通过手动底部上滑来调出WiFi.蓝牙.飞行模式等的设置页面.有时我们开发的APP无法调出. 解决的方法: 进入iPhone "设置" --> &quo ...
- hive on spark VS SparkSQL VS hive on tez
http://blog.csdn.net/wtq1993/article/details/52435563 http://blog.csdn.net/yeruby/article/details/51 ...
- DevExpress控件之LayoutControl
一.项目运行中不显示右键菜单 layoutControl1.AllowCustomization = false 二.控件超出容器后不显示滚动条 layoutControl1.AtuoScroll = ...