物体的移动,而不受任何操作系统的干扰
movement of the object, without interference from the any operation system?
有一个测试代码适合您:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<style>
bri {
position: absolute;
width: 20px;
height: 20px;
}
.dirt {
background-color:brown;
}
.gracz {
background-color:red
}
</style>
<script >
function makeLevel() {
width = 30
heigth = 30
var bricks = document.getElementById('bricks')
bricks.style.width = 20 * heigth + 'px'
bricks.style.height = 20 * width + 'px'
bricks.innerHTML = ''
for (var i = 0; i < width; i++) {
for (var j = 0; j < heigth; j++) {
var bri = document.createElement("bri");
bricks.appendChild(bri)
bri.style.top = i * 20 + "px"
bri.style.left = j * 20 + "px"
bri.id = i + "_" + j
bri.className = 'dirt'
}
}
pos_player_x = Math.floor(Math.random() * width)
pos_player_y = Math.floor(Math.random() * heigth)
bri = document.getElementById(pos_player_x + '_' + pos_player_y);
bri.className = 'gracz'
}
function moving(event) {
var key = event.which
//87 - w , 83 -s, 65 -a, 68 - d
switch (key) {
case 87:
var new_x = pos_player_x - 1;
var new_y = pos_player_y - 0;
break;
case 83:
var new_x = pos_player_x + 1;
var new_y = pos_player_y - 0;
break;
case 65:
var new_x = pos_player_x - 0;
var new_y = pos_player_y - 1;
break;
case 68:
var new_x = pos_player_x - 0;
var new_y = pos_player_y + 1;
break;
}
var new_pos = document.getElementById(new_x + '_' + new_y)
var old_pos = document.getElementById(pos_player_x + '_' + pos_player_y)
if (new_pos != null) {
new_pos.className = 'gracz';
old_pos.className = 'clean';
pos_player_y = new_y;
pos_player_x = new_x;
}
}
</script>
</head>
<body onload="makeLevel()" onkeydown="moving(event)">
<div id="game">
<div id="bricks" draggable="false"></div>
</div>
</body>
</html>
我们需要专注于所谓的"移动"功能。如果我们按住其中一个按钮"WASD",我们的玩家将沿着这个方向。但是,在第一秒钟,他移动了一次,之后他们会重复,直到他到达其中一堵墙。
问题来了。如果我们按住其中一个按钮,如何消除这个小停顿?
这是我的第一篇文章,很抱歉出现错误。
当前您的移动与接收到的按键数有关。这不仅是因为你感知到的操作系统问题(即它多久会开始发送重复的按键),还因为如果有人按下按键的速度比按下按钮的基本重复速度更快("垃圾邮件"),它会允许更快的移动。
相反,你的游戏应该有一个内部的"刻度",它可以量化移动。当按键被按下和释放时,你会将这些变化传播到一个内部变量,每次你的游戏决定是时候移动时,都会根据这个变量的当前状态进行移动。这意味着,无论有人是按住密钥还是发送垃圾邮件,无论操作系统发送重复信号的速度如何,区块都会以游戏允许的速度移动。
编辑:这是您的代码的调整版本。请注意新引入的startGameLoop
函数,以及我如何使用onkeydown
和onkeyup
事件。在这个版本中,当按下某个键时,蛇会以稳定的速度移动,释放后不会移动。
HTML:<body onload="makeLevel()" onkeydown="registerKey(event)" onkeyup="releaseKey()">
Javascript:
var currentlyPressedKey;
function makeLevel() {
width = 30
heigth = 30
var bricks = document.getElementById('bricks')
bricks.style.width = 20 * heigth + 'px'
bricks.style.height = 20 * width + 'px'
bricks.innerHTML = ''
for (var i = 0; i < width; i++) {
for (var j = 0; j < heigth; j++) {
var bri = document.createElement("bri");
bricks.appendChild(bri)
bri.style.top = i * 20 + "px"
bri.style.left = j * 20 + "px"
bri.id = i + "_" + j
bri.className = 'dirt'
}
}
pos_player_x = Math.floor(Math.random() * width)
pos_player_y = Math.floor(Math.random() * heigth)
bri = document.getElementById(pos_player_x + '_' + pos_player_y);
bri.className = 'gracz';
startGameLoop();
}
function startGameLoop(){
setInterval(move,250);
}
function registerKey(event){
currentlyPressedKey = event.which;
}
function releaseKey(){
currentlyPressedKey = null;
}
function move() {
var key = currentlyPressedKey;
//87 - w , 83 -s, 65 -a, 68 - d
switch (key) {
case 87:
var new_x = pos_player_x - 1;
var new_y = pos_player_y - 0;
break;
case 83:
var new_x = pos_player_x + 1;
var new_y = pos_player_y - 0;
break;
case 65:
var new_x = pos_player_x - 0;
var new_y = pos_player_y - 1;
break;
case 68:
var new_x = pos_player_x - 0;
var new_y = pos_player_y + 1;
break;
}
var new_pos = document.getElementById(new_x + '_' + new_y)
var old_pos = document.getElementById(pos_player_x + '_' + pos_player_y)
if (new_pos != null) {
new_pos.className = 'gracz';
old_pos.className = 'clean';
pos_player_y = new_y;
pos_player_x = new_x;
}
}
相关文章:
- 函数参数中的数据与指定变量之间的任何性能差异
- 有没有任何方法可以将控制器从文件加载到ui路由器$stateProvider中
- jQuery中是否内置了任何字符串格式化函数
- 在单击任何位置时隐藏元素,而不检查每次DOM单击
- 是否有任何snippet或jQuery插件可以列出easylist.txt模式匹配的DOM中的所有元素
- 有任何可能将facebook实时信使整合到一个网站中
- 是否有任何方法可以使用jQuery替换在数组中定义值的文本
- 防止Alt+Shift默认操作或检测多种操作系统语言的Javascript
- AJAX:$_GET不返回任何值
- 在同一个服务工作者中处理service-worker.js有任何影响吗
- 将事件处理程序绑定到任何可能的事件
- 在Highcharts中,我们可以通过任何方式在渲染图表之前获得plotWidth和plotHeight
- 任何方式使AJAX调用Gmail API,而无需通过JS库
- document.styleSheets不返回任何内容
- 在D3.js中,有没有任何方法可以将x和y方向上的滚动事件绑定到平移svg
- Graphiti中是否有任何工具提示功能
- 有没有任何方法可以使用node-js从不同的机器打开浏览器
- 在解析对象.save()中;没有返回任何原因
- 任何浏览器都不支持javascript函数
- 物体的移动,而不受任何操作系统的干扰