- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
有一个测试代码给你:
<!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”按钮之一,我们的播放器将遵循此方向。但是,第一秒他移动了一次,之后他们会重复移动直到他到达其中一堵墙。
问题来了。如果我们按住其中一个按钮,如何消除这个小停顿?
这是我的第一篇文章,如有错误请见谅。
最佳答案
目前,您的 Action 与收到的按键次数有关。这是有问题的,不仅是因为您感知到的操作系统问题(即它会在多长时间内开始发送重复键),而且还因为如果有人比基本重复更快地按下键(“垃圾邮件”),它允许更快的移动按下按钮的速度。
相反,您的游戏应该有一个内部“滴答声”,它可以量化运动。当按下和释放键时,您会将这些更改传播到一个内部变量,并且每次您的游戏决定该移动时,都会根据该变量的当前状态移动。这意味着无论某人是按住按键还是发送垃圾邮件,无论操作系统发送重复信号的速度如何,方 block 都将按照您的游戏允许的速度移动。
编辑:这是您的代码的调整版本。注意新引入的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;
}
}
关于javascript - 对象的移动,不受任何操作系统的干扰?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36515528/
如主题所述,是否有可能没有 future 的问题?。基本上我发布此线程以供将来引用,以便人们可以观看该场景。mongodb 被限制为 2Gb 的原因是什么?能否请您指出引用资料,以便像我这样的菜鸟可以
我正在使用 Bootstrap 4 轮播。它适用于所有 Windows 和 Android 操作系统,但不适用于 Mac 和 iPhone 操作系统。有什么解决办法吗?这是我的代码github.com
目录 内存管理硬件结构 早期内存的使用方法 分段 分页 逻辑地址,线性地址(intel架构) 虚拟地址 物理地址
页表的一些术语 现在Linux内核中支持四级页表的映射,我们先看下内核中关于页表的一些术语: 全局目录项,PGD(Page Global Directory) 上级目录项,PUD(
1. 文件路径 绝对路径 绝对路径 (absolute path) : 以一个盘符开头的路径,就是绝对路径 例如这里的 D:\java\IntelliJ IDEA Community Edition
1. 进程介绍 1.1 进程的概念 程序是由指令和数据组成的,但这些指令要运行,数据要读写,就必须将指令加载至 CPU,将数据加载至内存。在指令运行过程中还需要用到磁盘和网络等设备。进程就是用来加载指
1. 文件路径 绝对路径 绝对路径 (absolute path) : 以一个盘符开头的路径,就是绝对路径 例如这里的 D:\java\IntelliJ IDEA Community Edition
我有 REST APIManager 类 - 用于使用服务器 api 的单例。有返回用户Token实体的登录方法; 所有其他 API 方法都使用 token 来发出请求。登录后我应该在哪里存储该 to
当我在学习操作系统类(class)时,我不明白为什么下面代码的输出是这样的 代码: #include #include #include #include int main (int argc
我正在尝试在 C++ 中使用 fork() 和 wait() 系统调用。 我的代码非常简单。但是我收到以下错误: error C3861: 'fork': identifier not found 我
我需要播放不同格式的网络广播。我尝试使用 MPMoviePlayerController 播放广播,但它在 3-4 秒后停止。 var urlAddress = "http://streaming.r
多线程(进阶) 1. 常见的锁策略 1.1 乐观锁 悲观锁 乐观锁 : 总是假设最好的情况,每次去拿数据的时候都认为别人不会修改数据,但是在对数据提交更新的时候,再去判断这个数据在这个期间是否有别人对
如何让内存用起来? 内存使用:将程序放到内存中,PC指向开始地址 那就让首先程序进入内存 让程序从磁盘加载到内存中来,首先需要考虑要将程序代码安放在内存的什么位置 如果把入口地址直接放在0地址处,然后
操作系统 内存 HBase一定需要RAM! 64位 使用64位平台(和64位JVM)。 交换 注意交换,将swappiness设为0。 CPU 确保已将Hadoop设置为使用本机的硬件
我想编写一个脚本,将当前打开的 Windows 移动到某个位置。 理想情况下,我的程序应该是这样的: var window = FindWindow("Winamp"); window.setPos(
以下问题是我对操作系统的测试评论,但我不知道如何回答。我会第一次尝试解决他们的问题,但我也不知道如何开始。 Given the following information for an assembl
在操作系统方面我见过几次这个词。匿名内存 ,但我真的不知道,那是什么。 如果有人问我一些关于它的事情,我真的无法非常确定地说出那是什么。 我也搜索了它的解释,但不幸的是我还没有找到任何令人满意的东西。
在 Abraham Silberschatz 等人的“操作系统概念”第 9 版中,作者说: "Some operating systems support only static linking, i
我知道可用端口有限制(2^16),但我不明白操作系统如何管理这些端口。内部是否有一个微 Controller 可以复用所有打开的端口?端口实际上是如何与操作系统建立连接的?端口是否连接到操作系统中的特
如何激活另一个已最小化的应用程序的窗口? 如果前面有其他窗口,它可以很好地使用 NSRunningApplication 类的 activateWithOptions 方法,但这不适用于最小化的窗口。
我是一名优秀的程序员,十分优秀!