Search the Community
Showing results for tags 'remove array item'.
-
Hi guys, I'm trying to optimize my game removing unused array objects. This code decrease the "Y" position of each array's objects, if y is minor than "-150", it removes the element. for (var a=0; a < testArray.length; a++) { if(testArray[a] != 'unload'){ if(testArray[a].position.y > '-150' ){ bonuSpeed = 130 + (5 * timerSpeed); speedRandom = Math.floor(Math.random() * 280) + bonuSpeed; testArray[a].position.y -= speedRandom * game.system.delta; // move sprite using delta time } else { //alert(testArray.length); testArray[a].remove(); testArray[a] = 'unload'; } } }Unfortunately, "testArray[a].remove();", doesn't work as expected so my first optimization was to change the value of testArray[a] with: testArray[a] = 'unload';So I can check if the element is equal to "unload" and skip the y variation. This is quite good, but when the array length is major than 500 some graphic problem arrives (like fps drop). So I tried to remove the unused testArray[a] with a function: function removeByIndex(arr, index) { //alert(testArray.length); arr.splice(index, 1); //alert(testArray.length);} for (var a=0; a < testArray.length; a++) { if(testArray[a] != 'unload'){ if(testArray[a].position.y > '-150' ){ bonuSpeed = 130 + (5 * timerSpeed); speedRandom = Math.floor(Math.random() * 280) + bonuSpeed; testArray[a].position.y -= speedRandom * game.system.delta; // move sprite using delta time } else { //alert(testArray.length); testArray[a].remove(); removeByIndex(testArray, a); } } }and partially works, the testArray length decreases but the game freezes. I think the problem is that the "For" checks the testArray's length, and I change this length during the cycle. Can anyone help me with this problem? Thank you ^^