How do i get Object index in an array

Hamed Kamrava

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 {

    }
}

}
putvande

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);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I get the index of an object in this array?

From Dev

How can I get the index of array element inside object?

From Dev

How do I get the index of each item in a groupby object in Pandas?

From Dev

How do i get the value of first index in a returned List of array

From Dev

How do I get the index of an array where it contains a string?

From Java

How do i get an array instead of an object using mongoose?

From Dev

How do I get the attributes from an object array?

From Dev

How do I get the attributes from an object array?

From Dev

How do I get the key for each object in a dynamic JSON array?

From Dev

AngularJS: how do i get data into an object and array?

From Dev

How to get an object index from an array in swift

From Dev

How to get index of an object in JSON array?

From Dev

How do I find the index of an integer array?

From Dev

How can I get the index of an object in an array with a property value that matches a specific criteria?

From Dev

How do I get the $index of element in an ng-repeat when repeating over an object?

From Dev

How do I get the $index of element in an ng-repeat when repeating over an object?

From Dev

How do I get index of line

From Dev

How do I get the ListTile index in an ExpansionTile?

From Dev

How do I get index of line

From Dev

How do I get Index of Listview?

From Dev

How do I create an flattened array of arrays that are part of an object in an array, essentially get the child arrays of multiple objects?

From Dev

How do I get and update an AngularJS object in an array, if I have it's id value?

From Dev

I need to get the Index of a 2d array object in Java

From Dev

In Julia, how do I get the index of the first element in a sorted array that exceeds a certain value?

From Dev

PHP - How do I unset an array index by value inside that index

From Dev

How do I format following array/object

From Dev

How do I add the value of an array to an object

From Dev

How do I display array object information?

From Dev

How do I get the value of a @ object attribute?

Related Related

  1. 1

    How do I get the index of an object in this array?

  2. 2

    How can I get the index of array element inside object?

  3. 3

    How do I get the index of each item in a groupby object in Pandas?

  4. 4

    How do i get the value of first index in a returned List of array

  5. 5

    How do I get the index of an array where it contains a string?

  6. 6

    How do i get an array instead of an object using mongoose?

  7. 7

    How do I get the attributes from an object array?

  8. 8

    How do I get the attributes from an object array?

  9. 9

    How do I get the key for each object in a dynamic JSON array?

  10. 10

    AngularJS: how do i get data into an object and array?

  11. 11

    How to get an object index from an array in swift

  12. 12

    How to get index of an object in JSON array?

  13. 13

    How do I find the index of an integer array?

  14. 14

    How can I get the index of an object in an array with a property value that matches a specific criteria?

  15. 15

    How do I get the $index of element in an ng-repeat when repeating over an object?

  16. 16

    How do I get the $index of element in an ng-repeat when repeating over an object?

  17. 17

    How do I get index of line

  18. 18

    How do I get the ListTile index in an ExpansionTile?

  19. 19

    How do I get index of line

  20. 20

    How do I get Index of Listview?

  21. 21

    How do I create an flattened array of arrays that are part of an object in an array, essentially get the child arrays of multiple objects?

  22. 22

    How do I get and update an AngularJS object in an array, if I have it's id value?

  23. 23

    I need to get the Index of a 2d array object in Java

  24. 24

    In Julia, how do I get the index of the first element in a sorted array that exceeds a certain value?

  25. 25

    PHP - How do I unset an array index by value inside that index

  26. 26

    How do I format following array/object

  27. 27

    How do I add the value of an array to an object

  28. 28

    How do I display array object information?

  29. 29

    How do I get the value of a @ object attribute?

HotTag

Archive