开发者

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!


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.


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


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜