AS3: How to refer an object by his properties

240 Views Asked by At

Well, I'm doing a checkers game and I need to refer a piece by its position (x and y, both) and remove it from the screen (no problem with this).

I've been traying combinations with "this." but nothing. How would you do that?

1

There are 1 best solutions below

3
On

this.x and this.y are functional from the scope of your checkers pieces object; however, if you're accessing a piece outside of their scope, you must use a piece's instance name. Although not optimal, you could loop through children DisplayObjects.

// create a collection of your checker pieces
var checkers:Array = [];

// create a checker piece, whatever your DisplayObject class is.
var checker:Checker;
checkers.push(checker);

// add it to the stage, probably your game board
addChild(checker);    
checker.x = 100;
checker.y = 100;

// loop through the children (from your game board)
for (var i:uint = 0; i < numChildren; i++)
{
    var checker:DisplayObject = getChildAt(i);
    trace(checker.x);
    trace(checker.y);
}

Using coordinates to reference a piece may not be optimal for game play. You might want to consider a row / column or approach it from how your game board works.

If this is not clear, you should specify some code or expand your question with more detail.