Author Topic: indexOf() and FlxPoint  (Read 935 times)

GrimPanda

  • Member
  • **
  • Posts: 46
  • Karma: +1/-0
    • View Profile
    • Grim Panda Games
indexOf() and FlxPoint
« 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!!
"Mortis Ailuropoda"

man2

  • Member
  • **
  • Posts: 11
  • Karma: +0/-0
    • View Profile
Re: indexOf() and FlxPoint
« Reply #1 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));
}
« Last Edit: Sat, Jul 23, 2011 by man2 »

GrimPanda

  • Member
  • **
  • Posts: 46
  • Karma: +1/-0
    • View Profile
    • Grim Panda Games
Re: indexOf() and FlxPoint
« Reply #2 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!
"Mortis Ailuropoda"