0

我已经看到很多关于这个的线程,但我的问题没有解决。这可能是一个简单的方法,但我不知道......

我正在尝试像这样在数组中获取对象索引:

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
}

PS:

我可以在数组中获取项目索引int,但我不知道对象。

任何想法,将不胜感激。

编辑 :

球形状.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 {

    }
}

}
4

2 回答 2

2

我建议在你的类中添加rowcolumn作为公共变量。ballShape这样你就可以像这样得到它们:

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

也许把这两条线转过来:

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

成为 :

test[row][column] = new ballShape(column, column, row);
test[row][column].addEventListener(MouseEvent.CLICK, objClicked);
于 2013-08-23T16:05:59.417 回答
0

尝试类似:

受保护的函数 objClicked(e:MouseEvent):void {
    for (var row: int=0; row < test.length; row++) {
        var column:int = (test[row] as Array).indexOf(e.currentTarget);//使用 currentTarget 这样你就不会尝试匹配内部交互对象!
        如果(列>-1){
           跟踪(行,列);
           return;// 或中断,如果您需要在此循环下执行其他操作
        }
    }
}
于 2013-08-23T15:49:08.297 回答