Run code at end of loop ?
Hi all
This is another problem with a problem I just fixed.
I'm creating a simple navigation from an MC (navBtn) in the library.
The MC's are added using a loop with the text on the MC's coming from an array,
(in the actual code it's from XML).
When the loop has finished I wanted to create a final contact button using the same
MC in the library.
I'm using an if statement to determine the end of the loop
Code:
if (i>=nameArr.length) {
navCon = new Btn();
navCon.name='contact';
txtTF.text = 'Contact';
txtTF.setTextFormat(format);
navCon.width=txtTF.width+5;
navContainer.addChild(navCon);
navCon.x=500;
}
This if statement doesn't work and it either doesn't put the contact MC in or puts
it in the place of the final btn MC.
How can I make sure this final MC is added when the loop HAS finished.
Code:
package {
import flash.display.*;
import flash.text.*;
import flash.events.*;
public class Doc extends MovieClip {
private var navBtn:MovieClip;
private var navCon:MovieClip;
private var navBtnTwo:MovieClip;
private var nameArr:Array;
private var format:TextFormat;
private var txtTF:TextField;
private var prevBtn:Object;
private var navContainer:Sprite;
public function Doc() {
navBtn = new Btn();
navCon = new Btn();
nameArr = new Array();
nameArr=['Link 1','Link 2','Link 3'];
navContainer = new Sprite();
addChild(navContainer);
navContainer.x=20;
navContainer.y=50
;
for (var i:Number=0; i<=nameArr.length-1; i++) {
format = new TextFormat();
format.font='Verdana';
format.color=0x666666;
format.size=20;
txtTF = new TextField();
txtTF.name='myTxt';
txtTF.text=nameArr[i];
txtTF.selectable=false;
txtTF.autoSize=TextFieldAutoSize.LEFT;
txtTF.mouseEnabled=false;
txtTF.x=5;
txtTF.setTextFormat(format);
navBtn = new Btn();
navBtn.addChild(txtTF);
navContainer.addChild(navBtn);
navBtn.buttonMode=true;
navBtn.width=txtTF.width+5;
if (i!=0) {
var prevBtn:Object=navContainer.getChildAt(i-1);
navBtn.x=prevBtn.x+prevBtn.width+15;
}
if (i>=nameArr.length) {
navCon = new Btn();
navCon.name='contact';
txtTF.text = 'Contact';
txtTF.setTextFormat(format);
navCon.width=txtTF.width+5;
navContainer.addChild(navCon);
navCon.x=500;
}
navBtn.addEventListener(MouseEvent.MOUSE_OVER, navBtnOVER);
navBtn.addEventListener(MouseEvent.MOUSE_OUT, navBtnOUT);
navBtn.addEventListener(MouseEvent.CLICK, navBtnCLICK);
}
}
private function navBtnOVER(evt:MouseEvent):void {
format.color=0xff0000;
evt.currentTarget.getChildByName('myTxt').setTextFormat(format);
}
private function navBtnOUT(evt:MouseEvent):void {
format.color=0x666666;
evt.currentTarget.getChildByName('myTxt').setTextFormat(format);
}
private function navBtnCLICK(evt:MouseEvent):void {
trace(evt.target);
}
}
}