1

I'm using XML to store certain default variables in a Flash game. It's easy enough to access those variables in my main class, since I can just instantiate and load an XML variable (in this case, defaultXML) and then do defaultScore = defaultXML.defaults.score.@value; or something similar. But then if I want to retrieve that data from within a child class, I have to use defaultScore = MovieClip( this.parent ).defaultXML.defaults.score.@value;. It gets even more confusing when I'm in a grandchild class, since I'm not even aware of a way to retrieve values from a grandparent.

What's a good way to retrieve values from an XML file in Actionscript? Should I keep doing what I'm doing, or is there an easier way?

1 Answer 1

2

You can access grandparent objects simply by chaining the calls to parent to reach the level you require:

MovieClip( this.parent.parent.parent ).defaultXML.defaults.score.@value;

When you're adding code to existing movieclips made manually on the timeline, this is an easy way to get things working but it is prone to breaking if, for example, the structure of nested movieclips changes.

Another way would be to store the xml as a global object so it could be accessed from anywhere on the timeline hierarchy.

global.defaultScore = defaultXML

These are quick-and-easy techniques good for when you need to add code to the timeline in simple apps. In more advanced apps, all the code will exist in classes only, and then object-oriented techniques are the way to pass your xml data where it's needed.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I didn't know you could chain parent, and I've never worked with global variables (I'm just starting with Actionscript) so I'll have to check that out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.