0

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!

1
  • Just wondering if you have figured out the problem? Because i am having the same issue, i cant access any child added with the for loop from the outside. Something with scope problem? Commented Apr 25, 2014 at 5:19

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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!
0

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

Comments

0

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

2 Comments

the eventListener it's attached to the squares and it works, but all squares have the same url...
Hi, thank you very much for your answer, I tried but still no success
0

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.