Answered
Dynamic buttons
Hi, I'm making an image viewer which loads in the images and
their info from XML. I'm wanting to have a list of the images
titles, and clicking on a title brings up that image. I've parsed
the XML into an array, and I'm using a FOR loop to go through the
array and create a project_btn sprite, which contains a project_txt
textfield within it to display it's name. As part of the FOR loop,
I'm adding an event listener to listen for a ROLL_OVER event,
which, for the moment just traces the project_btn's name. The
problem I'm having is that it seems to only add the eventListener
to the LAST project_btn... so the others are useless.
Code below:
private function drawNavList() {
navList = new Sprite();
navList.x = -(showWidth/2)-195;
navList.y = -50;
addChild(navList);
var projectID:Number;
for (projectID = 0; projectID<numberOfProjects; projectID++) {
var project_btn:Sprite = new Sprite();
var project_txt:TextField = new TextField();
format.size = 14;
format.color = signflairGrey;
format.font = font.fontName;
format.align = 'center';
project_txt.text = sectionData[projectID][0];
project_txt.setTextFormat(format);
project_txt.background = true;
project_txt.backgroundColor = 0xff0000;
project_txt.selectable = false;
project_txt.embedFonts= true;
project_txt.y = 20*projectID;
project_txt.width = 165;
project_txt.height = 14;
project_btn.name = String(projectID);
project_btn.addChild(project_txt);
project_btn.addEventListener(MouseEvent.ROLL_OVER, projectOVER);
navList.addChild(project_btn);
}//for>
}//drawNavList>
private function projectOVER(e:Event) {
trace((e.target).name);
}//projectOVER>
Would appreciate any thoughts or suggestions.
Code below:
private function drawNavList() {
navList = new Sprite();
navList.x = -(showWidth/2)-195;
navList.y = -50;
addChild(navList);
var projectID:Number;
for (projectID = 0; projectID<numberOfProjects; projectID++) {
var project_btn:Sprite = new Sprite();
var project_txt:TextField = new TextField();
format.size = 14;
format.color = signflairGrey;
format.font = font.fontName;
format.align = 'center';
project_txt.text = sectionData[projectID][0];
project_txt.setTextFormat(format);
project_txt.background = true;
project_txt.backgroundColor = 0xff0000;
project_txt.selectable = false;
project_txt.embedFonts= true;
project_txt.y = 20*projectID;
project_txt.width = 165;
project_txt.height = 14;
project_btn.name = String(projectID);
project_btn.addChild(project_txt);
project_btn.addEventListener(MouseEvent.ROLL_OVER, projectOVER);
navList.addChild(project_btn);
}//for>
}//drawNavList>
private function projectOVER(e:Event) {
trace((e.target).name);
}//projectOVER>
Would appreciate any thoughts or suggestions.