简体   繁体   English

为什么这个对象不动?

[英]Why does this object not move?

I'm new to Javascript and I've tried to make a simple game where there's a block and it can move. 我是Javascript的新手,我试图制作一个简单的游戏,其中有一个块,它可以移动。 Right now, I've only made it so that it so that it should only move left, but it doesn't. 现在,我只是这样做,以便它只能向左移动,但事实并非如此。

I've checked for typos and I've checked the console with console.log and it works, but my object doesn't move. 我已经检查了拼写错误,我已经用console.log检查了控制台,但它确实有效,但是我的对象没有移动。

const ctx=document.getElementById("ctx").getContext("2d");

var one={
  width:30,
  height:30,
  x:250,
  y:200,
  color:"rgb(63, 154, 244)"
};
function draw(){
  ctx.save();
  ctx.fillStyle=one.color;
  ctx.fillRect(one.x,one.y,one.width,one.height);
  ctx.restore();
}
function move(){
  document.onkeydown=function(event){
    if(event.keyCode==65){
      one.x=one.x-15;
      console.log("test");
    }
  }
}

function startGame(){
  draw();
  setInterval(move,2000);
}
startGame();
<canvas id="ctx" width="1400" height="500" style="border:0.01em solid #fff"></canvas>

I thought that by pressing "a", the block would move left by 15, but it doesn't move. 我认为通过按“a”,该块将向左移动15,但它不会移动。 It still shows up as the blue color, but it just doesn't move. 它仍然显示为蓝色,但它不会移动。

Here's a sample modification on your code, you can improve it. 以下是对代码的示例修改,您可以对其进行改进。

const ctx=document.getElementById("ctx").getContext("2d");

var one={
  width:30,
  height:30,
  x:250,
  y:200,
  color:"rgb(63, 154, 244)"
};

function draw(){
  ctx.save();
  ctx.fillStyle=one.color;
  ctx.fillRect(one.x,one.y,one.width,one.height);
  ctx.restore();
}

// we're gonna register our event handler just one time.
document.onkeydown=function(event){
  if(event.keyCode==65){
    moveLeft();
  }
}

function moveLeft() {
  // I've added this line of code to erase the previous square from the canvas
  // Since we're gonna move your square to another location.
  // You'll probably want to move this code somewhere else
  // Can be inside a redraw function? Idk.
  ctx.clearRect(one.x, one.y, one.width, one.height);
  one.x=one.x-15;
  console.log("test");
}

function startGame(){
  // Let's call draw every 50ms
  setInterval(draw,50);
}

startGame();

I moved the setting of onkeydown callback outside, so it will be there from the start. 我把onkeydown回调的设置移到了外面,所以它从一开始就会存在。

And in the startGame() , instead of calling the former function move repeatedly with an interval, I called draw since we want to redraw the canvas to update the game visually. 并且在startGame() ,我没有调用前一个函数以间隔重复move ,而是调用draw因为我们想要 draw布以直观地更新游戏。

The problem with your code was: 你的代码的问题是:

  1. In your startGame , you were repeatedly calling move every 2 seconds, in which, the function just registers an event handler to document.onkeydown , it doesn't really do anything. 在你的startGame ,你每2秒重复调用一次move ,其中,函数只是向document.onkeydown 注册一个事件处理程序,它实际上没有做任何事情。

  2. Since the handler is being registered, you can assure that the value of one.x really updates when pressing the A key, you can't just validate it because the canvas never updates. 由于处理程序正在注册,因此您可以确保在按下A键时one.x的值确实更新,您不能只是验证它,因为画布永远不会更新。

Here is a working JSFiddle: https://jsfiddle.net/8qjcfya9/ 这是一个有效的JSFiddle: https ://jsfiddle.net/8qjcfya9/

UPDATE UPDATE

I just refactored some part of the code, still works the same. 我刚刚重构了部分代码,仍然可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM