How do i get Object index in an array

I have seen many threads about this but my problem doesn't solved. This may be a simple way but i have no idea...

I'm trying to get Objects indices in an array like so :

var test:Array = new Array();

for (var row:Number = 0; row < 2; row++) {
    test[row] = [];
    for (var column:Number = 0; column < 3; column++) {
        test[row][column].addEventListener(MouseEvent.CLICK, objClicked);
        test[row][column] = new ballShape(column, column, row);
        addChild(test[row][column]);
    }
}

function objClicked(evt:MouseEvent):void {
    // Here must return Object index in array
}

P.S :

I can get items index in int array, but i don't know about objects.

Any ideas would be appreciated.

Edit :

ballShape.as

package  {
    import flash.display.Graphics;
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import fl.motion.Color;

    public class ballShape extends Sprite {

    private var shapeId:uint;
    private var currentPosition:uint;
    private var arrayPosition:uint;
    private var color:Number;

    public function ballShape(Id:uint, currPos:uint, arrPos:uint) {
        setId(Id);
        setArrayPos(arrPos);
        setCurrentPos(currPos);

        //trace("Array : " + arrPos);
        //trace("Curr : " + currPos);

        if (arrPos == 0) {
            var posX:uint = 60;
        } else {
            var posX:uint = (arrPos + 1) * 60;
        }
        if (currPos == 0) {
            var posY:uint = 42;
        } else {
            var posY:uint = (currPos + 1) * 42;
        }
        if (arrPos == 0) {
            color = 0xFF0000;
        } else {
            color = 0x00FF00;
        }
        graphics.beginFill(color, 1.0);
        graphics.drawCircle(posX, posY, 20);
        graphics.endFill();     
        this.addEventListener(MouseEvent.CLICK, Clicked);
    }
    public function setId(Id:uint):void {
        shapeId = Id; 
    }
    public function getId():uint {
        return shapeId;
    }
    public function Clicked(evt:MouseEvent):void {
        //return getId();
        trace("Ball id is "  + getId());
        trace("Array id is " + getArrayPos());
        trace("PositionInArray id is "  + getCurrentPos());
        //return arrayPosition;     
    }
    public function setCurrentPos(Pos:uint):void {
        currentPosition = Pos;
    }
    public function getCurrentPos():uint {
        return currentPosition;
        trace(currentPosition);
    }
    public function setArrayPos(arrayPos:uint):void {
        arrayPosition = arrayPos;
    }
    public function getArrayPos():uint {
        return arrayPosition;
        trace(arrayPosition);       
    }
    public function addBead(arrayId, currPos):void {

    }
}

}

Answers


I would suggest adding row and column as public variables in your ballShape class. That way you can get them like this:

function objClicked(evt:MouseEvent):void {
    trace(ballShape(evt.target).getCurrentPos(), ballShape(evt.target).getArrayPos());
}

Maybe turn this two lines around:

test[row][column].addEventListener(MouseEvent.CLICK, objClicked);
test[row][column] = new ballShape(column, column, row);

to be :

test[row][column] = new ballShape(column, column, row);
test[row][column].addEventListener(MouseEvent.CLICK, objClicked);

Try something like:

protected function objClicked(e:MouseEvent):void {
    for (var row: int=0; row < test.length; row++) {
        var column:int = (test[row] as Array).indexOf(e.currentTarget);//use currentTarget so you don't try to match on internal Interactive Objects!
        if (column>-1) { 
           trace(row, column);
           return;//or break, if you need to do something else below this loop
        }
    }
}

Need Your Help

jQuery UI tabs preventing moving to another page in IE8

jquery jquery-ui

I have a weird issue with an image rotator I wrote using jQuery UI Tabs. The rotator works fine, however, if the user tries to move to another page either by clicking a link, using the URL toolbar,...

64-bit global hook locks up 32-bit applications

c++ winapi hook 64bit

I am attempting to create a simple DLL to hook into 64-bit applications on Win7 x64.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.