尝试让 div 在按下按键时在屏幕上移动
Trying to make div move on screen on keydown
我正在尝试制作一款游戏,玩家必须通过鼠标在迷宫中导航才能找到奶酪块。我在让 div 移动到我想要的位置时遇到了一些麻烦。问题是当你按下向下箭头键(我用来测试它的键)时,它不会像我想要的那样以十为增量继续下降。我试过循环,整个 shebang,但我就是不知道如何让它工作。
document.onkeydown = function(key){
if (key.keyCode == 38){
console.log("up");
} else if (key.keyCode == 39){
console.log("right");
} else if (key.keyCode == 37){
console.log("left");
} else if (key.keyCode == 40){
mouse.style.top = 10 + "px";
};
};
你每次都设置为10,先用split
取值:
else if (key.keyCode == 40){
mouse.style.top = parseInt(mouse.style.top.split("px")[0]) + 10 + "px";
}
您可以使用 Player
对象并修改它的 x
和 y
属性。
还创建一个 K
对象以将当前使用的 keys 数字设置为布尔值。
比在 requestAnimationFrame
这样的循环中移动播放器:
const Player = {
el: document.getElementById('mouse'),
x: 200,
y: 100,
speed: 2,
move() {
// If no collision with map or tiles... (TODO!)
this.el.style.transform = `translate(${this.x}px, ${this.y}px)`;
}
};
const K = {
fn(ev) {
ev.preventDefault();
const k = ev.which;
if (k >= 37 && k <= 40) K[k] = ev.type === "keydown"; // If is arrow
}
};
const update = () => {
// 1 / Math.sqrt(2) = 0.7071.. (Diagonal)
let dist = K[38] && (K[37] || K[39]) || K[40] && (K[37] || K[39]) ? 0.707 : 1;
dist *= Player.speed;
if (K[37]) Player.x -= dist;
if (K[38]) Player.y -= dist;
if (K[39]) Player.x += dist;
if (K[40]) Player.y += dist;
Player.move();
}
document.addEventListener('keydown', K.fn);
document.addEventListener('keyup', K.fn);
(function engine() {
update();
window.requestAnimationFrame(engine);
}());
#mouse{
position: absolute;
left: 0; top: 0;
width: 20px; height: 20px;
background: #000; border-radius: 50%;
}
Click here to focus, and use arrows
<div id="mouse"></div>
一个更简单的例子,它通过Npx步移动元素并重复,只要你按住键(就像打字一样) 将是:
const Player = {
el: document.getElementById('mouse'),
x: 0,
y: 0,
step: 10,
move() {
this.el.style.transform = `translate(${this.x}px, ${this.y}px)`;
}
};
const keysHandler = (ev) => {
const k = ev.which;
if (k>=37 && k<=40) {
ev.preventDefault();
({
'37' () {Player.x -= Player.step},
'38' () {Player.y -= Player.step},
'39' () {Player.x += Player.step},
'40' () {Player.y += Player.step},
}[k]());
Player.move();
}
}
document.addEventListener('keydown', keysHandler);
#mouse{
position: absolute;
left: 0; top: 0;
width: 20px; height: 20px;
background: #000; border-radius: 50%;
}
Click here to focus, and use arrows
<div id="mouse"></div>
即使使用了其他答案parseInt
,最好将变量保留在代码中
var x = 0;
var y = 0;
document.onkeydown = function(key){
if (key.keyCode == 38){
y -= 10;
} else if (key.keyCode == 39){
x += 10;
} else if (key.keyCode == 37){
x -= 10;
} else if (key.keyCode == 40){
y += 10;
};
document.getElementById("block").style.left = x+"px";
document.getElementById("block").style.top = y+"px";
};
#block
{
top: 0;
left: 0;
position: absolute;
width: 30px;
height: 60px;
background: teal;
}
<div id="block">
我正在尝试制作一款游戏,玩家必须通过鼠标在迷宫中导航才能找到奶酪块。我在让 div 移动到我想要的位置时遇到了一些麻烦。问题是当你按下向下箭头键(我用来测试它的键)时,它不会像我想要的那样以十为增量继续下降。我试过循环,整个 shebang,但我就是不知道如何让它工作。
document.onkeydown = function(key){
if (key.keyCode == 38){
console.log("up");
} else if (key.keyCode == 39){
console.log("right");
} else if (key.keyCode == 37){
console.log("left");
} else if (key.keyCode == 40){
mouse.style.top = 10 + "px";
};
};
你每次都设置为10,先用split
取值:
else if (key.keyCode == 40){
mouse.style.top = parseInt(mouse.style.top.split("px")[0]) + 10 + "px";
}
您可以使用 Player
对象并修改它的 x
和 y
属性。
还创建一个 K
对象以将当前使用的 keys 数字设置为布尔值。
比在 requestAnimationFrame
这样的循环中移动播放器:
const Player = {
el: document.getElementById('mouse'),
x: 200,
y: 100,
speed: 2,
move() {
// If no collision with map or tiles... (TODO!)
this.el.style.transform = `translate(${this.x}px, ${this.y}px)`;
}
};
const K = {
fn(ev) {
ev.preventDefault();
const k = ev.which;
if (k >= 37 && k <= 40) K[k] = ev.type === "keydown"; // If is arrow
}
};
const update = () => {
// 1 / Math.sqrt(2) = 0.7071.. (Diagonal)
let dist = K[38] && (K[37] || K[39]) || K[40] && (K[37] || K[39]) ? 0.707 : 1;
dist *= Player.speed;
if (K[37]) Player.x -= dist;
if (K[38]) Player.y -= dist;
if (K[39]) Player.x += dist;
if (K[40]) Player.y += dist;
Player.move();
}
document.addEventListener('keydown', K.fn);
document.addEventListener('keyup', K.fn);
(function engine() {
update();
window.requestAnimationFrame(engine);
}());
#mouse{
position: absolute;
left: 0; top: 0;
width: 20px; height: 20px;
background: #000; border-radius: 50%;
}
Click here to focus, and use arrows
<div id="mouse"></div>
一个更简单的例子,它通过Npx步移动元素并重复,只要你按住键(就像打字一样) 将是:
const Player = {
el: document.getElementById('mouse'),
x: 0,
y: 0,
step: 10,
move() {
this.el.style.transform = `translate(${this.x}px, ${this.y}px)`;
}
};
const keysHandler = (ev) => {
const k = ev.which;
if (k>=37 && k<=40) {
ev.preventDefault();
({
'37' () {Player.x -= Player.step},
'38' () {Player.y -= Player.step},
'39' () {Player.x += Player.step},
'40' () {Player.y += Player.step},
}[k]());
Player.move();
}
}
document.addEventListener('keydown', keysHandler);
#mouse{
position: absolute;
left: 0; top: 0;
width: 20px; height: 20px;
background: #000; border-radius: 50%;
}
Click here to focus, and use arrows
<div id="mouse"></div>
即使使用了其他答案parseInt
,最好将变量保留在代码中
var x = 0;
var y = 0;
document.onkeydown = function(key){
if (key.keyCode == 38){
y -= 10;
} else if (key.keyCode == 39){
x += 10;
} else if (key.keyCode == 37){
x -= 10;
} else if (key.keyCode == 40){
y += 10;
};
document.getElementById("block").style.left = x+"px";
document.getElementById("block").style.top = y+"px";
};
#block
{
top: 0;
left: 0;
position: absolute;
width: 30px;
height: 60px;
background: teal;
}
<div id="block">