Skip to main content
Inspiring
May 4, 2008
Answered

Building a game screen based on XML data

  • May 4, 2008
  • 3 replies
  • 555 views
I am trying to position a list of movieClips on the screen using positioning data from an xml document.

I've created 2 movieClips in my library with the Class linkage of box01 and box02.

I have an xml document that describes the x and y coordinates I want to use:


<?xml version="1.0" encoding="iso-8859-1"?>
<data>
<art name="box01" x="50" y="50" />
<art name="box02" x="100" y="100" />
</data>

I am able to parse that xml document in flash and see the data:
trace(xmldata.art[0].@name) displays "box01"

Now I want to loop through the length of my xml "art" elements and build my screen (by calling the movieClips box01 and box02 that are in the library).

But I don't know how to use the name box01, box02 etc from the xml doc to create my instances.

This doesn't work, but more or less like this:

var i:uint;
for (i=0; i < xmldata.art.length(); i++) {
//trace(xmldata.art .@name);
var myMovieClip:[xmldata.art
.@name] = new [xmldata.art .@name]();
myMovieClip.x = xmldata.art
.@x;
myMovieClip.y = xmldata.art .@y;
addChild(myMovieClip);
}


Its this line that I need the syntax help on:

var myMovieClip:[xmldata.art
.@name] = new [xmldata.art .@name]();


I want the name of my instances to be the "name" attribute from my xml doc.

I'm close - could someone please help me figure this out?

Thanks - I'll put my full code below.








----------------------------------------COMPLETE ACTION SCRIPT-----------------------

package {
import flash.display.*;
import flash.events.*;
import flash.net.URLLoader;
import flash.net.URLRequest;

public class myXMLDoc extends MovieClip {
private var xmldata:XML;

public function myXMLDoc() {
xmldata = new XML();
var xmlURL:URLRequest = new URLRequest("myXMLDoc.xml");
var xmlLoader:URLLoader = new URLLoader(xmlURL);
xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);
xmlLoader.addEventListener(IOErrorEvent.IO_ERROR,xmlLoadError);
}

function xmlLoaded(event:Event) {
xmldata = XML(event.target.data);
trace(xmldata.art[0].@name);
trace("Data loaded successfully.");
buildScreen();
}

function xmlLoadError(event:IOErrorEvent) {
trace(event.text);
}

function buildScreen() {
trace("building screen");
var i:uint;
for (i=0; i < xmldata.art.length(); i++) {
//trace(xmldata.art
.@name);

//******This next line is where I want to add my movieClips box01 and box02 to the screen******
//******and use the x and y positioning data from the xml****

var myMovieClip:[xmldata.art .@name] = new [xmldata.art.@name]();
myMovieClip.x = xmldata.art .@x;
myMovieClip.y = xmldata.art
.@y;
addChild(myMovieClip);
}
}
}
}
This topic has been closed for replies.
Correct answer gigasaurus
here's your corrected class file:


That is beautiful. Thankyou!

3 replies

kglad
Community Expert
Community Expert
May 5, 2008
you're welcome.
kglad
Community Expert
Community Expert
May 5, 2008
are you sure those objects in your library are movieclips? do they extend the sprite class? anyway, use DisplayObject() to cast box01 etc instead of MovieClip().
Inspiring
May 5, 2008
The movieclips are (I believe) setup properly in the FLA

I've placed a zipfile with my FLA, .as and .xml here so you can see the error occuring: http://bigfins.com/temp/test5xml.zip

If I use this (below) I get the same error.

var myMovieClip:DisplayObject = getChildByName(xmldata.art .@name);
kglad
Community Expert
Community Expert
May 5, 2008
here's your corrected class file:

kglad
Community Expert
Community Expert
May 4, 2008
1. use the attach code option to display code in this forum.

2. use getChildByName() to retrieve object references from instance strings:

Inspiring
May 4, 2008
Thankyou so much.

now when I try that here is the error that I get for that line:

1118: Implicit coercion of a value with static type flash.display:DisplayObject to a possibly unrelated type flash.display:MovieClip.

I've placed a zipfile with my FLA, .as and .xml here: http://bigfins.com/temp/test5xml.zip

The actionscript alone is below.

Thankyou again for any assistance.