PDA

View Full Version : getChildByName + Loader



alexmmn
December 7th, 2007, 05:38 PM
What seemed logical to do is not working apparently.

I have four buttons named button_0...button_3 on the stage and compiled into assets.swf. Essentially, I want the layout to be handled by designers and placed on stage as it should appear. However, this old approach is giving me a trouble with AS3. Seems to be the problem with the Loader class allowed to have only one child. So how do you get access to the children of that child and its properties (such as x,y,etc.)? Any ideas?

Thanks

here's the code:
---------------------
public class Test {
var assets:Loader;
public function Test()
{
assets = new Loader();
assets.contentLoaderInfo.addEventListener(Event.CO MPLETE, completeListener);
assets.contentLoaderInfo.addEventListener(IOErrorE vent.IO_ERROR, ioErrorListener);
assets.load(new URLRequest('assets.swf'));
}
private function completeListener(e:Event):void
{
var loaderInfo:LoaderInfo = e.target as LoaderInfo;
var m:MovieClip= MovieClip(loaderInfo.content);
array= new Array();
for (var i=0; i<m.numChildren; i++)
{
array.push(m.getChildByName("button_"+i));
array[i].x = 50+150*i;
array[i].y= 100;
addChild(array[i]);
}
}
}

Dazzer
December 8th, 2007, 12:19 AM
are those buttons on the stage?

It might be possible to just do



var temp:MovieClip = loader.content.button_1 as MovieClip
temp.whatever = whatever;

alexmmn
December 8th, 2007, 05:45 PM
are those buttons on the stage?

It might be possible to just do



var temp:MovieClip = loader.content.button_1 as MovieClip
temp.whatever = whatever;


Yes, they are on the stage and named sequentially. The idea is not to pre-define how many buttons there will be, but to be able to "analyze" as you initialize the application. Meaning, that if

var i=0;
while (loader.content.getChildByName("button"+i)!=undefined)
{
i++;
someArray.push(loader.content.getChildByName("button"+i));
addChild(someArray[i]);
}

Do you see where I am trying to get to?
The goal is to analyze the stage as oppose to know what's given in advance. This technique was primarily used for widgetized scenarios. For example, a matching game, where one time you may have 7 buttons with 7 matches, and the other time you may have 15 buttons with 15 matches or something like that.

Dazzer
December 9th, 2007, 01:14 AM
Could you just define a public function in the document class?



public function giveMeButtons():Array{
returns AllTheButtonsInAnArrayConvenientlyEasyAndNoFuss;
}


Then call it from the loader.

Is there a reason why the above code you had doesn't work? From what I see it should work...

alexmmn
December 10th, 2007, 12:29 AM
Could you just define a public function in the document class?



public function giveMeButtons():Array{
returns AllTheButtonsInAnArrayConvenientlyEasyAndNoFuss;
}


Then call it from the loader.

Is there a reason why the above code you had doesn't work? From what I see it should work...

Once again, the idea is not to rely upon the code in the "skin", if you will. The idea is for the skin to contain assets only.

I solved it. Apparently, the problem was that Safari caches swfs and even after hitting refresh it was still producing the old "picture". GotDamEat!

:-)

Dazzer
December 10th, 2007, 12:31 AM
Well glad you solved it. ^^