开发者

Space exteranlly loaded swfs with different heights AS3

I've been battling with this for a while now, I'm loading in swf files into a holder swf and want to position each loaded swf underneath eachother with a 10px margin.

This is what I have so far, Its pulling in all the data correctly now just want to position the loaded swfs.

Any help is much appreciated.


import flash.display.LoaderInfo;

var swfLoader:Loader;
var xml:XML;
var xmlList:XMLList;
var xmlLoader:URLLoader = new URLLoader();

xmlLoader.load(new URLRequest("components.xml"));
xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);

function xmlLoaded(event:Event):void
{
    xml = XML(event.target.data);
    xmlList = xml.children();
    for (var i:int = 0; i < xmlList.length(); i++)
    {
        swfLoader = new Loader();
        swfLoader.load(new URLRequest(xmlList[i].@SWFsource));
      开发者_开发问答  swfLoader.contentLoaderInfo.addEventListener(Event.INIT, loaderInitHandler);
        addChild(swfLoader);
    }
}

function loaderInitHandler(event:Event):void
{
    var swfInfo:LoaderInfo = event.target as LoaderInfo;

    //THIS IS WHERE I WANT TO POSITION MY LOADED SWF'S.
    // I CAN TRACE THEIR HEIGHTS BUT NOT SURE HOW TO POSITION THEM IN Y

    trace(swfLoader.y = swfInfo.height)
}


You will run into troubles when using the swfLoader member variable like this: You add each loader to the stage, but the swfLoader reference always points to only one object (the one added last).

It is also problematic to position your clips like this, because you can only get one loader reference at a time, so you will not be able to calculate the position relative to the other loaders.

If nothing else but loaders are positioned on the stage, you could iterate over the main object's children, but I would strongly recommend storing your clips in an array instead. Your code will look something like this:

var loaders:Array = []; // an Array of loaders instead of a single loader
var xml:XML;
var xmlList:XMLList;
var xmlLoader:URLLoader = new URLLoader();

xmlLoader.load(new URLRequest("components.xml"));
xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);

function xmlLoaded(event:Event):void
{
    xml = XML(event.target.data);
    xmlList = xml.children();
    for (var i:int = 0; i < xmlList.length(); i++)
    {
        var swfLoader:Loader = new Loader(); // local instead of member variable
        swfLoader.load(new URLRequest(xmlList[i].@SWFsource));
        swfLoader.contentLoaderInfo.addEventListener(Event.INIT, loaderInitHandler);
    }
}

function loaderInitHandler(event:Event):void
{
    var swfInfo:LoaderInfo = event.target as LoaderInfo;
    var swfLoader:Loader = swfInfo.loader; 
    if (loaders.length > 0) {
        var lastLoader : Loader = loaders[loaders.length-1]; 
        swfLoader.y = lastLoader.y + lastLoader.height + 10; // positioning happens here
    }
    addChild(swfLoader);
    loaders.push(swfLoader);
}

The loaders are only added to the stage and pushed onto the array when they are fully loaded, so you can be sure lastLoader is always the last recent item that was placed on the stage. You can later use the loaders array to remove or reposition the images.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜