PDA

View Full Version : Help with URLRequest within a loop


PibbTibbs
04-10-2008, 07:00 PM
Hi All

I have built a little news reader in Actionscript 3. I am having a problem with assigning the url property for each news item. Being completely new to as3 i was thinking that i may be doing this all wrong so i hope somebody can help.

here is the script in quetion:

import fl.motion.easing.*;
import flash.net.navigateToURL;
import gs.TweenLite;
var ySpacing:Number = 55;
var itemCount:Number = 0;
var pageNum:Number = 1;

var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, loadXML);
loader.load(new URLRequest("newsReader.php"));
function loadXML(e:Event):void
{

var xml = new XML(e.target.data);

for (var i=0;i<xml.item.length();i++) {
var newsBit:newsClip = new newsClip();
newsBit.newsHeader.htmlText = "<b>" + xml.item.newsHeader[i] + "</b>";
newsBit.newsDate.htmlText = "<b>" + xml.item.newsDate[i] + "</b>";
newsBit.newsText.htmlText = xml.item.newsText[i];

if (xml.item.newsLink[i] != "") {
var clickObj:moreInfo = new moreInfo();
clickObj.y = 43;
clickObj.x = 175;
clickObj.goToLink = new URLRequest();
clickObj.goToLink.url = xml.item.newsLink[i];
clickObj.addEventListener(MouseEvent.CLICK, loadLink);
function loadLink() {
navigateToURL(clickObj.goToLink, "_parent");
}
newsBit.addChild(clickObj);

}
newsBit.y = itemCount * ySpacing;
newsHolder.addChild(newsBit);
itemCount++
}

}




Everything works great except that no matter which clickObj you click, the URLRequest always loads the last item from the xml. I am thinking that a unique instance of the URLRequest object is not being created on each loop pass so the url parameter of the URLRequest object is being overwritten with the last value in the xml ech time around.

Can anyone shed some light on what i can do?

Thanks
pT

TomMalufe
04-10-2008, 07:36 PM
make clickObj:moreInfo clickObjs:Array instead.

Arrays can be your friend... sometimes you grow to hate them... but not often.

You will have to change your code only a little to access clickObjs[i].y and the like

TomMalufe
04-10-2008, 07:41 PM
Actually I was reading your code a little more closely and decided that there are a few changes you really should make.
Not a lot of changes, just some rearranging and changing of var names:


import fl.motion.easing.*;
import flash.net.navigateToURL;
import gs.TweenLite;
var ySpacing:Number = 55;
var itemCount:Number = 0;
var pageNum:Number = 1;

var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, loadXML);
loader.load(new URLRequest("newsReader.php"));
function loadXML(e:Event):void
{

var xml = new XML(e.target.data);
var clickObjs:Array = new Array();

for (var i=0;i<xml.item.length();i++) {
var newsBit:newsClip = new newsClip();
newsBit.newsHeader.htmlText = "<b>" + xml.item.newsHeader[i] + "</b>";
newsBit.newsDate.htmlText = "<b>" + xml.item.newsDate[i] + "</b>";
newsBit.newsText.htmlText = xml.item.newsText[i];

if (xml.item.newsLink[i] != "") {
clickObjs[i] = new moreInfo();
clickObjs[i].y = 43; //aren't all of these clickObjs at the same place like this?
clickObjs[i].x = 175;
clickObjs[i].goToLink = new URLRequest();
clickObjs[i].goToLink.url = xml.item.newsLink[i];
clickObjs[i].addEventListener(MouseEvent.CLICK, loadLink);

newsBit.addChild(clickObjs[i]);

}
newsBit.y = itemCount * ySpacing;
newsHolder.addChild(newsBit);
itemCount++
}

}
function loadLink(e:MouseEvent):void
{
navigateToURL(e.currentTarget.goToLink, "_parent");
}

TomMalufe
04-10-2008, 07:44 PM
looking at things... maybe you should do that same to "newsBit" as I did to "clickObj"


Dang! Now that I look at it, you may only have to do it to newsBit and not at all to clickObj... but I'm not going to try and test that theory. I leave that to you.