Jump to content
Search Community

Using extra vars from XMLLoader

mrEmpty test
Moderator Tag

Recommended Posts

Hello.

 

I'm using LoaderMax to build some dynamic menus from an XML file, everything is working well. However I'm now wondering how to do one thing. I want the XML file to hold a variable for each button, this variable is a number. When I mouse over the button, I want to be able to get at that number.

 

My loader:

 

xmlLoader.append(new XMLLoader('assets/config.xml', {contentLocation:"sectionContentValue" , onComplete:xmlLoaded})); 

 

contentLocation is the number I want to get at, in the XML it's called sectionContentValue.

 

Each button is given an event listener which trigger a function called intelliGo.

 

public function intelliGo(event:MouseEvent):void {
		//this function moves the content based upon where you press the menu
		trace(event.target.contentLocation);
	}

 

Which obviously won't work. I have an XMLList storing all instances on a tag, inside this tag is the tag. I'm now at a loss of what to do, any pointers would be great. I'll keep poking and respond here if I fix it in the mean time.

 

Cheers,

A.

Link to comment
Share on other sites

This probably doesn't help you directly in your scenario, but just so you know: XMLLoader parses all the attributes in LoaderMax-related nodes in your XML and anything it doesn't recognize specifically it still records in the vars object, so if part of your XML looked like this:


 

You could access myCustomVariable like this:

var loader:ImageLoader = LoaderMax.getLoader("1.jpg");
trace(loader.vars.myCustomVariable); //"whatever"

 

But it sounds like you're trying to apply some variable from your ActionScript to a group of loaders. You could use the vars object for that too, I suppose, but you'd need to set the value(s) yourself. Like after the XML loads, you could do something like:

 

var children:Array = LoaderMax.getLoader("assets/config.xml").getChildren(); 
for (var i:int = 0; i     children[i].vars.contentLocation = "sectionContentValue";
}

Link to comment
Share on other sites

Hello.

 

Work's servers are down so I can't get access to try this out until Monday, but I'll try mocking something up to give it a whirl. Thanks for the post.

 

I'm creating a set of buttons from the XML, basically the XML has a series of (for example) tags which contain info for a textfield, colour etc. Each button must send content around, so I'm trying to have a var in the XML which holds a number, for simplicity here lets say 10, 20, 30. I create the buttons in a loop, giving them all a listener. When I trigger the listener I'm trying to get at that 10, 20, 30 etc var, so I can feed that to Tweenmax to move the content about.

 

I've probably gone about it in a strange way, but it seemed the cleanest and so far it's working well. I love LoaderMax so much I've started sticking more stuff into the XML so I can use it more( no joke - must be the geek in me) and it may be clouding alternative methods! :)

Link to comment
Share on other sites

Hello.

 

I still can't get this working. I've tried your suggestions, here is a cut down version of the section of code:

 

private function init(event:Event):void {
  var xmlLoader:LoaderMax = new LoaderMax({maxConnections:1}); //create the loadermax instance
  xmlLoader.append(new XMLLoader('assets/config.xml', {onComplete:xmlLoaded}));  //add an xml loader, point it to the xml file
  xmlLoader.load();  //load the xml
 }

 private function xmlLoaded(event:LoaderEvent):void {  //this method is called when the xml is loaded
  var numItems:XMLList = event.target.content.menuItem;  //take all the menuItems int he XML and store them in a list
  for (var i:int = 0; i < numItems.length(); i++) {  //for each of those menuItems create a sprite and load the text into them

//stuff here to create a sprite for each section of the menu
//give each section an event listener
imSection.addEventListener(MouseEvent.ROLL_OVER, intelliGo);

//add the items
imSection.addChild(imSectionText);
imSection.y = (stage.stageHeight/numItems.length())*i;
imMenu.alpha = imAlpha;
imMenu.addChild(imSection);
  }

  //get the extra vars
  var sectionValue:Array = LoaderMax.getLoader("assets/config.xml").getChildren();
  for (var i:int = 0; i < sectionValue.length; i++) {
sectionValue[i].vars.contentLocation = "sectionContentValue";
  }

 //I then, on a roll-over of the menu section, need to be able to get at the corrent sectionContentValue number for each section of the menu
 public function intelliGo(event:MouseEvent):void {
  //I don't know how to get at that
 }

 

When I trace to te sectionValue I get an empty line break in the console, but no errors.

 

If I trace sectionValue[2] for example, I get 'unspecified'. If I trace sectionValue[2] as Number; I get null. So I have two problems, how to store those extra vars and how to link them with each section. With the ImageLoader you can specify extra vars such as name, and easily get access to that information. Can you do this when using the XMLLoader?

 

Apologies for the odd formatting of this post, I did wrap code in the code tags.

Link to comment
Share on other sites

Trying another route, when looping through the XMLList I'm pushing my 'sectionContentValue' into an array. Can I then grab the position of the current item from it's array, as that number will match the second array with my custom vars.

 

So, each menu section sits in an XMLList, and the custom vars sits in an array. The custom var's will math the buttons XMLList. So can I get to that?

Link to comment
Share on other sites

Just re-read your response. I don't have a group of loaders, I'm using 1 XMLLoader to grab a single XML file. The XML contains a few of these:

 

<menuItem>
<sectionColor>0x8AB0A5</sectionColor>
<sectionHeader>Section 1 with line breaks and word wrap</sectionHeader>
<sectionTextColor>0xffffff</sectionTextColor>
<sectionContentValue>10</sectionContentValue>
</menuItem>

 

I can get at the sectionContentValue data fine, and push that into an array. I need to couple that number to the sprite which triggers an event.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...