PDA

View Full Version : allocating xml to string - basic


savager2
02-20-2008, 11:55 AM
I'm working on my file and going off of tutorials and things.. and this lynda tutorial, of which I've posted an image of below, uses the loaders name to hold a string of one of the attributes of my xml file..

so my question is.. since I can't add a new method(? i think that's what it's called) to loader, like i can to a movieclip, how does one suggest I allocate the rest of the attributes in the xml file.. for example.. name is going to hold the source of the large image to load. so what if I have another attribute, called format, that I also want to be able to specify using event.target._____..

this isn't my file, it's just the example.. but if i knew how to do that then i'd be set..

see when they click on one of those buttons I need it to check a separate attribute of the xml, and depending on what it is.. run slightly different.. thanks.

tarafenton
02-20-2008, 04:27 PM
very simply...

myNewAtt = xmlList[i].attribute("format");

what do you really want to do with this? Maybe I could help you with that too.

savager2
02-21-2008, 04:44 AM
hhmm.. so I just create a variable that holds it.. and when i put that in the for loop it'll create a variable for each button that holds the data in my format attribute.. hhmm. so would that be making a different variable for each button. just because it needs to be "attached" to the button.. since I've added a listener to the button.. I need to know which button has been clicked.. so I'm just assuming it's going to be something like

if(event.target. myenwAtt? == "landscape"){
etc...

does it work that way.. can i call it through event.target.. if so then I'm gravy and I thank you very much.. but i would like to know the logic.. mynewAtt would be a variable right.. and there would be one new variable for each button.. and I can always call them with event.target?.. thanks a lot...

ohh and also.. i'm wondering that if i create that var in the for loop.. does that mean that it falls into the scope of that loop.. so that it can't be changed right.. because it doesn't exist elsewhere.. but it can still be read..

savager2
02-21-2008, 11:52 AM
and what datatype would mynewatt be... a string I assume..

tarafenton
02-21-2008, 03:53 PM
i'm a little confused what you are trying to do. Are you trying to make text from the xml buttons? clarify a little more and I could help you out with some code to get you started. also paste your code in (i can't get it to work, but use open and close brackets[] with as /as) so I can copy and adjust some of it to work for you.
//this is just a test block of actionscript code
the datatype is an XMLList, you would have to use myNewAtt.toString(); to make it a string, but that depends on what you want to do with the data.

savager2
02-21-2008, 05:33 PM
sorry.. like i said that wasn't even my code.. it was from a tutorial,.. in a movie window... so i couldn't use [ as] tags..
i just thought i was missing some major idea, like a common way to attach a reference of my xml list to the button that the person clicked.. here's the part of my code I'm referring to.. oh and the xml list looks like this..

<image name="purty" desc="Testing the description" swf="Assets/Blah.swf" thumb="Assets/Thumbs/introT1.jpg" format="horizontal" date="1.1.08" />

var _photoList:XMLList = xmlData.image;//already parsed the xml data and am storing a list of my photos here
for (var i:int = 0; i< _photoList.length(); i++)//for loop to create buttons
{
var _thumbHolder:ThumbHolderBTN = new ThumbHolderBTN();//symbol from the library.. nothing more than a rectangle.
_thumbHolder.addEventListener(MouseEvent.ROLL_OVER , onOver, false, 0, true);//roll over and out listeners
_thumbHolder.addEventListener(MouseEvent.ROLL_OUT, onOut, false, 0, true);//
_thumbHolder.x = _thumbx;//positioning
_thumbx += _thumbHolder.width + _spacing;//positioning
_thumbLoader = new Loader();//new loader for the thumbs
_thumbLoader.load(new URLRequest(_photoList[i].attribute("thumb")));//grabs the url of the thumb from the thumb attribute in the list of images relating to photoList[i] for which image we're on..
_thumbLoader.name = _photoList[i].attribute("swf");//this is what i'm asking for help about.. I was shown in that tutorial that we can store like directions to this buttons swf attribute in the name for each button.
_thumbLoader.alpha = .5;
_thumbLoader.x = -34;
_thumbLoader.y = -22.5;
_thumbLoader.y +=1;
_thumbLoader.addEventListener(MouseEvent.CLICK, loadImage, false, 0, true);
_thumbHolder.addChild(_thumbLoader);
_thumbArray.push(_thumbHolder);//just putting each thumbholdr in an array
_navHolder.addChild(_thumbHolder);//this is just a sprite that i put all the buttons in, since i'm going to need to figure out scrolling as well..

}
}

function loadImage(event:MouseEvent):void{
_thumbLoader = new Loader();
_thumbLoader.load(new URLRequest(event.target.name));//see now this will load the url that is in my "swf" attribute using the event you clicked on's name
_thumbLoader.x = 10;//blah
_thumbLoader.scaleX = .2;//blah
_thumbLoader.scaleY = .2;//blah
addChildAt(_thumbLoader, 0);
}

function onOver(event:MouseEvent):void{

}

function onOut(event:MouseEvent):void{

}

event.target.name is going to be _photoList[i].attribute("swf")... which is.. an url to the main swf i'm loading in..
so what I'm trying to do.. is "assign" _photoList[i].attribute("format") to event.target._____ anything... so that I can do something like

function loadImage(event:MouseEvent):void{
if(event.target.____ =="horizontal"){
then do this
}
}

or presumably I'd be able to do this as well if i stored the "desc" attribute in something as well.

function onOver(event:MouseEvent):void{
textbox.text = event.target.descATTRIBUTE;
}

so what you were saying before..
very simply...

myNewAtt = xmlList[i].attribute("format");

is to create a new variable.. that is each attribute of my xml.. and then I could read them using event.target.mynewAtt...

sorry if this is confusing.. i'm not trying to make it that way..

tarafenton
02-21-2008, 06:38 PM
great i just wanted some of your code to work off of so you can get it working and slowly figure out why...
this is good
//you already have them giving it a name
_thumbLoader.name = _photoList[i].attribute("swf");
//trace out the name, because you are going to have to find where the number(i)
trace(_thumbLoader.name);
//i changed this
_thumbLoader.addEventListener(MouseEvent.CLICK, _click);
}

Put this function IN the for loop


you might not need this right now but it is good to know for the future
anyway this is how I reference them from outside the for loop
function _click(evt:MouseEvent):void {
//depending on the tmpButton pressed, get the slice
var str:String = evt.target.name;
var mySlice = str.substr(9);
outsideVariable = mySlice;
//my example name is tmpButton0 so str.substr(9) is 0
trace(mySlice);
trace(evt.target.name);



what are you trying to do over, out and click?

savager2
02-21-2008, 06:52 PM
if I trace _thumbLoader.name... all I'm going to get is the "swf" attribute.. so.. in this case.. Assets/Intro1.swf Assets/Intro2.swf and so on all the way down the list..

then I see you create a string that holds the event.target.name and then two other variables that are equal to it.. but I'm lost at the string substring(if thats what that means) part...

I put that function inside the for loop???.. see the reason I'm doing this is so that I can have references to particular xml attributes..

so on over.. lets say I want to have a text box have the string inside my attribute "desc".

hmm.. i'm looking over what you put..

tarafenton
02-21-2008, 07:02 PM
give it a more useful name
_thumbLoader.name = "thumbLoader"+i;

tarafenton
02-21-2008, 07:09 PM
this will be overload but take what you need for the text field



////////////////////////////////////////////////////////////////////////////////////

var popTitle = xmlData.attribute("desc");
var str0:String = popTitle[i];
var popTitle_tf:TextField = new TextField();
var popTitle_tfName:String = "popTitle_tf"+i;
popTitle_tf.name = popTitle_tfName;
popTitle_tf.antiAliasType = AntiAliasType.ADVANCED;
popTitle_tf.selectable = false;


popTitle_tf.text = str0.toUpperCase();
popTitle_tf.x =55+(122.5*i);
popTitle_tf.y = 177;
popTitle_tf.width = popTitle_tf.textWidth + 20;

savager2
02-21-2008, 07:12 PM
oohh.. you're saying if i named it that instead.. i think i'm getting you.. i'll be working on it.. thanks a lot..

savager2
02-22-2008, 04:32 AM
alright.. without more code, or an explanation.. this is what i gather.. someone else want to chime in and let me know if i'm on the right track?

I'm pretty sure this is all in a for loop..

var popTitle = xmlData.attribute("desc");
I assume you declared this popTitle var above.. to a string.. and here you assign it to equal the proper attribute in your xml list..

var str0:String = popTitle[i];
then.. you make a string that is popTitle and which time through the loop it is.. right.. so wouldn't this create a string that would be like "Image1" "image2" and so on, if the text in your desc attribute was "image"?

var popTitle_tf:TextField = new TextField();
create new textfield

var popTitle_tfName:String = "popTitle_tf"+i;
then a string that is.. the textfield + i?? so this is a name.. i guess..

the rest is kind of moot at thsi point til i understand the top.. thanks for the code, but i'm more looking for the logic behind how it works..

savager2
02-22-2008, 04:17 PM
so.. i'm still stuck on this, and I hate not being able to work on it.. all I'm trying to do is use a for loop to create buttons that when you roll over/click, it's somehow connected to my xml.. my code is attached below.. and it's working using "name" as a "link" to one of my xml attributes..

I just want to somehow form links to other attributes of mine.. help is greatly appreciated..

and thanks tarafenton but i lost you.. I see what you mean about naming it "thumbloader"+i.. then it traces which button you're over.. but.. then you use substring to shorten that... into just the number..?? and then.. after that.. i'm still not clear what to do with it.. sorry..

tarafenton
02-25-2008, 02:34 PM
i don't see your attached code...
i think you just want to know what going on and I am not the one to explain how things work. i can help you to get it to work and then you can make your own explaination.
maybe this is the piece your missing...the substring your getting is going to be the node in the xml, because when you create the buttons in the loop you give it the name + i (it's not the best explaination i know) like i said i can help get you to make it work, but if your looking for the reason why.... i don't care (more or less) i learn as i go and if it works IT Works and I move on to the next challenge.

if your still stuck i can help you to get it to work

savager2
02-25-2008, 03:33 PM
hey.. thanks.. nah.. I think I figured it out.. and yeah.. as long as it works it works.. later