间隔未被clearInterval()清除
interval not getting cleared by clearInterval()
本文关键字:清除 clearInterval 更新时间:2023-09-26
我一直在玩画布,我试图做一个移动和跳跃的正方形,移动部分完成了,但跳跃部分有一个问题:它跳得更快每次你跳
这里有一个jsfield
,下面是代码:
///////////////////////////////////////////////////////////////////////////////
// VARIABLES //
///////////////////////////////////////////////////////////////////////////////
var canvas = document.getElementById("arena");
var ctx = canvas.getContext("2d");
var square = new Player("#330099", 32, 32);
var keys = [];
var velX = 0;
var speed = 50;
var friction = 0.8;
var jumping = false;
var jumpInterval;
///////////////////////////////////////////////////////////////////////////////
// OBJECTS //
///////////////////////////////////////////////////////////////////////////////
function Player(color, width, height, jumpHeight, drawAction){
this.color = color || "#000000";
this.width = width || 50;
this.height = height || 50;
this.jumpHeight = jumpHeight || 100;
this.x = canvas.width/2;
this.y = canvas.height-this.height;
this.draw = drawAction || function(){
ctx.fillStyle = this.color;
ctx.fillRect(this.x, this.y, this.width, this.height);
};
};
///////////////////////////////////////////////////////////////////////////////
// EVENT LISTENERS //
///////////////////////////////////////////////////////////////////////////////
document.body.addEventListener("keydown", function (e) {
keys[e.keyCode] = true;
});
document.body.addEventListener("keyup", function (e) {
keys[e.keyCode] = false;
});
///////////////////////////////////////////////////////////////////////////////
// FUNCTIONS //
///////////////////////////////////////////////////////////////////////////////
function Jump(){
if(jumping == true){
if(square.y+square.height > canvas.height-square.jumpHeight){
square.y--;
console.log("jumping");
} else { jumping = false; console.log("peak reached"); }
}
if(jumping == false){
if(square.y < canvas.height-square.height){ square.y++; console.log("falling"); }
else { clearInterval(jumpInterval); console.log("interval cleaned"); }
}
}
///////////////////////////////////////////////////////////////////////////////
// UPDATE & DRAW //
///////////////////////////////////////////////////////////////////////////////
function update(){
window.requestAnimationFrame(update);
if(keys[37]){ //left arrow
if(velX > -speed){
velX--;
}
}
if(keys[39]){ //right arrow
if(velX < speed){
velX++;
}
}
if(keys[32]){ //space bar
if(jumping == false){
jumping = true;
}
jumpInterval = setInterval(Jump, 10);
}
/*
if(keys[39] == false){
jumping = false;
jumpInterval = setInterval(Jump, 10);
}
*/
if(velX != 0){
velX *= friction;
}
square.x += velX;
draw()
}
function draw(){
ctx.clearRect(0, 0, canvas.width, canvas.height);
square.draw();
}
update()
控制台不断打印出"interval cleaned",因此看起来它实际上并没有清除它。
这意味着我每次跳跃的间隔越来越多,这就解释了这个问题,除了我不知道为什么它没有被清除!
(当我向右移动并跳跃时,它似乎也走得更快)
问题是,当空格键被按下时,您为每个update
调用设置了一个新的间隔:
if(keys[32]){ //space bar
if(jumping == false){
jumping = true;
}
jumpInterval = setInterval(Jump, 10);
}
但最有可能的是,update
将在按空格键时被调用多次。在这种情况下,您将只清除最后一个间隔。
if(keys[32]){ //space bar
if(jumping == false){
jumping = true;
jumpInterval = setInterval(Jump, 10);
}
}
///////////////////////////////////////////////////////////////////////////////
// VARIABLES //
///////////////////////////////////////////////////////////////////////////////
var canvas = document.getElementById("arena");
var ctx = canvas.getContext("2d");
var square = new Player("#330099", 32, 32);
var keys = [];
var velX = 0;
var speed = 50;
var friction = 0.8;
var jumping = false;
var jumpInterval;
///////////////////////////////////////////////////////////////////////////////
// OBJECTS //
///////////////////////////////////////////////////////////////////////////////
function Player(color, width, height, jumpHeight, drawAction) {
this.color = color || "#000000";
this.width = width || 50;
this.height = height || 50;
this.jumpHeight = jumpHeight || 100;
this.x = canvas.width / 2;
this.y = canvas.height - this.height;
this.draw = drawAction || function() {
ctx.fillStyle = this.color;
ctx.fillRect(this.x, this.y, this.width, this.height);
};
};
///////////////////////////////////////////////////////////////////////////////
// EVENT LISTENERS //
///////////////////////////////////////////////////////////////////////////////
document.body.addEventListener("keydown", function(e) {
keys[e.keyCode] = true;
});
document.body.addEventListener("keyup", function(e) {
keys[e.keyCode] = false;
});
///////////////////////////////////////////////////////////////////////////////
// FUNCTIONS //
///////////////////////////////////////////////////////////////////////////////
function Jump() {
if (jumping == true) {
if (square.y + square.height > canvas.height - square.jumpHeight) {
square.y--;
console.log("jumping");
} else {
jumping = false;
console.log("peak reached");
}
}
if (jumping == false) {
if (square.y < canvas.height - square.height) {
square.y++;
console.log("falling");
} else {
clearInterval(jumpInterval);
console.log("interval cleaned");
}
}
}
///////////////////////////////////////////////////////////////////////////////
// UPDATE & DRAW //
///////////////////////////////////////////////////////////////////////////////
function update() {
window.requestAnimationFrame(update);
if (keys[37]) { //left arrow
if (velX > -speed) {
velX--;
}
}
if (keys[39]) { //right arrow
if (velX < speed) {
velX++;
}
}
if (keys[32]) { //space bar
if (jumping == false) {
jumping = true;
jumpInterval = setInterval(Jump, 10);
}
}
if (velX != 0) {
velX *= friction;
}
square.x += velX;
draw()
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
square.draw();
}
update()
#arena {
border: 2px solid black;
}
<canvas id="arena" width="400px" height="200px;"></canvas>
相关文章:
- document.open/document.write没有正确地清除chrome中的文档——这是chrome的错误吗
- 单击jquery清除输入值
- 如何在Microsoft VirtualEarth 6.3中使用纯javascript清除整个形状层
- 清除以前的$_GET值或不获取仅隐藏字段的值
- setInterval和clearInterval javascript无法按需工作
- clearInterval 不起作用
- Javascript清除缓存以清除基本身份验证凭据
- clearInterval在Javascript中不起作用
- 清除启动日期选取器值
- 在 SlickGrid 中编辑后,根据不同列中的另一个单元格清除单元格
- php页面,该页面在清除值后接受输入
- 清除数据并将旧值附加到文本框中
- 使用javascript清除Asp.NET FreeTextBox(RTF文本框)
- 如何使Visual Studio自动清除Javascript控制台
- 使用javascript或html本身清除文本字段
- 在初始化计时器之前,使用 clearInterval 清除计时器值
- JS clearInterval() 不会清除
- Javascript clearInterval()不能清除间隔
- 在函数中使用clearInterval来清除另一个函数中的setInterval
- 间隔未被clearInterval()清除