00:00
00:00
Newgrounds Background Image Theme

Dunso just joined the crew!

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!

hiTest Array against itself problem

421 Views | 3 Replies
New Topic Respond to this Topic

I'm running into a problem that I've never had before. I'm hitTesting the same array against itself, but I'm now trying to remove the two objects that are colliding and replace them with one object of the same kind.

for (var i:int = Level.waterCubeArray.length - 1; i >= 0; i--){
	var water = Level.waterCubeArray[i];
					
	for (var i2:int = Level.waterCubeArray.length - 1; i2 >= 0; i2--){
		var water2 = Level.waterCubeArray[i2];
					
		if(water == water2) continue;
					
			if(water.hitTestObject(water2)){
							
				water.parent.removeChild(water);
				water2.parent.removeChild(water2);
							
				Level.waterCubeArray.splice(Level.waterCubeArray.indexOf(water), 1);
				Level.waterCubeArray.splice(Level.waterCubeArray.indexOf(water2), 1);
							
					water.produceCube(water);
							
							
			}
					
		}
					
	}

"water.produceCube(water)" creates another block of water and adds it to the same array. One of the problems seems to be that the new block gets tested against the two blocks that disappear to create it. That seems to be what's happening. Also, if three blocks are hitting against each other, I get errors and other strange behavior.

Is my logic wrong on this?

Response to hiTest Array against itself problem 2016-04-11 16:07:09


At 4/11/16 02:14 PM, Barzona wrote:

Since you are deleting elements from an array while still inside the for loop, I assume you are getting some "outside bounds" errors. Correct?

If that is the case, what I do is create an array before the for loops to store the index of the element that is going to be removed. After the for loops are done, you can iterate over that new array and do something like this:

for (var i:int = 0; i < spliceArray.length; i++){
     Level.waterCubeArray.splice(spliceArray[i], 1);
}

$$$ | Strawberry Dodge | Abusive Reviews! | Sig by TheDingo

BBS Signature

var len:int = Level.waterCubeArray.length;
var intersectIndexes:Vector.<int> = new Vector.<int>();
var i:int;

for (i = len - 1; i >= 0; i--){
	var water:DisplayObject = Level.waterCubeArray[i];
	
	for (var j:int = len - 1; j >= 0; j--){
		var water2:DisplayObject = Level.waterCubeArray[j];
		
		if (water === water2) continue;
		
		if (water.bounds.intersects(water2.bounds)) {
			intersectIndexes.push(i, j);
			water.produceCube(water);
		}
	}
}

for (i = 0; i < intersectIndexes.length; i++) {
	var water:DisplayObject = Level.waterCubeArray[i];
	water.parent.removeChild(water);
	Level.waterCubeArray.splice(i, 1);
}

Programming stuffs (tutorials and extras)

PM me (instead of MintPaw) if you're confuzzled.

thank Skaren for the sig :P

BBS Signature

Response to hiTest Array against itself problem 2016-04-12 01:00:33


Thank you for your help, people. I went a slightly different direction for this problem instead. Instead of removing both blocks and adding a third, I just combined the information of the new block of water with the old one and then remove the new block. Works out better than what i was thinking before.

I'll still need to use the code you gave me in the future for something else I'm doing, so I definitely appreciate your help.