Callng a button made from a for loop from another object's event listener
I made 2 arrays one for names insect parts, the other for the parts of the insects to show light up. Rolling over the buttons created by the array and for loop, the parts and buttons light up. But I also want the parts and buttons to light up when rolling over the insect parts. My problem is I don't know how to call for the buttons from loop when rolling over the insect parts. See function rollHandler2 and 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.button开发者_如何学JAVAMode = 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");
}
}
}
I've made some modifications in your code. look at the comment and you should get what i mean. All my comments are marked as this: //---This is an example comment---\\
//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");
}
}
}
Hope this helps!
精彩评论