as3.0 xml照片库预加载器

时间:2014-02-10 17:51:25

标签: xml actionscript-3 preloader photo-gallery

我有xml动态照片库(带鼠标控制的滚动条和电影面板,当点击滚动条图像时大图片工作。)关于预加载器的问题。我可以使用预加载器用于大图片部分但是当我尝试做同样的事情时滚动条部分什么都没发生。你可以帮我添加预加载器到滚动部分吗?求助(请注意:我没有为卷轴部分添加预加载器代码。)

import caurina.transitions.*;
import fl.controls.ProgressBar;
import fl.transitions.Tween;
import fl.transitions.TweenEvent;
import flash.display.Sprite;
import fl.transitions.easing.*;
import flash.display.MovieClip;


var full_tween:Tween;
var full_mc:MovieClip;
var thisOne:MovieClip;
var preloaders_mc:MovieClip;
var my_images:XMLList;
var my_total:Number;
//load xml
var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML(); 
xmlLoader.addEventListener(Event.COMPLETE, LoadXML);

var xmlPath:String = "image-scroller.xml";
xmlLoader.load(new URLRequest(xmlPath));
trace("loading xml from: " + xmlPath);

function LoadXML(e:Event):void {
    trace("xml loading complete");
    xmlData = new XML(e.target.data);
    //trace(xmlData.image);
    my_images = xmlData.image; 
    my_total = my_images.length();

    buildScroller();
    createContainer();
}

function createContainer():void {
    thisOne = new MovieClip();
    addChild(thisOne);

    preloaders_mc = new MovieClip();
    preloaders_mc.x = thisOne.x;
    preloaders_mc.y = thisOne.y;
    addChild(preloaders_mc);
}


var konum:Object = new Object();
var imageHolder:Sprite = new Sprite(); 
var scroller:MovieClip = new MovieClip();
var speed:Number;
var padding:Number = 10;
var thumbFadeOut:Number = 0;
var thumbFadeIn:Number = 1;
var thumbSmall:Number = 1;
var thumbLarge:Number = 1.1;
this.addChild(imageHolder); 
this.addChild(scroller);
scroller.y = scroller.x = padding;
konum.dikey = 485 ;
scroller.y = konum.dikey;

//build scroller from xml
function buildScroller():void{
    trace("build Scroller");
    for (var item:uint = 0; item < my_total; item++ )  {
        var thumb_url = my_images[item].@src;  
        var thisOne:MovieClip = new MovieClip();

        //outline
        var blackBox:Sprite = new Sprite();
        blackBox.graphics.beginFill(0xFFFFFF);
        blackBox.graphics.drawRect( -1, -1, 142, 107);
        blackBox.alpha = thumbFadeOut;
        thisOne.addChild(blackBox);
        thisOne.blackBox = blackBox;

        thisOne.x = thisOne.myx = (140 + padding) * item;
        thisOne.itemNum = item;

        //image container
        var thisThumb:Sprite = new Sprite();
        //add image
        var ldr:Loader = new Loader();
        ldr.load(new URLRequest(thumb_url));

        //assign event listeners for Loader
        ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
        ldr.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
        thisThumb.addChild(ldr);
        thisOne.addChild(thisThumb);

        //create listeners for this thumb
        thisOne.buttonMode = true;
        thisOne.addEventListener(MouseEvent.MOUSE_OVER, overScrollerItem);
        thisOne.addEventListener(MouseEvent.MOUSE_OUT, outScrollerItem);
        thisOne.addEventListener(MouseEvent.CLICK, callFull);
        thisOne.name = my_images[item].@title;  

        //add item
        scroller.addChild(thisOne);
    }

    scroller.addEventListener(Event.ENTER_FRAME, moveScrollerThumbs);
    trace("termination of build scroller");
}

function overScrollerItem(e:MouseEvent):void {
    //trace("over" + e.currentTarget.name);
    Tweener.addTween(e.currentTarget, { scaleX:thumbLarge, scaleY:thumbLarge, x:e.currentTarget.myx - e.currentTarget.width * Math.abs(thumbSmall - thumbLarge)/2, y: -e.currentTarget.width * Math.abs(thumbSmall - thumbLarge)/2, time:1 } );
    Tweener.addTween(e.currentTarget.blackBox, { alpha:thumbFadeIn, time: 1 } );
}
function outScrollerItem(e:MouseEvent):void {
    //trace("out" + e.currentTarget.name);
    Tweener.addTween(e.currentTarget, { scaleX:thumbSmall, scaleY:thumbSmall, x:e.currentTarget.myx, y:0, time:1 } );
    Tweener.addTween(e.currentTarget.blackBox, { alpha:thumbFadeOut, time: 1 } );
}
function callFull(e:MouseEvent):void {

    var full_loader:Loader = new Loader();
    var full_url = my_images.(@title == e.currentTarget.name).@url;  
    full_loader.load(new URLRequest(full_url));
    full_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completefull);
    full_loader.contentLoaderInfo.addEventListener(Event.INIT, fullLoaded);
    full_mc = new MovieClip();
    imageHolder.removeChildren();
    imageHolder.addChild(full_mc)

    var full_pb:ProgressBar = new ProgressBar();
    full_pb.source = full_loader.contentLoaderInfo;
    full_pb.x = (stage.stageWidth - full_pb.width)/2;
    full_pb.y = (stage.stageHeight - full_pb.height)/2;
    preloaders_mc.addChild(full_pb);

    full_pb.addEventListener(Event.COMPLETE, donePb);

}

function donePb(e:Event):void {
    var my_pb:ProgressBar = ProgressBar(e.target);
    preloaders_mc.removeChild(my_pb);
    my_pb.removeEventListener(Event.COMPLETE, donePb);
}

function fullLoaded(e:Event):void {

    var konumla:Object = new Object();
    full_mc = new MovieClip();

    konumla.dikey = 15 ;
    full_mc.y = konumla.dikey;

    imageHolder.removeChildren(); 
    imageHolder.addChild(full_mc); 
    var my_loader:Loader = Loader(e.target.loader);
    full_mc.addChild(my_loader);
    full_tween = new Tween(my_loader, "alpha", Strong.easeIn, 0,1,0.5, true);

    my_loader.x = (stage.stageWidth - my_loader.width)/2;
    my_loader.y = (stage.stageHeight - my_loader.height)/2;

    my_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, fullLoaded);
}

function completefull(e:Event):void {
    //trace("thumbnail complete "+e.target.loader.parent.parent.name);
    //size image into scroller
    resizeMe(e.target.loader, 800, 450);

}

function completeHandler(e:Event):void {
    //trace("thumbnail complete "+e.target.loader.parent.parent.name);
    //size image into scroller
    resizeMe(e.target.loader.parent, 140, 105, true, true, false);
    Tweener.addTween(e.target.loader.parent.parent, { alpha:1, time: .5 } );
}
function errorHandler(e:IOErrorEvent):void {
    trace("thumbnail error="+e);
}


//The resizing function
// parameters
// required: mc = the movieClip to resize
// required: maxW = either the size of the box to resize to, or just the maximum desired width
// optional: maxH = if desired resize area is not a square, the maximum desired height. default is to match to maxW (so if you want to resize to 200x200, just send 200 once)
// optional: constrainProportions = boolean to determine if you want to constrain proportions or skew image. default true.
function resizeMe(mc:DisplayObject, maxW:Number, maxH:Number=0, constrainProportions:Boolean=true, centerHor:Boolean=true, centerVert:Boolean=true):void{
    maxH = maxH == 0 ? maxW : maxH;
    mc.width = maxW;
    mc.height = maxH;
    if (constrainProportions) {
        mc.scaleX < mc.scaleY ? mc.scaleY = mc.scaleX : mc.scaleX = mc.scaleY;
    }
    if (centerHor) {
        mc.x = (maxW - mc.width) / 2;
    }
    if (centerVert){
        mc.y = (maxH - mc.height) / 2;
    }
}

function moveScrollerThumbs(e:Event):void {
    if ( mouseY > scroller.y && mouseY < scroller.y + scroller.height) {//vertically over scroller
        if (mouseX < stage.stageWidth/2 - padding*2 && mouseX > 0) {//left of stage explicitly
            speed = -(mouseX - (stage.stageWidth/2 - padding*2)) / 8;
        }
        else if (mouseX > stage.stageWidth/2 + padding*2 && mouseX < stage.stageWidth) {//right of stage explicitly
            speed = -(mouseX - (stage.stageWidth/2 + padding*2)) / 8;
        }
        else {
            speed = 0;
        }
        scroller.x += speed;

        //scroller limits
        if (scroller.x < -scroller.width + stage.stageWidth - padding) { //if scrolled too far left
            scroller.x = -scroller.width + stage.stageWidth - padding;
        }
        else if (scroller.x > padding) { //if scrolled to far right
            scroller.x = padding;
        }
    }
}

0 个答案:

没有答案