views:

56

answers:

4

Hi guys,

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!

A: 

Hi there,

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);

Michael
I tried like that too...I get the same result :(
Adrian
A: 

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); 
  });
Sander Pham
the eventListener it's attached to the squares and it works, but all squares have the same url...
Adrian
Hi, thank you very much for your answer, I tried but still no success
Adrian
+1  A: 

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.

George Phillips
Hi, I tried like you said, but I get the same thing...It always get the last url from the xml :(. thanks for your support!
Adrian
A: 

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;
Alex Milde