00:00
00:00
Newgrounds Background Image Theme

GodsPurpose 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!

Array is splicing wrong objects

522 Views | 5 Replies
New Topic Respond to this Topic

Array is splicing wrong objects 2012-01-20 21:00:46


Ok, so I have a stageMovement Array that grabs hold of everything except the UI in my game, so I can use a for statement as a virtual camera. Anyways: everything works just fine until I spam spells.

The spells are contained in another array than the hero, but for some reason when I splice out like 15 spells at the same time, I invariably splice the hero out.

I realize that my code is probably an abomination, but that's how you get better right?

This is my vcam forloop

function dostagemovement()
		{
			if (Hero[0].x > 700)
			{
				for (i=stageMove.length-1; i>=0; i--)
				{
					//make a ref to current object
					ob = stageMove[i];
					st = new stageMovement(ob,.5,-(Hero[0].x/125),0);
				}
			}
			if (Hero[0].x < 200)
			{
				for (i=stageMove.length-1; i>=0; i--)
				{
					//make a ref to current object
					ob = stageMove[i];
					st = new stageMovement(ob,.5,((900-Hero[0].x)/125),0);
				}
			}
		}

This is (I suspect) the offending for loop

for (i=Magic.length-1; i>=0; i--)
			{
				if (Magic[i].HP < 0)
				{
					Magic.splice(i,01);
					stageMove.splice(Magic[i],1);
				}
			}

Response to Array is splicing wrong objects 2012-01-20 21:03:00


SO basically if I cast like 20 spells in quick succession the camera for loop only tweens the new spells. because the hero and all of the stage get spliced out for no reason that I can see.

Response to Array is splicing wrong objects 2012-01-20 22:25:46


WOOOO!

it works, I can't help but feel like this is a super wasteful way to handle the problem, but I can't for the life of me figure out why it was splicing the wrong objects...
Two days of slamming my head against the wall and it was so simple.

for (n=Magic.length-1; n>=0; n--)
			{
				if (Magic[n].HP < 0)
				{
					Magic.splice(n,01);
					for (i=stageMove.length-1; i>=0; n--)
					{//JUST BELOW HERE IS WHAT CHANGED
						if (stageMove[i] == Magic[n])
						{
							stageMove.splice(Magic[n],1);
							Magic.splice(Magic[n],1);
						}

					}
				}
			}

Response to Array is splicing wrong objects 2012-01-21 03:39:05


Wait no... nevermind, the spells work now, but they dont kill anything in the Animals array.
I think I'm just going to start over from scratch.

Do you guys know if there is some weird relationship between Boolean statements and for loops?
It seems like if I make a for loop that asks:

for (var i=Array-1;i>=0;i--){
if (Array[i].Boolean==true){
Array.splice(Array[i],1)}
}

it splices every goddamn thing in the array the second the Boolean is true for one of the items in the array. Is it because I leave the Boolean as just "var varname:Boolean=false;" when I define them?

does that make them an attribute of every item in that class? one that gets changed globally?

Because it doesn't seem like that's the case...

I'm just pretty confused about this, because I really thought I understood how for-loops and arrays worked together to access specific objects in an array, but I must have seriously misunderstood something. I'll keep searching, but I really could use a hand.

Response to Array is splicing wrong objects 2012-01-21 05:07:38


At 1/21/12 03:39 AM, bonkanailios wrote: for (var i=Array-1;i>=0;i--){
if (Array[i].Boolean==true){
Array.splice(Array[i],1)}
}

This code has a lot of problems:

You cannot use a class name as a variable name. "Array" is the name of the class. Also any variable name should be descriptive and even though your example is made up for demonstration purposes, you should not use generic names like "array".

var booleans:Array = [true, false, false];//initialize the array with 3 boolean values

Now to your code:

for (var i=Array-1;i>=0;i--){

Array -1 makes no sense. That's like saying "car -1". You cannot subtract 1 from a car.
You can subtract 1inch from its length, or subtract 1 from the number of wheels it has.

You should always type your variables.

To get the array length, use the length property:

for (var i:int=booleans.length-1;i>=0;i--){

---

if (Array[i].Boolean==true){

First of all, the comparison with "true" is pretty much useless, as the value itself already is a boolean value either true or false.
You are trying to access the i-th element of the Array (which is fine) but then you try to access a property "Boolean" on this element (which does not exist on the elements, because your elements are booleans and they do not have any properties.
On top of that, you are again using a class name.

It should be:

if(booleans[i])

---

Array.splice(Array[i],1)}

Do you know what you are doing here? If you are unsure about what the splice method does and how to use it, consult the help. RTFM!
You are passing the i-th element of the array (which is a boolean) as the first parameter to the function, which is expecting an integer value.

The whole code looks like this:

var booleans:Array = [true, false, false];
			for (var i:int = booleans.length - 1; i >= 0; i--) if (booleans[i]) booleans.splice(i, 1);
			trace(booleans);
I'll keep searching, but I really could use a hand.

This is really basic stuff. You should not go on before you haven't figured out how this works.
http://active.tutsplus.com/tutorials/act ionscript/as3-101-loops/
http://active.tutsplus.com/tutorials/act ionscript/as3-101-arrays/

This is a multi part tutorial, you should at least read all of them.

Response to Array is splicing wrong objects 2012-01-21 08:59:13


Thanks for the links, I actually did know a lot of that I was just exasperated when I wrote the post. I always forget to type.length, I will think more carefully and phrase my question better next time. I will also follow the links and read them carefully. Thanks again for taking time to help.