Skip to main content
July 24, 2007
Question

XML and AS2.0

  • July 24, 2007
  • 21 replies
  • 1270 views
OK I have been going down one road for this and have taken a few wrong turns.

A few things about what we are looking at:
I am using Flash CS3 but I am using Action Script 1.0/2.0 for compatability purposes.

What I have been able to do so far is create random instances of a movie clip to appear over a set amount of time but I need to go a bit further. The web application needs to be updated regularly and the best way I can think of doing that is with keeping all the variables in an XML. The problem is I am not sure how to format the XML properly to get Flash to read it and load the variables.

I am creating an online version of a vision screening tool we use. The test is graphically simple just a collection of a few primitives on a dark background. There are basically two objects a fixation point and a stimulus. What I need to be able to do is this.

Display either movieclip at (x,y) for (z)ms where x,y,& z are all variables loaded from an xml. I have asked questions along this line before but this is a different scope in case anyone wants to claim I double posted, this is quite different than they way I was doing it before.

Any help with the XML or even any actionscript suggestions would be greatly appreciated.

Thanks
This topic has been closed for replies.

21 replies

July 25, 2007
sort of
the variables contain the x,y coordinates and the name of the movieClip instance as well as a value for a timer event. The last one is not the most important so lets just leave that out for the moment.

I was trying to state it with a with statement as in:

with(oValue){
_x=xValue
_y=yValue
}

but that is when I get the error that the object does not exist. Do I need to add some actionscript to my movieClip to make it work? Or is there another way to do it? Do I need to add an event to the clip itself I guess is what I am asking or do I need to attach the code that makes the clip appear the clip itself?

I did already make sure that the clips were exported for Actionscript but that didnt seem to make a difference
Inspiring
July 25, 2007
Hi again,

I *think* what you're asking is, how do you place a variable inside a clip?

It's simply: clipInstanceName.variableName=value;

For example, say your clip instance is named "magicSquare" and the
variable is "myVar":

magicSquare.myVar=123;

-or-

magicSquare['myVar']=123;

Is this what you're asking?

shurleynova wrote:
> One quick question that has me confused however
>
> It is awesome that I now have the variables in Flash from the XML but...
>
> I am familiar with attaching variables to dynamic text by adding the variable
> in the properties box but how do I assign the variable to a movieClip?
>

--
http://www.baynewmedia.com
Faster, easier, better...ActionScript development taken to new heights.
Download the BNMAPI today. You'll wonder how you ever did without it!
Available for ActionScript 2.0/3.0.
July 25, 2007
One quick question that has me confused however

It is awesome that I now have the variables in Flash from the XML but...

I am familiar with attaching variables to dynamic text by adding the variable in the properties box but how do I assign the variable to a movieClip?
Inspiring
July 25, 2007
That's the way we do ;) Good luck!

shurleynova wrote:
> Ok I have it I created an xml using the example from Patrick B and I can see in
> the Output window that the variables are loaded correctly. The XML and AS is
> formatted like the code attached I get an out put that looks like this:
>
> Data loaded: true
> object_type=stimuli
> x_coordinates=25
> y_coordinates=25
> z_display_interval=200
> object_type=stimuli
> x_coordinates=85
> y_coordinates=175
> z_display_interval=200
> object_type=stimuli
> x_coordinates=325
> y_coordinates=55
> z_display_interval=200
> object_type=stimuli
> x_coordinates=115
> y_coordinates=355
> z_display_interval=200
> object_type=fixation
> x_coordinates=289
> y_coordinates=229
> z_display_interval=200
>
> Thanks to all now I have to figure out how I am going to use the variables to
> display my clips :)
>
>
>
>
>
>
> <?xml version="1.0"?>
> <!-- Where omc is the object to display xp is x position, yp is y position,
> zms is display interval -->
> <timeline>
> <point omc="stimuli" xp="25" yp="25" zms="200"/>
> <point omc="stimuli" xp="85" yp="175" zms="200"/>
> <point omc="stimuli" xp="325" yp="55" zms="200"/>
> <point omc="stimuli" xp="115" yp="355" zms="200"/>
> <point omc="fixation" xp="289" yp="229" zms="200"/>
> </timeline>
>
> // Then the actionScript in Flash looks like this
>
> var timelineXML=new XML();
>
> timelineXML._context=this;
> timelineXML.ignoreWhite=true;
>
> timelineXML.onLoad=function(success:Boolean) {
> trace ('Data loaded: '+success);
> this._context.parseXMLData(this.firstChild);
> }//onLoad
>
> function parseXMLData(data:XMLNode) {
> for (var count:Number=0;count<data.childNodes.length;count++) {
> var currentNode:XMLNode=data.childNodes[count];
> var oValue:String=String(currentNode.attributes.omc);
> var xValue:Number=Number(currentNode.attributes.xp);
> var yValue:Number=Number(currentNode.attributes.yp);
> var zValue:Number=Number(currentNode.attributes.zms);
> trace ('object_type='+oValue);
> trace ('x_coordinates='+xValue);
> trace ('y_coordinates='+yValue);
> trace ('z_display_interval='+zValue);
> }//for
> }//parseXMLData
>
> timelineXML.load('input.xml');
>

--
http://www.baynewmedia.com
Faster, easier, better...ActionScript development taken to new heights.
Download the BNMAPI today. You'll wonder how you ever did without it!
Available for ActionScript 2.0/3.0.
July 25, 2007
Ok I have it I created an xml using the example from Patrick B and I can see in the Output window that the variables are loaded correctly. The XML and AS is formatted like the code attached I get an out put that looks like this:

Data loaded: true
object_type=stimuli
x_coordinates=25
y_coordinates=25
z_display_interval=200
object_type=stimuli
x_coordinates=85
y_coordinates=175
z_display_interval=200
object_type=stimuli
x_coordinates=325
y_coordinates=55
z_display_interval=200
object_type=stimuli
x_coordinates=115
y_coordinates=355
z_display_interval=200
object_type=fixation
x_coordinates=289
y_coordinates=229
z_display_interval=200

Thanks to all now I have to figure out how I am going to use the variables to display my clips :)




July 24, 2007
Oh I didnt see that post from Patrick B when I posted that last one I think that migth be what I needed to know, thanks all I'll be back if I still get stuck.
Damon Edwards
Inspiring
July 24, 2007
I bought "Foundation XML for Flash" a long time ago when I was starting out.. It helped a great deal.
Inspiring
July 24, 2007
I wouldn't waste money on an XML book. The format is open by
specification, meaning you can do with it what you like. It's better if
you do some experiments to see how nodes and attributes are accessed in
Flash, then you make up any XML formats you want. That's the beauty of
XML, it's eXtensible.

Patrick

shurleynova wrote:
> Man that was so easy I almost feel retarded. Thank you I was making it way to
> hard I am kinda new at XML and the make your own <tags> kinda throws me
> sometimes. I think I need to go get a good XML book any suggestions?
>

--
http://www.baynewmedia.com
Faster, easier, better...ActionScript development taken to new heights.
Download the BNMAPI today. You'll wonder how you ever did without it!
Available for ActionScript 2.0/3.0.
Inspiring
July 24, 2007
Hi shurleynova,

XML is pretty versatile; you should be able to get it do do whatever
you want. With AS2.0, it's also very loose so you can get away with poor
formatting.

Let me suggest a format like this to begin with:

<timeline>
<point x="20" y="30" z="15"/>
<point x="10" y="30" z="35"/>
<point x="60" y="10" z="2"/>
<point x="80" y="5" z="4"/>
</timeline>

This format is actually proper and easy to work with in Flash. Many
people would parse this data into multi-dimensional arrays but this is
automatically done by Flash. If you save this to a file named
"timeline.xml", your script would look something like this:

var timelineXML=new XML();

timelineXML._context=this;
timelineXML.ignoreWhite=true;

timelineXML.onLoad=function(success:Boolean) {
trace ('Data loaded: '+success);
this._context.parseXMLData(this.firstChild);
}//onLoad

function parseXMLData(data:XMLNode) {
for (var count:Number=0;count<data.childNodes.length;count++) {
var currentNode:XMLNode=data.childNodes[count];
var xValue:Number=Number(currentNode.attributes.x);
var yValue:Number=Number(currentNode.attributes.y);
var zValue:Number=Number(currentNode.attributes.z);
trace ('x='+xValue);
trace ('y='+xValue);
trace ('z='+xValue);
}//for
}//parseXMLData

timelineXML.load('timeline.xml');

If you wanted to access a specific node in the list once the data is
loaded, a simple function like this does the trick:

function getPointNode(index:Number):XMLNode {
return (timelineXML.firstChild.childNodes[index]);
}//getPointNode

As you can see, XML is parsed into an associative array so dealing with
it in memory is very straightforward. If you wanted to get the "x" value
from node 1 (index 0), you would call:

var myXValue=getPointNode(0).attributes.x;

Just keep in mind that all attributes are text so if you want to work
with them as numbers, convert them first.

And there you go!

Patrick

shurleynova wrote:
> OK I have been going down one road for this and have taken a few wrong turns.
>
> A few things about what we are looking at:
> I am using Flash CS3 but I am using Action Script 1.0/2.0 for compatability
> purposes.
>
> What I have been able to do so far is create random instances of a movie clip
> to appear over a set amount of time but I need to go a bit further. The web
> application needs to be updated regularly and the best way I can think of doing
> that is with keeping all the variables in an XML. The problem is I am not sure
> how to format the XML properly to get Flash to read it and load the variables.
>
> I am creating an online version of a vision screening tool we use. The test is
> graphically simple just a collection of a few primitives on a dark background.
> There are basically two objects a fixation point and a stimulus. What I need to
> be able to do is this.
>
> Display either movieclip at (x,y) for (z)ms where x,y,& z are all variables
> loaded from an xml. I have asked questions along this line before but this is a
> different scope in case anyone wants to claim I double posted, this is quite
> different than they way I was doing it before.
>
> Any help with the XML or even any actionscript suggestions would be greatly
> appreciated.
>
> Thanks
>

--
http://www.baynewmedia.com
Faster, easier, better...ActionScript development taken to new heights.
Download the BNMAPI today. You'll wonder how you ever did without it!
Available for ActionScript 2.0/3.0.
July 24, 2007
Man that was so easy I almost feel retarded. Thank you I was making it way to hard I am kinda new at XML and the make your own <tags> kinda throws me sometimes. I think I need to go get a good XML book any suggestions?