Skip to main content
Inspiring
November 28, 2008
Answered

Dynamic buttons

  • November 28, 2008
  • 2 replies
  • 316 views
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.
This topic has been closed for replies.
Correct answer ArmstrongCo
Ah.. nevermind, turns out the code above does what it should, just the other titles happened to be sitting under another button's hit area, so the eventListener wasnt getting through.

2 replies

ArmstrongCoAuthorCorrect answer
Inspiring
November 28, 2008
Ah.. nevermind, turns out the code above does what it should, just the other titles happened to be sitting under another button's hit area, so the eventListener wasnt getting through.
Inspiring
November 28, 2008
Hmm... I found a post that suggested adding the event listener to the container sprite, and then checking to see which object within it dispatched the event... any ideas how I would go about doing that?