Trying to get away from _root!

Hello, how do I adjust this AS2 code to eliminate the _root so this MC can be used anywhere in a .swf?

var dados:XML = new XML();
dados.ignoreWhite = true;
dados.load(‘children.xml’);
dados.onLoad = function():Void {
qtd = this.childNodes[0].childNodes.length;

for (i=0; i<qtd; i++) {
bt.duplicateMovieClip(‘bt’+i, _root.getNextHighestDepth());
_root[‘bt’+i]._y += i22;
_root[‘bt’+i].t = this.childNodes[0].childNodes
.childNodes[0].firstChild;
_root[‘bt’+i].texto.text = _root[‘bt’+i].t;
_root[‘bt’+i].link = this.childNodes[0].childNodes*.childNodes[1].firstChild;
_root[‘bt’+i].onPress = function() {
getURL(this.link, “_self”);
};
_root[‘bt’+i].onRollOver = function() {
this.gotoAndStop(2);
};
_root[‘bt’+i].onRollOut = function() {
this.gotoAndStop(1);
};
}
bt.unloadMovie();
};
stop();

First Way :---------------

var dados:XML = new XML();
dados.ignoreWhite = true;
dados.load(‘data.xml’);
refer = this;

dados.onLoad = function():Void
{
qtd = this.childNodes[0].childNodes.length;

for (i = 0; i < qtd; i++)
{
bt.duplicateMovieClip(‘bt’+i, refer.getNextHighestDepth());
refer[‘bt’+i]._y += i22;
refer[‘bt’+i].t = this.childNodes[0].childNodes
.childNodes[0].firstChild;
refer[‘bt’+i].texto.text = refer[‘bt’+i].t;
refer[‘bt’+i].link = this.childNodes[0].childNodes*.childNodes[1].firstChild;

refer[‘bt’+i].onPress = function()
{
getURL(this.link, “_self”);
};

refer[‘bt’+i].onRollOver = function()
{
this.gotoAndStop(2);
};
refer[‘bt’+i].onRollOut = function()
{
this.gotoAndStop(1);
};
}

bt.unloadMovie();
};
stop();

Second Method:

import mx.utils.Delegate;

var dados:XML = new XML();
dados.ignoreWhite = true;
dados.onLoad = Delegate.create(this,loaded);
dados.load(‘data.xml’);

function loaded()
{
var qtd = this.childNodes[0].childNodes.length;

for (i = 0; i < qtd; i++)
{
bt.duplicateMovieClip(‘bt’+i, this.getNextHighestDepth());
this[‘bt’+i]._y += i22;
this[‘bt’+i].t = this.childNodes[0].childNodes
.childNodes[0].firstChild;
this[‘bt’+i].texto.text = this[‘bt’+i].t;
this[‘bt’+i].link = this.childNodes[0].childNodes*.childNodes[1].firstChild;

this[‘bt’+i].onPress = function()
{
getURL(this.link, “_self”);
};

this[‘bt’+i].onRollOver = function()
{
this.gotoAndStop(2);
};
this[‘bt’+i].onRollOut = function()
{
this.gotoAndStop(1);
};
}

bt.unloadMovie();
};
stop();

Thanks for your reply

I tried both your first method, and also “this._lockroot = true;” above my origional script and they both work great!

Only now, the text from my XML doesn’t show up when I use these methods elseware in the SWF. I know that it is reading the XML because when I rename the actual .XML as a test, a path error comes up in the output.

Upon further testing, the XML text references show up when I run “Test Scene” from within the MC.

But when I test-run the whole thing, the mouse overs and the duplicateMovieClip command both work, but no text???

Here is my XML file if this helps :slight_smile:
It sems as if the “<![CDATA[???]]>” commands are ignored when the MC is not on the main timeline even though everything else works.

<?xml version=“1.0” encoding=“UTF-8”?>
<menu>
<item>
<texto>
<![CDATA[Link 1]]>
</texto>
<link>
<![CDATA[children.php]]>
</link>
</item>
<item>
<texto>
<![CDATA[Link 2]]>
</texto>
<link>
<![CDATA[children.php]]>
</link>
</item>
<item>
<texto>
<![CDATA[Link 3]]>
</texto>
<link>
<![CDATA[children.php]]>
</link>
</item>
<item>
<texto>
<![CDATA[Link 4]]>
</texto>
<link>
<![CDATA[children.php]]>
</link>
</item>
<item>
<texto>
<![CDATA[Link 5]]>
</texto>
<link>
<![CDATA[children.php]]>
</link>
</item>
</menu>

are you able to read in
<item>
<texto>

tags?

[quote=sparkdemon;2359355]are you able to read in
<item>
<texto>

tags?[/quote]

I can tell that the <item> tag works because it duplicates the bt MC the amount of times I have that tag in there. For example, It outputs 5 MC fields and then 4 when I edited one of the <item> tags out.