从另一个对象的事件侦听器调用由for循环创建的按钮

时间:2011-03-20 13:35:36

标签: actionscript-3 button for-loop rollover

我制作了两个阵列,一个用于名称昆虫部分,另一个用于显示昆虫的部分。滚动数组创建的按钮和循环,部件和按钮亮起。但是我还希望在滚动昆虫部件时点亮部件和按钮。我的问题是我不知道如何在滚动昆虫部分时从循环中调用按钮。请参阅函数rollHandler2和outHandler2。

//the arrays look like this...

var parts:Array= [ "Cervical sclerites", "Protonotopleural lobe", "Proepisternum",...]
var shapes:Array = [pt0, pt1, pt2, ...]


for (var i:int = 0; i < parts.length; i++){
    var b:MovieClip = new Btn();
    but.x = 15;
    but.y = ((i)*(b.height+1))+10;
    but.name = "b"+i;
    but.info.text = parts[i];
    but.buttonMode = true;
    but.info.mouseEnabled=false;
    addChild(b);
    but.addEventListener(MouseEvent.ROLL_OVER, rollHandler);
    but.addEventListener(MouseEvent.ROLL_OUT, outHandler);
    //b.addEventListener(MouseEvent.CLICK, outHandler);

}

for (var j:int = 0; j < shapes.length; j++){
    shapes[j].buttonMode = true;
    shapes[j].addEventListener(MouseEvent.ROLL_OVER, rollHandler2);
    shapes[j].addEventListener(MouseEvent.ROLL_OUT, outHandler2);
}

function rollHandler(event:MouseEvent):void {
    for (var i:int = 0; i < parts.length; i++) {
        if (event.currentTarget.name == "b"+i) {
            this.shapes[i].gotoAndStop("over");
            event.currentTarget.gotoAndStop("over");
        }
    }
}

function outHandler(event:MouseEvent):void {
    for (var i:int = 0; i < parts.length; i++) {
        if (event.currentTarget.name == "b"+i) {
            this.but[i].gotoAndStop("start");
            event.currentTarget.gotoAndStop("start");
        }
    }
}

function rollHandler2(event:MouseEvent):void {
    for (var i:int = 0; i < shapes.length; i++) {
        if (event.currentTarget == shapes[i]) {
            this.parts[i].gotoAndStop("over");
                        //How should i call the button b created from the parts loop?
            event.currentTarget.gotoAndStop("over");
        }
    }
}

function outHandler2(event:MouseEvent):void {
    for (var i:int = 0; i < shapes.length; i++) {
        if (event.currentTarget == shapes[i]) {
            this.b[i].gotoAndStop("start");
                        //same problem here.. Not sure how to call it.
            event.currentTarget.gotoAndStop("start");
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我在你的代码中做了一些修改。看看评论,你应该明白我的意思。我的所有评论都标记为:// ---这是一个示例评论--- \\

//the arrays look like this...

var parts:Array= [ "Cervical sclerites", "Protonotopleural lobe", "Proepisternum",...];
var shapes:Array = [pt0, pt1, pt2, ...];
var buttons:Array = new Array(); //---Create new array to hold your buttons---\\


for (var i:int = 0; i < parts.length; i++){
    var b:MovieClip = new Btn();
    but.x = 15;
    but.y = ((i)*(b.height+1))+10;
    but.name = "b"+i;
    but.info.text = parts[i];
    but.buttonMode = true;
    but.info.mouseEnabled=false;
    buttons.push(b); //---Push your button into the array each time it loops---\\
    addChild(b);
    but.addEventListener(MouseEvent.ROLL_OVER, rollHandler);
    but.addEventListener(MouseEvent.ROLL_OUT, outHandler);
    //b.addEventListener(MouseEvent.CLICK, outHandler);

}

for (var j:int = 0; j < shapes.length; j++){
    shapes[j].buttonMode = true;
    shapes[j].addEventListener(MouseEvent.ROLL_OVER, rollHandler2);
    shapes[j].addEventListener(MouseEvent.ROLL_OUT, outHandler2);
}

function rollHandler(event:MouseEvent):void {
    for (var i:int = 0; i < parts.length; i++) {
        if (event.currentTarget.name == "b"+i) {
            this.shapes[i].gotoAndStop("over");
            event.currentTarget.gotoAndStop("over");
        }
    }
}

function outHandler(event:MouseEvent):void {
    for (var i:int = 0; i < parts.length; i++) {
        if (event.currentTarget.name == "b"+i) {
            this.but[i].gotoAndStop("start");
            event.currentTarget.gotoAndStop("start");
        }
    }
}

function rollHandler2(event:MouseEvent):void {
    for (var i:int = 0; i < shapes.length; i++) {
        if (event.currentTarget == shapes[i]) {
            this.parts[i].gotoAndStop("over");
                        //How should i call the button b created from the parts loop?
            buttons[i].gotoAndStop("over"); //---Then you call the button like this at each place.---\\
            event.currentTarget.gotoAndStop("over");
        }
    }
}

function outHandler2(event:MouseEvent):void {
    for (var i:int = 0; i < shapes.length; i++) {
        if (event.currentTarget == shapes[i]) {
            this.b[i].gotoAndStop("start");
                        //same problem here.. Not sure how to call it.
            event.currentTarget.gotoAndStop("start");
        }
    }
}

希望这有帮助!