Flixel Forums

development => help => Topic started by: GrimPanda on Sat, Jul 23, 2011

Title: indexOf() and FlxPoint
Post by: GrimPanda on Sat, Jul 23, 2011
If I have an array of FlxPoints, how would I use indexOf() to see if a FlxPoint already exists inside the array?

Example:

Code: [Select]
var pointArray:Array = new Array();
pointArray.push(new FlxPoint(0,0));
pointArray.push(new FlxPoint(0,1));
pointArray.push(new FlxPoint(1,1));
pointArray.push(new FlxPoint(1,3));

//The following line should not execute (IE a FlxPoint of 1,1 exists above)
if (!pointArray.indexOf(1,1)) pointArray.push(new FlxPoint(1,1);

So in the example, I have a point index of 1,1 in the array, but I have no idea how to actually check for that value... Thanks!!
Title: Re: indexOf() and FlxPoint
Post by: man2 on Sat, Jul 23, 2011
it seems that you can't do that. indexof() will search for instance reference not what it contains. So you will need to make a loop to find what the array contains, and if it doesn't find anything, create a new one

I usually do this:

Code: [Select]
var found:Boolean=false;
for each( var p:FlxPoint in pointArray )
{
    if( p.x == 1 && p.y ==1)
{
found=true;
                break;
}
}

if(found==false)
{
pointArray.push(new FlxPoint(1,1));
}
Title: Re: indexOf() and FlxPoint
Post by: GrimPanda on Sun, Jul 24, 2011
it seems that you can't do that. indexof() will search for instance reference not what it contains. So you will need to make a loop to find what the array contains, and if it doesn't find anything, create a new one

Ahh ok, glad to know I wasn't just missing something here.  That's pretty close to what I ended up doing as well.  Thanks!