2013-09-27 41 views
0

下面是我的幻燈片的代碼,我遇到的問題是,第一個圖像加載後有我需要的完美大小,但第二個圖像加載有不同的大小,我希望它的大小與第一個。此外,圖像不會按順序加載。任何專家都可以找出並解決問題。as3 xml幻燈片

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> 
+0

爲什麼不讓圖像在閃光燈之外的圖像大小相同? – Ronnie

回答

0

我不認爲theImage的高度表示加載後圖像高度。這應該可以在裝載機上使用。爲了適應不同大小的圖像,您必須在應用比例時帶上實際的圖像高度/寬度。否則,當然,您可以預先調整大小的所有圖像的大小。

關於隨機載荷,這正是

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

beginImage在做什麼。要按順序完成加載,可以直接拾取第0個元素。