00:00
00:00
Newgrounds Background Image Theme

Someone gifted MetalSlayer69 supporter status!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

AS2 Object Lag(?) on Array Splice

316 Views | 5 Replies
New Topic Respond to this Topic

AS2 Object Lag(?) on Array Splice 2023-09-22 18:01:28


Please preview this SWF by clicking the link here.


Click on the light grey rectangle to spawn some floaty grey circles.


When they reach the bottom of their origin point, they should delete themselves and fire a small bullet.


However, if you look carefully, you can see that when once a circle disappears, the ones spawned previously will slightly lag upwards. I think this is because of splicing the array of enemies while still letting a for loop cycle through it. If I simply remove the movieClip without splicing the array, this problem doesn't occur, but then I have an empty space in the array that I can't remove unless I do something like:

if(!game) {
  eArray.splice(i,1);
}

Would it be better to just splice the array or remove the movieClip and then splice the array later?


[1] - [2]

Response to AS2 Object Lag(?) on Array Splice 2023-09-22 18:28:07


At 9/22/23 06:01 PM, Nabella wrote: Please preview this SWF by clicking the link here.

Click on the light grey rectangle to spawn some floaty grey circles.

When they reach the bottom of their origin point, they should delete themselves and fire a small bullet.

However, if you look carefully, you can see that when once a circle disappears, the ones spawned previously will slightly lag upwards. I think this is because of splicing the array of enemies while still letting a for loop cycle through it. If I simply remove the movieClip without splicing the array, this problem doesn't occur, but then I have an empty space in the array that I can't remove unless I do something like:

Would it be better to just splice the array or remove the movieClip and then splice the array later?


Some people iterate through the array backwards, so if you remove and splice an item out, it won't affect all of the other ones. Other people add all of the things that need to be spliced to another array, and then iterate through there removing things from the main array.

Response to AS2 Object Lag(?) on Array Splice 2023-09-22 18:42:17


At 9/22/23 06:28 PM, MSGhero wrote:
Some people iterate through the array backwards, so if you remove and splice an item out, it won't affect all of the other ones. Other people add all of the things that need to be spliced to another array, and then iterate through there removing things from the main array.


Would iterating through the array backwards look something like this? I tried this just now and it seems pretty close to solving the problem but the first enemy spawned is frozen in place.

for(i = eArray.length; i > 0; i--) {
   //do stuff  
}


As for pushing things that need to be spliced into another array, would that mean I have to push one enemy into 2 arrays using the function I call to put them on stage or push them into an array while they're on stage and need to be deleted?


[1] - [2]


At 9/22/23 06:28 PM, MSGhero wrote:
Some people iterate through the array backwards, so if you remove and splice an item out, it won't affect all of the other ones. Other people add all of the things that need to be spliced to another array, and then iterate through there removing things from the main array.


Wait, nvm, I googled it, found an example, imitated it, and it seemingly solved the problem.

for(i = eArray.length-1; i >= 0; i--) {
	eObj = eArray[i];
		
	eBehav(eObj);
}


Still do elaborate on the two arrays, though. I would appreciate the insight.


[1] - [2]


At 9/22/23 06:50 PM, Nabella wrote:
At 9/22/23 06:28 PM, MSGhero wrote:
Some people iterate through the array backwards, so if you remove and splice an item out, it won't affect all of the other ones. Other people add all of the things that need to be spliced to another array, and then iterate through there removing things from the main array.
Wait, nvm, I googled it, found an example, imitated it, and it seemingly solved the problem.

Still do elaborate on the two arrays, though. I would appreciate the insight.


Not entirely sure if I understood what @MSGHero meant - feel free to correct me if I didn't.


But I guess it would be something like this?

var array:Array = [1, 2, 3, 4, 5, 6, 7, 8];
// array to store items to splice
var spliceResult:Array = [];
for (var i:Number = 0; i < array.length; ++i) {
    // say we want to remove all even numbers
    if(array[i] % 2 == 0) {
        spliceResult.push(array[i]);
    }
}
// now loop through both arrays to remove them from the main array
for each(var number:Object in spliceResult) {
    for (var i:Number = 0; i < array.length; ++i) {
        if(array[i] == number) {
            array.splice(i, 1);
        }
    }
}

Which would indeed remove all the numbers, but is probably inefficient so I might've been thinking of something else.


An alternative is to filter out the elements to be spliced by taking the inverse -- that is, creating a new array consisting of elements that should be kept rather than removed. From the above example..

var array:Array = [1, 2, 3, 4, 5, 6, 7, 8];
var results:Array = [];
for (var i:Number = 0; i < array.length; ++i) {
    // say we again want to remove even numbers; then add the odd ones to results
    if (array[i] % 2 != 0) {
        results.push(array[i]);
    }
}
// toss the old array by assigning it to `results`
array = results


In AS3 and JavaScript you could use the inbuilt array methods to simplify this down:

var array = [1, 2, 3, 4, 5, 6, 7, 8];
var results = array.filter(function(item) {
    return item % 2 != 0
})

but AS2 doesn't have that syntactic sugar unfortunately.


Slint approves of me! | "This is Newgrounds.com, not Disney.com" - WadeFulp

"Sit look rub panda" - Alan Davies

BBS Signature

Response to AS2 Object Lag(?) on Array Splice 2023-09-23 20:11:58


I meant the first of @Gimmick's comments, not as an efficient solution but one that works and helps you understand why your original code doesn't work. But glad the backwards iteration worked.