PDA

View Full Version : trying to attach functions to as created buttons



cF516
February 15th, 2009, 01:22 AM
hey everyone
im trying to create a series of buttons that will take the user to a link, each link different (obv.). i dont know how many buttons im going to have because im taking in an xml file and from there determining the number of buttons ill need but i need the buttons to go to their own sites. i cant get the buttons to remember where they have to go, they just get stuck on the last 1 through the loop.

for (var i=0; i<linkCounter; i++) {
var newPictureButton:pictureButton = new pictureButton();
this.addChild(newPictureButton);

newPictureButton.x=Math.random()*100;
newPictureButton.y=Math.random()*50;
newPictureButton.mcText.text=i+1;
newPictureButton.name="newPictureButton"+i;
newPictureButton.value = i;

trace(newPictureButton.value = i);

buttons[i]=newPictureButton;

this.buttons[i].link.addEventListener(MouseEvent.CLICK, function(e:Event) {clickedButton(buttons[i]);});
}


function clickedButton(number) {
trace(linkArray[number]);
var request:URLRequest=new URLRequest('http://'+linkArray[number]);
navigateToURL(request);
}


any ideas?
thanks

ayumilove
February 15th, 2009, 01:49 AM
//Ayumilove Sample XML
var ayuXML:XML =
<ayumilove>
<buttonURL name= "http://google.com"></buttonURL>
<buttonURL name= "http://kirupa.com"></buttonURL>
<buttonURL name= "http://ayumilove.wordpress.com"></buttonURL>
</ayumilove>

////////////////////////////////////////////////////////////////////////////////////////////

var totalButtons:int = ayuXML.buttonURL.length();
var buttonNum:Array = [0,1,2];
var buttonArray:Array = new Array; //I assume var 'buttons' is an array
var buttonURL:Array = new Array;
var i:int = 0;

//Store all the URL into my buttonURL array.
for(i=0;i<totalButtons; i++)
{
var tempURL:String = ayuXML.buttonURL[i].@name;
trace(tempURL);
buttonURL.push(tempURL);
}

////////////////////////////////////////////////////////////////////////////////////////////


for( i = 0; i<totalButtons; i++)
{
//btnPic = button picture
var btnPic:AyuButton = new AyuButton();
btnPic.x = Math.random() * 300;
btnPic.y = Math.random() * 100;
btnPic.mcText.text = String(i + 1);
btnPic.name = "btnPic" + i;
btnPic.value = i;
btnPic.mouseChildren=false;
trace(btnPic.value);
buttonArray.push(btnPic);
addChild(btnPic);
trace("haha"+i);
btnPic.addEventListener(MouseEvent.CLICK, clickButton);



}

////////////////////////////////////////////////////////////////////////////////////////////

function clickButton(e:MouseEvent) :void
{
for(var i:int=0;i<buttonArray.length;i++)
{
if(e.currentTarget ==buttonArray[i])
{
var tempURL:URLRequest = new URLRequest(buttonURL[i]);
navigateToURL(tempURL);
}
}
}

cF516
February 15th, 2009, 01:38 PM
wow thanks haha. very much better than mine