0

This error kept troubling me for about 2 hours now... I'm making an idle game where you can have your own city and I'm making a building system right now, the problem is the game crashes whenever I delete from array (I have build queue which holds buildings to be built and then removes them) building from build queue. I tried .shift .pop .push .indexOf(0) === 0 and [0] === "" and .splice(1,1) it just comes up with like .splice is not a function or .pop is not a function for all of them.

Nothing worked. Please HELP!

  if (buildValue === 100 && buildQueue.indexOf("house") === 0){
    populationmax++;
    // here i need a command that will remove first element from array called buildQueue.
    buildValue = 0;
  }
3
  • 2
    arr.splice(indexOfItemToDelete,1) Commented May 13, 2016 at 18:01
  • duplicate of stackoverflow.com/questions/369602/… Commented May 13, 2016 at 18:03
  • Now that I've seen the other code, I've edited my answer to fix your problem. Commented May 13, 2016 at 22:47

2 Answers 2

1

Removing From Array

if (buildValue === 100 && buildQueue.indexOf("house") === 0){
  populationmax++;
  buildQueue.splice(0, 1); //removes first element
  buildValue = 0;
}

JS Snippet

x = [1, 2, 3];
alert(x); //1,2,3
x.splice(0, 1);
alert(x); //2,3

Adding To/Creating Array

First, you don't need to put a blank string inside the buildQueue array, this might actually cause problems later, just do this:

buildQueue = [];

Second, you are trying to add strings to your array as if it were a string, using +=. Doing this however, is turning your array into a string, which is why you're getting the warning about `.splice()' you need to add strings to your array like this:

buildQueue.push(someString);

This way buildQueue will remain an array of strings.

Sign up to request clarification or add additional context in comments.

5 Comments

didnt work either tried it before. it just comes with .shift is not a function or whatever i use
@ZgniotekMc, I added a snippet, maybe you're testing your code wrong?
I dont know, i swear i use it right way, i used it before a lot. And now it just comes up with .splice is not a function. i tried putting it in separate function nothing. I need to fix it fast because im stuck on this things and cant develop my game further.
can you post some code concerning creating/adding to buildQueue?
posted on top as a answer
0
var buildValue = 0,
    buildQueue = [""],
    buildSpeed = 1/200;
  if (buildQueue[0]){
    buildValue += buildSpeed;
  }
  if (buildValue >= 100){
    buildValue = 100;
  }
  if (buildValue === 100 && buildQueue.indexOf("house") === 0){
    populationmax++;

    buildValue = 0;
  }
  if (buildValue === 100 && buildQueue.indexOf("big house") === 0){
    populationmax+=4;

    buildValue = 0;
  }
  if (buildValue === 100 && buildQueue.indexOf("gold storage") === 0){
    goldmax++;

    buildValue = 0;
  }
  if (buildValue === 100 && buildQueue.indexOf("food storage") === 0){
    foodmax++;

    buildValue = 0;
  }
  if (buildValue === 100 && buildQueue.indexOf("wood storage") === 0){
    woodmax++;

    buildValue = 0;
  }
  if (buildValue === 100 && buildQueue.indexOf("stone storage") === 0){
    stonemax++;

    buildValue = 0;
  }
  if (buildValue === 100 && buildQueue.indexOf("iron storage") === 0){
    ironmax++;

    buildValue = 0;
  }

  buildSpeed = 0.2;

That is all i have to do with build. Also if you buy a building it will just add to array. eg gold storage will add buildQueue += "gold store"; And the spaces between lines inside ifs are supposed to have command that deletes the [0] element.

1 Comment

OMG MAN THANK YOU, I WAS DOING THE RIGHT THINGS ALL THE TIME BUT THE [""] WAS THE MISTAKE. THANKS I LOVE U :D

Your Answer

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.