AS3:如何创建线库

时间:2014-11-28 12:14:06

标签: xml actionscript-3 gallery line

我正在尝试创建一个线条库:所有照片都设置在另一个旁边,但照片的宽度不同。我必须将x loacation放在function callThumbs()中,但我在函数thumbLoaded(e:Event)中获得了所需的widths

我将它们放入variables minWidthminiHeight。我已经看到一些带有callback函数的示例,但我无法将其实现到我的代码中。有人可以帮忙吗?

var myXMLLoader:URLLoader = new URLLoader();
myXMLLoader.load(new URLRequest("images.xml"));
myXMLLoader.addEventListener(Event.COMPLETE, processXML);

var my_thumb_width:Number;
var my_thumb_height:Number;
var my_images:XMLList;
var my_total:Number;

var currentX:Number = 0;

var miniWidth:Number;
var miniHeight:Number;

if (stage)
{
    initialize();
}
else
{
    addEventListener(Event.ADDED_TO_STAGE,initialize);
}

    function initialize(e:Event = null):void
{
    removeEventListener(Event.ADDED_TO_STAGE,initialize);
    stage.scaleMode = StageScaleMode.NO_SCALE;
    stage.align = StageAlign.TOP_LEFT;
    stage.addEventListener(Event.RESIZE,onResizeDoc);
    stage.dispatchEvent(new Event(Event.RESIZE));
    stage.addEventListener(Event.REMOVED_FROM_STAGE, onRemove);
    resizeMyDoc();
}

function onResizeDoc(event:Event = null):void
{
    resizeMyDoc();
}

function resizeMyDoc():void
{
    var sw:Number = stage.stageWidth;
    var sh:Number = stage.stageHeight;

    if (stage.displayState == StageDisplayState.FULL_SCREEN)
    {
        //code nor relevant to my issue
    }
    else
    {
        //code nor relevant to my issue
    }
}

function processXML(e:Event):void
{
    var myXML:XML = new XML(e.target.data);

    my_thumb_width = myXML. @ WIDTH;
    my_thumb_height = myXML. @ HEIGHT;
    my_images = myXML.IMAGE;
    my_total = my_images.length();

    preloaders_mc = new MovieClip();
    preloaders_mc.x = miniaturki.x;
    preloaders_mc.y = miniaturki.y;
    addChild(preloaders_mc);

    miniaturki.buttonMode = true;

    callThumbs();

    myXMLLoader.removeEventListener(Event.COMPLETE, processXML);
    myXMLLoader = null;
}

function callThumbs():void
{
    for (var i:Number = 0; i < my_total; i++)
    {
        var thumb_url = my_images[i].@THUMB;;
        var thumb_loader = new Loader();
        thumb_loader.load(new URLRequest(thumb_url));
        thumb_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, thumbLoaded);

        thumb_loader.name = i;
    }
}

function thumbLoaded(e:Event):void
{
    var my_thumb:Loader = Loader(e.target.loader);
    miniaturki.addChild(my_thumb);

    miniWidth = my_thumb.width;
    miniHeight = my_thumb.height;

    trace(miniWidth);
    trace(miniHeight);

    my_thumb.contentLoaderInfo.removeEventListener(Event.COMPLETE, thumbLoaded);
}

function onRemove(e:Event=null):void
{
    stage.removeEventListener(Event.RESIZE, onResizeDoc);
}

以下是我正在谈论的两个functions;

function callThumbs():void
{
    for (var i:Number = 0; i < my_total; i++)
    {
        var thumb_url = my_images[i].@THUMB;;
        var thumb_loader = new Loader();
        thumb_loader.load(new URLRequest(thumb_url));
        thumb_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, thumbLoaded);

        thumb_loader.name = i;
    }
}

function thumbLoaded(e:Event):void
{
    var my_thumb:Loader = Loader(e.target.loader);
    miniaturki.addChild(my_thumb);

    my_tweens[Number(my_thumb.name)]=new Tween(my_thumb, "alpha", Strong.easeIn, 0,1,0.5, true);

    miniWidth = my_thumb.width; // traced width is the one that I need
    miniHeight = my_thumb.height;

    trace(miniWidth);
    trace(miniHeight);

    my_thumb.contentLoaderInfo.removeEventListener(Event.COMPLETE, thumbLoaded);
}

第二个问题是resize handler。当我删除他时,图片为loaded有任何问题,但当resize handler停留时,我首先要resize窗口查看图片。早些时候,当我遇到同样的问题时,我只需要添加DispatchEventstage.dispatchEvent(new Event(Event.RESIZE));,但我添加了它,问题仍然存在

任何人都可以帮忙!

1 个答案:

答案 0 :(得分:0)

将您的加载器存储在一个数组中 - 将所有内容存储在数组中。

在顶部创建数组:

var aThumbs:Array = new Array();

在您将加载器添加到舞台时将其推入:

var my_thumb:Loader = Loader(e.target.loader);
miniaturki.addChild(my_thumb);
aThumbs.push(my_thumb);

然后使用数组引用如下:

iGetThumbWidth = aThumbs[i].width;