0

下面是我的幻灯片的代码,我遇到的问题是,在加载的第一张图像具有我需要的完美尺寸但第二张图像加载的尺寸不同后,我希望它与第一张的尺寸相同。图像也没有按顺序加载。任何专家都可以找出并解决问题。

stop();
import gs.*;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;

stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;

//hides the description box until the image is loaded
//hides the image until it is loaded

theImage.alpha=0;
loadingBar.visible = false;

//variables to hold the final coordinates of the image tween
var finalX:Number;
var finalY:Number;

//variable to hold the number of images in the XML
var listLength:Number;

//keeps track of what image should be displayed
var currPainting:Number=0;

//arrays to hold the contents of the XML, using this to allow
//for the random order of the images
var imageArray:Array = new Array();


//Loader event for the XML
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, onLoaded);

var xml:XML;

loader.load(new URLRequest("paintings.xml"));

function onLoaded(e:Event):void {
//load XML
xml=new XML(e.target.data);
var il:XMLList=xml.images;
listLength=il.length();

populateArray();
}

function populateArray():void {
//takes the properties defined in the XML and stores them 
//into arrays
var i:Number;
for (i = 0; i < listLength; i++) {
    imageArray[i]=xml.images[i].pic;

}
beginImage();
}

function beginImage():void {
//grabs a random number between 0 and the number
//of images in the array
currPainting=Math.floor(Math.random()*imageArray.length);

//load description

var imageLoader = new Loader();

//catches errors if the loader cannot find the URL path
imageLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, catchFunction);
//actually loads the URL defined in the image array
imageLoader.load(new URLRequest(imageArray[currPainting]));
//adds a listener for while the image is loading
imageLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, imgLoading);
//adds a listener for what to do when the image is done loading
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imgLoaded);


function catchFunction(e:IOErrorEvent) {
    trace("Bad URL: " + imageArray[currPainting] + " does not exist");
    //take out the bad URL from the array
    imageArray.splice(currPainting,1);


    //check to see if there are images left,
    //else restart the slideshow
    if (imageArray.length==0) {
        populateArray();
    } else {
        beginImage();
    }
}

function imgLoading(event:ProgressEvent):void{
    //show the loading bar, and update the width
    //based on how much is loaded
    loadingBar.visible = true;
    loadingBar.x = stage.stageWidth/2;
    loadingBar.y = stage.stageHeight/3
    loadingBar.bar.width = (event.bytesLoaded/event.bytesTotal)*100;
}

function imgLoaded(event:Event):void {
    loadingBar.visible = false;
    var scale:Number =  stage.stageWidth * 0.292708 / theImage.width;
    //add the image and get the dimensions to center the image
    theImage.addChild(imageLoader);
    theImage.x = stage.stageWidth/2.85;
    theImage.y = 0;

    if(theImage.height * scale > stage.stageHeight){
    scale = stage.stageHeight * 0.704918 / theImage.height;
    }   

    // apply the scale to the image
    theImage.scaleX = theImage.scaleY = scale;


    //take the contents of the loaded image and cast it as bitmap data
    //to allow for bitmap smoothing

    var image:Bitmap = imageLoader.content as Bitmap;
    image.smoothing=true;
            //start tween function
    easeIn();
}
}

function easeIn():void {

TweenLite.to(theImage, 8, {onComplete:hideStuff});
TweenLite.to(theImage, 1, {alpha:1, overwrite:0});
}

function hideStuff():void {
TweenLite.to(theImage, 1, {alpha:0, onComplete:nextImage});

}


function nextImage():void {
//take out the image that was just displayed
imageArray.splice(currPainting,1);


//remove the picture
theImage.removeChildAt(0);

//start over
if (imageArray.length==0) {
    populateArray();
} else {
    beginImage();
}
}

这是xml

    <xml>
            <images><pic>portfolio1.jpg</pic></images>
            <images><pic>portfolio2.jpg</pic></images>
            <images><pic>portfolio3.jpg</pic></images>
            <images><pic>portfolio4.jpg</pic></images>
            <images><pic>portfolio5.jpg</pic></images>

    </xml>
4

1 回答 1

0

我不认为theImage'''''''''''''''''''''''''''')的高度代表加载后的高度。这应该可以与加载器一起使用。为了适应不同尺寸的图像,您必须在应用比例时带来图像的实际高度/宽度。否则,当然,您可以将所有图像预先调整为相同大小。

关于随机加载,这正是

currPainting=Math.floor(Math.random()*imageArray.length);

beginImage做。要按顺序完成加载,您可以直接选择第 0 个元素。

于 2013-09-27T22:40:27.397 回答