PDA

View Full Version : Need help with an xml tutorial


stef
03-29-2004, 11:21 AM
Hey All :)

I kind of guess this isn't the best place to ask for help on a tutorial from a friends of ed book but their forum isn't visited that much anymore to say the least, which makes it really hard to get any help from anyone :(

If I post the code, which looks worse than it is there is only one line I don't understand which will be high-lighted in bold.

tarotdeck = new XML();
tarotdeck.load("tarotdeck.xml");
tarotdeck.onLoad = findTheDeck;

var passItOn = ""
tellMeFirstLevel.textfieldDisplayArea = "I don't know yet.";
tellMeSecondLevel.textfieldDisplayArea = "I don't know yet.";
tellMeThirdLevel.textfieldDisplayArea = "I don't know yet.";
tellMeFourthLevel.textfieldDisplayArea = "I don't know yet.";

function findTheDeck () {
for (var counter01 = 0; counter01 <= tarotdeck.childNodes.length; counter01++) {
// we examine the nodeName property, and compare it to our expectations using ==
if (this.childNodes[counter01].nodeName.toLowerCase() == "deck") {
passItOn = this.childNodes[counter01];
trace ("passItOn.nodeName = " + passItOn.nodeName);
tellMeFirstLevel.textfieldDisplayArea = passItOn.nodeName;
}
trace ("counter01 = "+counter01);
}

findTheArcana(passItOn);

}


function findTheArcana (deckLevel) {

for (var counter02 = 0; counter02 <= deckLevel.childNodes.length; counter02++) {

if (deckLevel.childNodes[counter02].nodeName.toLowerCase() == arcana"){
passItOn = deckLevel.childNodes[counter02];
trace ("passItOn.nodeName = " + passItOn.nodeName);
tellMeSecondLevel.textfieldDisplayArea = passItOn.nodeName;
}
trace ("counter02 = "+counter02);
}
}

The book explains this code as a conduit between two function and how this passes the location of the nodes between functions?
But the thing is it hasn't been defined as a function, please could someone put me out my mistery and example this possible easy concept to my aching brain :)

thanks for any help you guys & galls can offer

thanks

stefan

splict
03-29-2004, 12:00 PM
findTheArcana has been defined as a function - right below your bolded line. So your bolded line takes the node assigned to the variable passItOn (a few lines above) and sends it as a parameter to the function findTheArcana.

-splict

stef
03-29-2004, 12:22 PM
DUh I think I have to get my eyes tested ;)

So what is the result of passing the variable to the function in the form of a parameter?

Do this mean that the variable with then be stored inside of the function much like a letter box holds post?

thanks for the reply :)

stefan

stef
03-30-2004, 09:17 AM
Is it possible to send any parameters to a function in this manor creating a variable inside of the function?

I have been use to creating functions and parameters such as being able to add a parameter that addes the parameter to a trace output.

example

function displayMyName(sMyName:Sting):Void {

trace ("Welcome to the website " +sMyName);

}

working from this model how is it possible to pass the function a parameter? does it create a local variable inside the function just by adding a parameter which contains the variable which is storing data in this case about the name of a node inside of an xml object.

I am kind of getting the concepts down I have been learning actionscript quite intensively recently for the most part it's been fine but there are the few parts that I can't get my head around the example above being one of them.

thanks again for the help anyone can offer if I could offer any virtual beers for the help I would :)

stefan

splict
03-30-2004, 10:20 AM
Is it possible to send any parameters to a function in this manor creating a variable inside of the function?
Pretty much.
working from this model how is it possible to pass the function a parameter?
displayMyName("stef");does it create a local variable inside the function just by adding a parameter which contains the variable which is storing data in this case about the name of a node inside of an xml object.
Not sure exactly what you are saying, but I think you are on the right path. It does create a local variable from the number/string/etc that you passed to the function.


thanks again for the help anyone can offer if I could offer any virtual beers for the help I would :)Cheers :D

-splict

stef
03-30-2004, 10:44 AM
Pretty much.

displayMyName("stef");
Not sure exactly what you are saying, but I think you are on the right path. It does create a local variable from the number/string/etc that you passed to the function.


thank you splict your help is very welcome

Also glad you are enjoying the beers ;)

well onto the passing of variables to a functions parameters I haven't seen this method of passing data before, and have been finding it hard to locate any materials relating to this?

Just to get this right, sorry you will have to bear or beer with me,

What your saying is that variables can be passed from within one function to another by using the parameter of the function expression.
And so defining the function (in the case of the xml below) will pass the node information in the form of a variable to the function for use in the newly create function.

So in effect any variable that has been correctly defined can be passed to a function through the parameter and so be access within the scope of the newly created function. phheeewwwww

so with the code.....

function findTheArcana (deckLevel) {

for (var counter02 = 0; counter02 <= deckLevel.childNodes.length; counter02++) {.........

within the functions parameters is the string variable called decklevel which is this ineffect creating an empty variable to be used inside of the function?

sorry for the convoluted language I am kind of still working on that stuff, if using the word stuff in a sentance isn't bad enough :)

thanks again splict

you rock :)

splict
03-30-2004, 11:30 AM
I know you said you understood most of it but I will go through it all at once. As far as examples of functions with parameters passed to it - that is how a good majority of functions are created. reading functions in the forums or in the manual will offer many examples. I didn't find a specific resource but I'm sure with a search you could come up with something. functions in Actionscript use a similar syntax to most other oop programing languages as well - especially ones based on the same standard, such as javascript. Hope this helps some, though.

Here are clickable links to the references in the comments:
http://www.kirupa.com/developer/actionscript/array.htm #1
http://www.kirupa.com/developer/actionscript/tricks/arrayduplication.htm #2

-splicttarotdeck = new XML(); // create our XML object
tarotdeck.load("tarotdeck.xml"); // load xml file
tarotdeck.onLoad = findTheDeck; // when the file has loaded (or returned an error) do this (findTheDeck)
// this is what actually calls the function that is defined below
// notice this makes the onLoad function a reference to the findTheDeck function. There is a better way to do this, though.

var passItOn = ""; // initialize our empty variable for use later.
// this would probably be more correct if it was initialized inside of the findTheDeck function, since thats where it is used.
// if done like this, then the variable exists outside of the function which is not necessary since we are passing it through as a parameter.

tellMeFirstLevel.textfieldDisplayArea = "I don't know yet.";
tellMeSecondLevel.textfieldDisplayArea = "I don't know yet.";
tellMeThirdLevel.textfieldDisplayArea = "I don't know yet.";
tellMeFourthLevel.textfieldDisplayArea = "I don't know yet.";

function findTheDeck () { // defines what our function is/does

for (var counter01 = 0; counter01 <= tarotdeck.childNodes.length; counter01++) { // loops through our child nodes.
// since array indexes go from 0 to length - 1, it should probably be:
// for (var counter01 = 0; counter01 < tarotdeck.childNodes.length; counter01++) {
// see: http://www.kirupa.com/developer/actionscript/array.htm #1
// we examine the nodeName property, and compare it to our expectations using ==

// during our loop, when we get to the "deck" node
if (this.childNodes[counter01].nodeName.toLowerCase() == "deck") {
passItOn = this.childNodes[counter01]; // we make our previously created variable a _reference_ to that object
// see http://www.kirupa.com/developer/actionscript/tricks/arrayduplication.htm #2
// for an explanation about the difference between copying an array and creating a reference to it
trace ("passItOn.nodeName = " + passItOn.nodeName); // display our variable's (which is actually a reference to a node) name
// this would be the same as tracing the original nodes name using its absolute path
tellMeFirstLevel.textfieldDisplayArea = passItOn.nodeName; // assign the name to this variable/object
}
trace ("counter01 = "+counter01);
}

findTheArcana(passItOn); // call are function and send to it the reference to the node

}


function findTheArcana (deckLevel) { // definition of this function that is called in previous function

for (var counter02 = 0; counter02 <= deckLevel.childNodes.length; counter02++) { // loop through deck level
// this could have been acheived by looping through the original node

// the rest is similar to the previous function
if (deckLevel.childNodes[counter02].nodeName.toLowerCase() == arcana"){
passItOn = deckLevel.childNodes[counter02];
trace ("passItOn.nodeName = " + passItOn.nodeName);
tellMeSecondLevel.textfieldDisplayArea = passItOn.nodeName;
}
trace ("counter02 = "+counter02);
}
}