1

hello okay my code runs but when i make a collision with the star it erases everything on my screen but what i need it to do is just remove the star from the screen but keep everything else in place because i plan to make a scoreboard for each star collected later on.

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);
 }
asked Apr 20, 2015 at 18:13
0

2 Answers 2

2

replace

delete stars[0];

with

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

I don't see anything else that is wrong.

answered Apr 20, 2015 at 18:27
Sign up to request clarification or add additional context in comments.

3 Comments

i figured it out its stars.splice(i,1); :D i was bothered by this for past 2 hours but simple mistake :P
if you dont want to change the loop direction like Pluto said just add i--; after the splice as i edited it to show
Just make sure the i-- only appears where you remove an element. Otherwise you'll have an infinite loop.
0

You'll want to traverse through the array backwards. This is because the length of the array will change when you remove elements (and indices of elements at the end will be reduced by one), so your current code would skip some of the elements. In addition, as deme72 said, you'll want to use .splice(index, count) to remove items from the array. See the updated code below:

for (var i = stars.length - 1; i >= 0; i--) {
 // Other code
 stars.splice(i, 1);
}
answered Apr 20, 2015 at 18:38

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.