Answered
Building a game screen based on XML data
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);
}
}
}
}
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);
}
}
}
}