Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript游戏,如何改变给出动作的键_Javascript_Html - Fatal编程技术网

JavaScript游戏,如何改变给出动作的键

JavaScript游戏,如何改变给出动作的键,javascript,html,Javascript,Html,我在这里使用游戏,在这个版本中,加速的关键是 用鼠标左键点击,我想通过使用键码(“32”)让空格键加速工作,但我不知道该把这行放在哪里以及如何写 我尝试的代码如下: 我所期望的是,我可以通过按空格键(“32”)来加速。 但现在,它只需要用鼠标左键点击就可以了。不确定while.keyup时你想用做什么,但你所需要的只是 window.addEventListener("keydown", function(event) { if (event.keyCode == 32) { ac

我在这里使用游戏,在这个版本中,加速的关键是 用鼠标左键点击,我想通过使用键码(“32”)让空格键加速工作,但我不知道该把这行放在哪里以及如何写

我尝试的代码如下:

我所期望的是,我可以通过按空格键(“32”)来加速。
但现在,它只需要用鼠标左键点击就可以了。不确定while.keyup时你想用
做什么,但你所需要的只是

window.addEventListener("keydown", function(event) {
  if (event.keyCode == 32) {
    accelerate(-0.2)
  }
});

window.addEventListener("keyup", function(event) {
  if (event.keyCode == 32) {
    accelerate(0.05)
  }
});
您还需要防止使用空格键滚动,这可以通过添加
event.preventDefault()来实现
并进行检查以确保
event.target==document.body

工作代码段:

var-myGamePiece;
var myobstackes=[];
var myScore;
函数startName(){
myGamePiece=新组件(30,30,“红色”,10,120);
myGamePiece.gravity=0.05;
myScore=新组件(“30px”、“控制台”、“黑色”、280、40、“文本”);
myGameArea.start();
}
var myGameArea={
画布:document.createElement(“画布”),
开始:函数(){
this.canvas.width=480;
this.canvas.height=270;
this.context=this.canvas.getContext(“2d”);
document.body.insertBefore(this.canvas,document.body.childNodes[0]);
这个.frameNo=0;
this.interval=setInterval(updateGameArea,20);
},
清除:函数(){
this.context.clearRect(0,0,this.canvas.width,this.canvas.height);
}
}
功能组件(宽度、高度、颜色、x、y、类型){
this.type=type;
这个分数=0;
这个。宽度=宽度;
高度=高度;
这是0.speedX=0;
该值为0;
这个.x=x;
这个。y=y;
这个重力=0;
这个重力速度=0;
this.update=函数(){
ctx=myGameArea.context;
如果(this.type==“text”){
ctx.font=this.width+“”+this.height;
ctx.fillStyle=颜色;
ctx.fillText(this.text,this.x,this.y);
}否则{
ctx.fillStyle=颜色;
ctx.fillRect(this.x,this.y,this.width,this.height);
}
}
this.newPos=函数(){
this.gravitySpeed+=this.gravity;
this.x+=this.speedX;
这个.y+=这个.speedY+这个.gravitySpeed;
这个;
}
this.hitbooth=函数(){
var rockbottom=myGameArea.canvas.height—this.height;
如果(this.y>rockbottom){
y=最低点;
这个重力速度=0;
}
}
this.crashWith=函数(otherobj){
var myleft=this.x;
var myright=this.x+(this.width);
var mytop=this.y;
var mybottom=this.y+(this.height);
var otherleft=otherobj.x;
var otherright=otherobj.x+(otherobj.width);
var othertop=otherobj.y;
var otherbottom=otherobj.y+(otherobj.height);
var-crash=true;
如果((mybottomotherbottom)| |(myrightotherright)){
崩溃=错误;
}
返回碰撞;
}
}
函数updateGameArea(){
变量x,高度,间隙,最小高度,最大高度,最小间隙,最大间隙;
对于(i=0;i

帆布{
边框:1px实心#D3;
背景色:#f1f1;
}

加快 使用加速按钮保持在空中

你能活多久


我应该把它放在哪里?event.preventDefault();和event.target==document.bodyIt已经在底部我编辑的代码段中。在检查键代码的地方,还检查了
event.target==document.body
event.preventDefault()
在那些
addEventListener
函数中。是的,我明白了,谢谢你的时间,祝你度过愉快的一天。