0

So when writing a game on Khan Academy When I try to remove a bullet from the array I run into the error "Object does not support method splice" I have been checking my code for hours and have not found out why it does not work. Ideas?

EDIT: The code used to remove a bullet is bullets[i].splice(i,1); and that is what errors out my code.

MVCE:

var bullets = [];

var bullet= function(x,y,blah)
{
    //code that is not important here
};

bullets.push(bullet(0,0,30));
for(var I = 0; I < bullets.length; I++){
if(bulletRemove){
bullets[I].splice(i,1)
}
}
  • 2
    post your code here. we don't go "outside" to look at it. – Marc B Jun 20 '16 at 14:42
  • 1
    Can you please post your code here? The code you linked doesn't make a ton of sense- every call to the `splice()` function is commented out, so I'm not sure how your code can generate the error you're talking about. – Kevin Workman Jun 20 '16 at 14:42
  • Just found out that I could select all then hit Ctrl+k to indent –  Jun 20 '16 at 14:58
  • 1
    exactly WHERE in all that is the error occurring? we're not going to count down 548 lines... – Marc B Jun 20 '16 at 15:00
  • Go to the very bottom draw function. Scroll up a little. You will see a if statement saying –  Jun 20 '16 at 15:01
  • if(rectCircleCollide(player.x,player.y,bSize,bSize,null,bullets[i].giveCords("x"),bullets[i].giveCords("y"),10)){ –  Jun 20 '16 at 15:01
  • that is the error spot –  Jun 20 '16 at 15:01
  • @MarcB sorry about that –  Jun 20 '16 at 15:05

1 Answers1

1

You have a variable named bullets:

var bullets = [];

(Side note: Why is there a random curly bracket right before this line?)

This bullets variable is an array. It holds instances of the Bullet class:

bullets.push(new Bullet(x, y, 10, player.x+bSize/2, player.y+bSize/2));

You can use the array to access a Bullet at a particular index, and then you can call functions of the Bullet class on that instance:

bullets[i].move();

You can also call the splice() function on the array itself:

bullets.splice(i,1);

However, you can't call the splice() function on a particular Bullet instance!

bullets[i].splice(i,1);

This line is taking an instance of Bullet from the i index of the bullets array, and then trying to call the splice() function from the Bullet class. But the Bullet class doesn't have a splice() function! This is what's causing the error.

Instead, you probably meant to call it on the array itself:

bullets.splice(i,1);

In the future, please please please try to narrow your problem down before posting a question. Try to post an MCVE instead of your entire project. You could have put together an example program that used just a few lines to create a hard-coded array and used that to demonstrate your problem. Chances are you would have found the problem yourself in the process of creating the MCVE!

Community
  • 1
  • 1
Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
  • I will work on making MCVE in the future. Thanks so much for the help! –  Jun 20 '16 at 15:46
  • Also the curly brackets are used on khan academy to group code. so if I have a section of code about collisions (as I do) and I do the following { "collision code here" } and put functions in the collision code I can click on a arrow by the first bracket and compress all the code into one line so you just see {<=>} instead of a lot of code. It helps organize the code into sections. –  Jun 20 '16 at 15:49
  • @ChristopherPeart Fair enough about the curly brackets. I might argue that these sections should be split into their own functions then. Using random curly brackets seems a bit strange, mostly because it's not obvious to me what the scope of variables defined inside these blocks are. Seems like a recipe for more bugs. – Kevin Workman Jun 20 '16 at 15:54
  • If you would like go to this link https://www.khanacademy.org/computer-programming/escape/6556810072817664 that I have my program and you can see what I mean better –  Jun 20 '16 at 15:55
  • @ChristopherPeart I'm behind a firewall that blocks that site (this is why we ask for code in the question itself, it's not **just** us being lazy), but I understand what you mean. But the scope of the variable is external to the block, which is again not very obvious to me. [Quote from MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/block): "Although "standalone" blocks are valid syntax, you do not want to use standalone blocks in JavaScript, because they don't do what you think they do, if you think they do anything like such blocks in C or Java." – Kevin Workman Jun 20 '16 at 16:02
  • Huh. I just use them to group code so I can find the code I need faster. I don't try to use them to have local variables. –  Jun 20 '16 at 16:31
  • @ChristopherPeart If that's your preference, I say go for it. I think it makes your code slightly harder for other people to read, and splitting it into functions would be a better way to organize your code. But go with whatever fits into your head the best. – Kevin Workman Jun 20 '16 at 16:35
  • 1
    Looking back at old answers I realized how good of an idea that is. People like you keep SO alive (and me too) –  Aug 25 '16 at 00:05
  • 1
    @ChristopherPeart Thanks, that's awesome to hear! – Kevin Workman Aug 25 '16 at 00:30