我写了一些非常相似的东西。这是工作代码,您可以按原样使用它或更改它。玩的开心! * Nicholas
顺便说一下,点击切换暂停。不幸的是loadPrevious()没有实现。只需复制并更改loadNext()方法
像这样的东西应该可以工作。如果你想逆循环
// load Previous
if(__currentIndex>1){
__currentIndex--;
}else{
__currentIndex=__xml.child("image").length()-1;
}
这里是Demo类(如何使用ImageSlideshow)
package
{
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import de.goldsource.display.ImageSlideshow;
public class Demo extends Sprite
{
public function Demo()
{
// setup
stage.scaleMode=StageScaleMode.NO_SCALE;
stage.align=StageAlign.TOP_LEFT;
var xml:XML = <slideshow>
<image src='assets/img1.jpg' delay="0.1" />
<image src='assets/img2.jpg' delay="0.1" />
<image src='assets/img3.jpg' delay="0.1" />
</slideshow>;
ImageSlideshow.DEBUG = true;
addChild(new ImageSlideshow(xml));
}
}
}
的幻灯片类。您可以使用它并更改它,但保留许可证文本。该软件包是de.goldsource.display将此代码保存在一个名为ImageSlideshow.as的文件中,并将其放入de/goldsource/display /相对于您的src root或lib目录中。
package de.goldsource.display
{
import flash.display.Bitmap;
import flash.display.Loader;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.net.URLRequest;
import flash.system.System;
import flash.utils.Timer;
import flash.utils.setTimeout;
/**
*
* <p>
* <b>License</b>
* <br/>
* <a href="http://www.opensource.org/licenses/mit-license.php">based on The MIT License (MIT)</a>
* </p>
* <p>
* Copyright (c) 2011 Nicholas Schreiber
* </p>
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
* </p>
* <p>
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
* </p>
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
* </p>
*
* @author Nicholas Schreiber
* */
public class ImageSlideshow extends Sprite
{
/**
* Traces Memory Usage Info
* */
public static var DEBUG:Boolean = false;
/**
* Slideshow data
*/
private var __xml:XML;
/**
* Main Timer
* */
private var __timer:Timer = new Timer(1000);
/**
* True if paused
* */
private var __paused:Boolean = false;
/**
* Current Element
* */
private var __currentIndex:int=-1;
/**
* loads the images
* */
private var __loader:Loader;
/**
* True if Loader has done its job
* */
private var __loaderReady:Boolean=true;
/**
* Error Count
* */
private var __errorCount:uint = 0;
/**
* Only for Debug Purposes
* */
private var __lastMemoryMax:uint = 0;
/**
* ImageSlideshow - Loads and displays Images
*
* <p>
* jpg, gif or png possible<br/>
* delay in seconds
* </p>
* <p>
* <code>
* <slideshow><br/>
* <image src='assets/img1.jpg' delay="0.1" /><br/>
* <image src='assets/img2.png' delay="2" /><br/>
* ...<br/>
* <image src='assets/imgN.gif' delay="0.1" /><br/>
* </slideshow><br/>
* </code>
* </p>
* @param $xml some XML as described above
* */
public function ImageSlideshow($xml:XML=null)
{
__xml = $xml;
__loader = new Loader();
// Click toggles Pause
addEventListener(MouseEvent.CLICK,__onClick);
// The Update Interval
__timer.addEventListener(TimerEvent.TIMER,__onTimer);
// load the first image
loadNext();
}
/**
* Toggle Pause
* */
public function togglePause():void{
if(__paused){
__paused = false;
}else{
__paused = true;
}
__updateTimerStatus();
}
/**
* loadNext Image (looped)
* */
public function loadNext():void{
// return if not possible
if(!__loaderReady || __xml == null)return;
// loader is blocked
__loaderReady = false;
// load Next
if(__currentIndex<__xml.child("image").length()-1){
// slides availabe
__currentIndex++;
}else{
// no more slides, looping the slideshow...
__currentIndex=0;
}
// resetting timer
__timer.stop();
__timer.reset();
// setting delay
__timer.delay = __xml.child("image")[__currentIndex][email protected]*1000;
// setup loader
__loader.contentLoaderInfo.addEventListener(Event.COMPLETE, __onLoadComplete);
__loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,__onIOError);
// load
__loader.load(new URLRequest(__xml.child("image")[__currentIndex][email protected]));
}
/**
* toggles Pause on Click
* */
private function __onClick($e:MouseEvent):void{
togglePause();
}
/**
* starts the timer if possible or stops it if paused
* */
private function __updateTimerStatus():void{
if(!__loaderReady)return;
if(__paused && __timer.running){
__timer.stop();
}else{
__timer.start();
}
}
/**
* Invoked by the timer
* */
private function __onTimer(e:TimerEvent):void{
loadNext();
}
/**
* Invoked if image is not existent (wrong src url!)
* */
private function __onIOError(e:IOErrorEvent):void{
// remove listeners
__loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, __onLoadComplete);
__loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,__onIOError);
// iterate error count, will stop the app when > 10 in a row
__errorCount ++;
trace("Image could not be loaded "+__errorCount +" of 10 attempts");
if(__errorCount > 10)return;
// if more than one image in list try next
__loaderReady = true;
if(__xml.child("image").length()>1)loadNext();
}
/**
* Invoked when image is loaded
* */
private function __onLoadComplete(e:Event):void{
// remove the listeners
__loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, __onLoadComplete);
__loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,__onIOError);
// reset the error count, cause on image was loaded successful
__errorCount = 0;
// add the loaded image to the display
addChild(__loader.content);
// yes the loader is no longer blocked
__loaderReady = true;
// restarts the timer if was running
__updateTimerStatus();
// removes
__cleanUp();
}
/**
* Removes all unnecessary Bitmaps
* */
private function __cleanUp():void{
// leave method if nothing to do (only one object on screen)
if(numChildren==1)return;
/*
never forget to dispose() a bitmap you want to get rid of,
otherwise FlashPlayer will not remove it from the RAM
removing and disposing within one line
*/
if(getChildAt(0) is Bitmap)Bitmap(removeChildAt(0)).bitmapData.dispose();
// Debug mode only
if(!DEBUG)return;
if(__lastMemoryMax<System.totalMemory){
trace("New Memory Peak "+((__lastMemoryMax= System.totalMemory)/Math.pow(1024,2)).toFixed(2)+" MB");
}
}
/**
* xml Getter/Setter - Slideshow data
* <p>
* jpg, gif or png possible<br/>
* delay in seconds
* </p>
* <p>
* <code>
* <slideshow><br/>
* <image src='assets/img1.jpg' delay="0.1" /><br/>
* <image src='assets/img2.png' delay="2" /><br/>
* ...<br/>
* <image src='assets/imgN.gif' delay="0.1" /><br/>
* </slideshow><br/>
* </code>
* </p>
*
* @param value XML
* @return XML
* */
public function get xml():XML
{
return __xml;
}
public function set xml(value:XML):void
{
__xml = value;
__currentIndex = -1;
loadNext();
}
}
}
这波斯沃思,我很欣赏这个建议。现在,它“起作用”,脚本暂停5秒(空白画布),然后一次显示所有图像,而不是按顺序显示。 – dallen
你介意给我一个关于我如何做图像队列的例子吗? – dallen
@dallen - 查看我的更新 – Bosworth99