0

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 {

    }
}

}
7
  • You want to get the index that the object has in the array? I don't think you can get that in this way. You can get the childindex from the parent but not row and / or column values Commented Aug 23, 2013 at 15:31
  • I want to get object's index in array which was clicked. Commented Aug 23, 2013 at 15:34
  • The problem is that you have a multidimensional Array. So you'll at least need to loop through each row and then use indexOf() to return the column within the row. So you won't just have one index, but two. There might be better ways to accomplish this, like putting each row in a Sprite and then getting the child index within the parent. Commented Aug 23, 2013 at 15:36
  • @AmyBlankenship Tnx. Can you give me an example? Commented Aug 23, 2013 at 15:38
  • Is ballShape a class your created yourself? Commented Aug 23, 2013 at 15:44

2 Answers 2

2

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);
Sign up to request clarification or add additional context in comments.

4 Comments

I've tried your approach but get me this error Access of possibly undefined property row through a reference with static type ballShape. Any ideas?
I have changed my answer, since you had public functions to get the row and column from your own class.
Thank you for your reply. The error changed to this TypeError: Error #1010: A term is undefined and has no properties. at ballShape_fla::MainTimeline/frame1() .
See my answer update. Might be that you need to change does lines around.
0

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
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.