335 posts
  • Sold between 10 000 and 50 000 dollars
  • Author had a Free File of the Month
  • Has been a member for 5-6 years
  • Interviewed on the Envato Notes blog
  • Bought between 100 and 499 items
  • Exclusive Author
  • Microlancer Beta Tester
+3 more
tiam says

Hello! Im rubbing my head with a little problem and couldnt think of a better place to ask for help :)

Im making a file where I want to import some settings into actionscript from a xml fil but I cant find out how to get the xml content out of the xml declaration…

here is the code

settingXML = new XML ; settingXML.ignoreWhite = true; settingXML.onLoad = function(loaded) { if (loaded) { }

var dub = this.firstChild;
var setting = dub.attributes;
var start_var = setting.start;
var about_var = setting.about;
var portfolio_var = setting.portfolio;        
var contact_var = setting.contact;

trace(start_var) settingXML.load(“settings.xml”);

}

I want to be able to get the variables imported with xml to work outside of the xml declaration so that I could use these variables elswhere maybe in another mc.

How do I do this? Thanks for any help :)

4335 posts
  • Beta Tester
  • Bought between 10 and 49 items
  • Community Moderator
  • Contributed a Blog Post
  • Contributed a Tutorial to a Tuts+ Site
  • Exclusive Author
  • Grew a moustache for the Envato Movember competition
+6 more
Reaper-Media Volunteer moderator says

You should already be able to refer to them / use them from outside that decleration.

ie:
my_mc.variable
where my_mc is the movieclip which loads the XML (can be _root or _parent)
and variable is the variable which you want to use (e.g. contact_var)
335 posts
  • Sold between 10 000 and 50 000 dollars
  • Author had a Free File of the Month
  • Has been a member for 5-6 years
  • Interviewed on the Envato Notes blog
  • Bought between 100 and 499 items
  • Exclusive Author
  • Microlancer Beta Tester
+3 more
tiam says

Thanks a lot for the answer Reaper-Media!!

However it seems like when trying to access the var outside the declaration the xml is not loaded… I tried to trace outside but just gets undefined. and when tracing the xml loaded outside i get that it is not loaded. is there any way to make the variable only load if the xml is loaded ? :)

675 posts
  • Exclusive Author
  • Won a Competition
  • Most Wanted Bounty Winner
  • Sold between 10 000 and 50 000 dollars
  • Philippines
  • Bought between 10 and 49 items
  • Has been a member for 4-5 years
+1 more
dexterous says

Are you declaring your variables inside the onLoad function using the ‘var’ key word. This makes the variables as ‘local’ for the function and they will be removed after the function block.

To use the variable outside the function block, declare them outside. And then you can use them anywhere as Reaper has said.

var setting:String; var start_var:String; var about_var:String;

settingXML = new XML ; settingXML.ignoreWhite = true; settingXML.onLoad = function(loaded) { if (loaded) { var dub = this.firstChild; start_var = dub.attributes; about_var = setting.start; } trace(start_var);

}

settingXML.load(“settings.xml”);

HTH

335 posts
  • Sold between 10 000 and 50 000 dollars
  • Author had a Free File of the Month
  • Has been a member for 5-6 years
  • Interviewed on the Envato Notes blog
  • Bought between 100 and 499 items
  • Exclusive Author
  • Microlancer Beta Tester
+3 more
tiam says

Thanks a lot for the help I really appreciate it!

I must be stupid… I still get undefined when tracing the variables

outside the declaration I did trace (start_var)

this is my code now

var start_var:String; var about_var:String;

trace (about_var)

settingXML = new XML ; settingXML.ignoreWhite = true; settingXML.onLoad = function(loaded:Boolean) { if (loaded) { }

var dub = this.firstChild;
var setting = dub.attributes;
var start_var = setting.start;
var about_var = setting.about;
var portfolio_var = setting.portfolio;        
var contact_var = setting.contact;

trace(settingXML.loaded) settingXML.load(“settings.xml”);

}
675 posts
  • Exclusive Author
  • Won a Competition
  • Most Wanted Bounty Winner
  • Sold between 10 000 and 50 000 dollars
  • Philippines
  • Bought between 10 and 49 items
  • Has been a member for 4-5 years
+1 more
dexterous says
I still get undefined when tracing the variables

Tiam,

You must try to access or trace it AFTER your onload function is executed.

You can do one of the following:

In the last line inside the onLoad function, call another function and try to trace it there.

OR simply. Put these actions on frame 1 – add a stop() action.

In the onLoad function last line – play(), in the second frame try to trace.

HTH .

6770 posts
  • Italy
  • Sold between 10 000 and 50 000 dollars
  • Has been a member for 4-5 years
  • Microlancer Beta Tester
  • Beta Tester
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Exclusive Author
  • Author had a Free File of the Month
  • Bought between 10 and 49 items
+1 more
doru says
I must be stupid… I still get undefined when tracing the variables

try give the variables some default value.

instead

var start_var:String;
var about_var:String;
trace(about_var); //nothing there :) yeap you get undefined

try

var start_var:String = "some text";
var about_var:String = "some text";
trace(about_var); //you get "some text"

and inside the function use only

start_var = "some other text";
about_var = "some other text";
trace(about_var); //inside the function or outside you get - "some other text"

edit: use ” < code … bla bla in the post editor,” is not working :(

335 posts
  • Sold between 10 000 and 50 000 dollars
  • Author had a Free File of the Month
  • Has been a member for 5-6 years
  • Interviewed on the Envato Notes blog
  • Bought between 100 and 499 items
  • Exclusive Author
  • Microlancer Beta Tester
+3 more
tiam says

Thanks doro! But by doing that I dont get the information from the xml, or do I? :)

1312 posts
  • Elite Author
  • Sold between 50 000 and 100 000 dollars
  • Has been a member for 5-6 years
  • Microlancer Beta Tester
  • United Kingdom
  • Attended a Community Meetup
  • Referred between 200 and 499 users
+3 more
LGLab says

The problem is you declare your variables again inside the onLoad function by using the term “var” inside the function, which makes that var “local” or “relative”, hence you won’t be able to access it outside the onLoad function, try this:

http://pastie.org/767362

Edit: Also your xml object is not datatyped, it should be:

var settingXML:XML = new XML();

Not sure if that makes a difference as AS2 is quite tolerant :-)

335 posts
  • Sold between 10 000 and 50 000 dollars
  • Author had a Free File of the Month
  • Has been a member for 5-6 years
  • Interviewed on the Envato Notes blog
  • Bought between 100 and 499 items
  • Exclusive Author
  • Microlancer Beta Tester
+3 more
tiam says

Amazing! Its working now :D

Thanks so very much for the help!!!! Really appreciated :)

by
by
by
by
by
by