PDA

View Full Version : Resorting to the forum - dynamic buttons linkage



nortago
March 23rd, 2008, 10:31 AM
I'm just getting into the as3 stuff at the mo, and I've decided to try and code a rss feed as a way of getting into it, anyway I've got to a certain point and I can't figure out how to get past it! - Any ideas oh gurus of kirupa?

Thanks!

Here's what I've got... the problem is I can't get the doSomething function to find the right id? Any ideas, solutions

Liam


var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, onLoaded);





var xml:XML;

function onLoaded(e:Event):void{
xml = new XML(e.target.data);
//trace(xml.channel.item);
//Get to specific xml contents
var list_array:XMLList = xml.channel.item;
trace(list_array.length());

for(var i:uint=0; i<list_array.length(); i++){
trace("hello");
var menu_item:item=new item();
menu.holder.addChild(menu_item);
menu_item.y = 40*i;
menu_item.id = i;
menu_item.name_txt.text = list_array.title.text()[i];
menu_item.author_txt.text = list_array.author.text()[i];
menu_item.addEventListener(MouseEvent.MOUSE_DOWN,d oSomething);

}
}

function doSomething(evt:MouseEvent):void
{
trace(id);
}

jwopitz
March 23rd, 2008, 11:09 AM
try this:



function doSomething(evt:MouseEvent):void
{
trace(item(evt.target).id);
}

nortago
March 24th, 2008, 08:41 PM
Hey man - thanks for responding!

I've added the code you suggested and it seems to work ok, but only sometimes?

I'm using this...


var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, onLoaded);


/*function openLink(link:String):void{

//navigateToURL(new URLRequest(link),"_blank");

trace(link);

}*/



var xml:XML;

function onLoaded(e:Event):void{
xml = new XML(e.target.data);
trace(xml.channel.item);
//Get to specific xml contents
var il:XMLList = xml.channel.item;
//trace(il[3]);
for(var i:uint=0; i<il.length(); i++){
var menu_item:item=new item();
menu.holder.addChild(menu_item);
menu_item.id = i;
trace(menu_item.id);
menu_item.y = 40*i;
menu_item.name_txt.text = il.title.text()[i];
menu_item.author_txt.text = il.author.text()[i];
menu_item.addEventListener(MouseEvent.MOUSE_DOWN, loadLink);
function loadLink(e:MouseEvent):void{
trace(item(e.target).id);
}
}

}

loader.load(new URLRequest("http://hq.papertank.net/index.php?c=feed&a=recent_activities&id=1&token=6ea48fe7816ed986fae6d41d76a1afc32779ad6a"));

But It returns this output...


TypeError: Error #1034: Type Coercion failed: cannot convert flash.text::TextField@240d3941 to item.
at MethodInfo-351()
TypeError: Error #1034: Type Coercion failed: cannot convert flash.text::TextField@240d3821 to item.
at MethodInfo-351()
TypeError: Error #1034: Type Coercion failed: cannot convert flash.text::TextField@240d3461 to item.
at MethodInfo-351()
TypeError: Error #1034: Type Coercion failed: cannot convert flash.text::TextField@240d36a1 to item.
at MethodInfo-351()
2
TypeError: Error #1034: Type Coercion failed: cannot convert flash.text::TextField@240d39a1 to item.
at MethodInfo-351()
4
TypeError: Error #1034: Type Coercion failed: cannot convert flash.text::TextField@240d3b21 to item.
at MethodInfo-351()

the numbers 2 and 4 are return correctly, but this only happens every few clicks ? ? I have no idea why this is happening? any suggestions?

Thanks again :D

Liam

jwopitz
March 24th, 2008, 09:41 PM
well it sounds like maybe you are not using an item instance each time? Oh you know what? It could be that the evt.target gets changed depending on where in the item instance you are clicking. I bet that is what it is. On the click handler, try this:



trace(evt.target);


If that is indeed the case, then you might want to say mouseChildren = false on your item instances.

nortago
March 24th, 2008, 09:56 PM
Dude... Actually I just noticed, hat it actually works fine - but only if I click on my imported movieclip bg - if I click on the textfield within it (ie, the tf on which I load in menu_item.author_txt.text = il.author.text()[i]; I ger the text error... So your code worked originally! So mega thanks dude! I just need to figure out how to stop this? What do you think? would I need to create a blank mc to sit on top ?

mega thanks

Liam