gpt4 book ai didi

从数组中删除的javascript

转载 作者:行者123 更新时间:2023-11-30 16:55:43 24 4
gpt4 key购买 nike

你好,我的代码可以运行,但是当我与星星发生碰撞时,它会删除​​我屏幕上的所有内容,但我需要它做的只是从屏幕上删除星星,但保留其他所有内容,因为我打算制作一个稍后收集的每颗星的记分牌。

stars=[];
stars.push({
x: 420,
y: 580,
width: 5,
height: 5

});


function update() {

var score = 0; // score starts at 0

// check keys
if (keys[38] || keys[32]) {
// up arrow or space
if (!player.jumping && player.grounded) {
player.jumping = true;
player.grounded = false;
player.velY = -player.speed * 2;
}
}
if (keys[39]) {
// right arrow
if (player.velX < player.speed) {
player.velX++;
}
}
if (keys[37]) {
// left arrow
if (player.velX > -player.speed) {
player.velX--;
}
}

player.velX *= friction;
player.velY += gravity;

ctx.clearRect(0, 0, width, height);
ctx.fillStyle = "black";
ctx.beginPath();

player.grounded = false;
for (var i = 0; i < boxes.length; i++) {
ctx.rect(boxes[i].x, boxes[i].y, boxes[i].width, boxes[i].height);

var dir = colCheck(player, boxes[i]);



if (dir === "left" || dir === "right") {
player.velX = 0;
player.jumping = false;
} else if (dir === "bottom") {
player.grounded = true;
player.jumping = false;
} else if (dir === "top") {
player.velY *= -1;
}

}
for (var i = 0; i < stars.length; i++) {
ctx.rect(stars[i].x, stars[i].y, stars[i].width, stars[i].height);

var dir = colCheck(player, stars[i]);

if(dir != null)
{
delete stars[0];
//stars.splice(a);
//var resultObject = search.splice(0,1);
}


if (dir === "left" || dir === "right") {
player.velX = 0;
player.jumping = false;
} else if (dir === "bottom") {
player.grounded = true;
player.jumping = false;
} else if (dir === "top") {
player.velY *= -1;
}

}


if(player.grounded){
player.velY = 0;
}

player.x += player.velX;
player.y += player.velY;

ctx.fill();
ctx.fillStyle = "blue";
ctx.fillRect(player.x, player.y, player.width, player.height);

requestAnimationFrame(update);
}

最佳答案

替换

delete stars[0];

stars.splice(i,1);
i--;

我没有看到任何其他错误。

关于从数组中删除的javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29755350/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com