0

我制作了 2 个阵列,一个用于命名昆虫部位,另一个用于显示昆虫的部位。滚动由数组和 for 循环创建的按钮,部件和按钮会亮起。但我也希望部件和按钮在翻过昆虫部件时亮起。我的问题是我不知道在翻过昆虫部位时如何从循环中调用按钮。请参阅函数 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");
        }
    }
}
4

1 回答 1

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

希望这可以帮助!

于 2011-03-20T17:30:37.990 回答