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;
  }
killereks
  • 189
  • 1
  • 12

2 Answers2

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.

  • didnt work either tried it before. it just comes with .shift is not a function or whatever i use –  killereks May 13 '16 at 18:37
  • @ZgniotekMc, I added a snippet, maybe you're testing your code wrong? –  May 13 '16 at 18:52
  • 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. –  killereks May 13 '16 at 18:54
  • can you post some code concerning creating/adding to buildQueue? –  May 13 '16 at 19:09
  • posted on top as a answer –  killereks May 13 '16 at 19:21
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.

killereks
  • 189
  • 1
  • 12
  • OMG MAN THANK YOU, I WAS DOING THE RIGHT THINGS ALL THE TIME BUT THE [""] WAS THE MISTAKE. THANKS I LOVE U :D –  killereks May 14 '16 at 18:23