Adrian
Adrian

Reputation: 1017

AS3 URLRequest in for Loop problem

I read some data from a xml file, everything works great besides urls. I can't figure what's the problem with the "navigateURL" function or with the eventListener... on which square I click it opens the last url from the xml file

for(var i:Number = 0; i <= gamesInput.game.length() -1; i++)
 {
  var square:square_mc = new square_mc();

  //xml values
  var tGame_name:String   = gamesInput.game.name.text()[i];//game name
  var tGame_id:Number     = gamesInput.children()[i].attributes()[2].toXMLString();//game id
  var tGame_thumbnail:String  = thumbPath + gamesInput.game.thumbnail.text()[i];//thumb path
  var tGame_url:String     = gamesInput.game.url.text()[i];//game url

  addChild(square);
  square.tgname_txt.text = tGame_name;
  square.tgurl_txt.text = tGame_url;

  //load & attach game thumb
  var getThumb:URLRequest = new URLRequest(tGame_thumbnail);
  var loadThumb:Loader = new Loader();
   loadThumb.load(getThumb);
   square.addChild(loadThumb);
  //
  square.y = squareY;
  square.x = squareX;
  squareX += square.width + 10;

  square.buttonMode = true;
  square.addEventListener(MouseEvent.CLICK, navigateURL);

 }

 function navigateURL(event:MouseEvent):void
 { 
  var url:URLRequest = new URLRequest(tGame_url);
  navigateToURL(url, "_blank");
  trace(tGame_url);
 }

Many thanks!

Upvotes: 0

Views: 1146

Answers (4)

Alex Milde
Alex Milde

Reputation: 511

try tracing this:

function navigateURL(event:MouseEvent):void
 { 
  var url:URLRequest = new URLRequest(tGame_url);
  navigateToURL(url, "_blank");
  //trace(tGame_url);
  trace(event.currentTarget.tgurl_txt.text);
 }

you should add the url to your square in the loop

square.theUrl = tGame_url;

in the event listener function you should be able to access it with

event.currentTarget.theUrl;

Upvotes: 0

S P
S P

Reputation: 4643

you should add the addEventListener on the Squares

mmm..still figuring how eventhandler function will ever get the correct tgame_url var.

What if you try this:

 square.addEventListener(MouseEvent.CLICK, function navigateURL(event:MouseEvent):void 
 {  
    var url:URLRequest = new URLRequest(tGame_url); 
     navigateToURL(url, "_blank"); 
    trace(tGame_url); 
  });

Upvotes: 0

George Phillips
George Phillips

Reputation: 4654

In navigateURL() you use tGame_url, but I think you'd rather use something like tgurl_txt.text which will be different for each square.

Upvotes: 1

Michael
Michael

Reputation: 53

Looks like you're not attaching the event listener properly. Instead of this.addEventListener, attach it to the variable you created when creating new square_mc..... so:

square.addEventListener(MouseEvent.CLICK, navigateURL);

Upvotes: 0

Related Questions