【问题标题】:Load images from folder into slideshow将文件夹中的图像加载到幻灯片中
【发布时间】:2009-06-12 20:21:42
【问题描述】:

我有这个 AS3 幻灯片代码,它加载带有图像列表的 XML。我正在寻找的是一种跳过 XML 部分的方法,只需加载文件夹“images”中的所有图像?

如何添加一个常量布尔值来决定它是否应该随机播放图像? 常量随机:布尔=真; ...

// import tweener
import caurina.transitions.Tweener;

// delay between slides
const TIMER_DELAY:int = 2000;
// fade time between slides
const FADE_TIME:Number = 1;

// flag for knowing if slideshow is playing
var bolPlaying:Boolean = true;
// reference to the current slider container
var currentContainer:Sprite;
// index of the current slide
var intCurrentSlide:int = -1;
// total slides
var intSlideCount:int;
// timer for switching slides
var slideTimer:Timer;
// slides holder
var sprContainer1:Sprite;
var sprContainer2:Sprite;
// slides loader
var slideLoader:Loader;
// url to slideshow xml
var strXMLPath:String = "slideshow.xml";
// slideshow xml loader
var xmlLoader:URLLoader;
// slideshow xml
var xmlSlideshow:XML;

function initSlideshow():void { 
    // create new urlloader for xml file
    xmlLoader = new URLLoader();
    // add listener for complete event
    xmlLoader.addEventListener(Event.COMPLETE, onXMLLoadComplete);
    // load xml file
    xmlLoader.load(new URLRequest(strXMLPath));

    // create new timer with delay from constant
    slideTimer = new Timer(TIMER_DELAY);
    // add event listener for timer event
    slideTimer.addEventListener(TimerEvent.TIMER, nextSlide);

    // create 2 container sprite which will hold the slides and
    // add them to the masked movieclip
    sprContainer1 = new Sprite();
    sprContainer2 = new Sprite();
    mcSlideHolder.addChild(sprContainer1);
    mcSlideHolder.addChild(sprContainer2);

    // keep a reference of the container which is currently
    // in the front
    currentContainer = sprContainer2;
}

function onXMLLoadComplete(e:Event):void {
    // create new xml with the received data
    xmlSlideshow = new XML(e.target.data);
    // get total slide count
    intSlideCount = xmlSlideshow..image.length();
    // switch the first slide without a delay
    switchSlide(0);
}

function fadeSlideIn(e:Event):void {
    // add loaded slide from slide loader to the
    // current container
    addSlideContent();

    // check if the slideshow is currently playing
    // if so, show time to the next slide. If not, show
    // a status message
    if(bolPlaying) {
    } else {
    }
    // fade the current container in and start the slide timer
    // when the tween is finished
    Tweener.addTween(currentContainer, {alpha:1, time:FADE_TIME, onComplete:onSlideFadeIn});
}

function onSlideFadeIn():void {
    // check, if the slideshow is currently playing
    // if so, start the timer again
    if(bolPlaying && !slideTimer.running)
        slideTimer.start();
}

function switchSlide(intSlide:int):void {
    // check if the last slide is still fading in
    if(!Tweener.isTweening(currentContainer)) {
        // check, if the timer is running (needed for the
        // very first switch of the slide)
        if(slideTimer.running)
            slideTimer.stop();
        // change slide index
        intCurrentSlide = intSlide;
        // check which container is currently in the front and
        // assign currentContainer to the one that's in the back with
        // the old slide
        if(currentContainer == sprContainer2)
            currentContainer = sprContainer1;
        else
            currentContainer = sprContainer2;
        // hide the old slide
        currentContainer.alpha = 0;
        // bring the old slide to the front
        mcSlideHolder.swapChildren(sprContainer2, sprContainer1);
        // delete loaded content
        clearLoader();
        // create a new loader for the slide
        slideLoader = new Loader();
        // add event listener when slide is loaded
        slideLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, fadeSlideIn);
        // load the next slide
        slideLoader.load(new URLRequest(xmlSlideshow..image[intCurrentSlide].@src));
    }
}

function nextSlide(e:Event = null):void {
    // check, if there are any slides left, if so, increment slide
    // index
    if(intCurrentSlide + 1 < intSlideCount)
        switchSlide(intCurrentSlide + 1);
    // if not, start slideshow from beginning
    else
        switchSlide(0);
}

function previousSlide(e:Event = null):void {
    // check, if there are any slides left, if so, decrement slide
    // index
    if(intCurrentSlide - 1 >= 0)
        switchSlide(intCurrentSlide - 1);
    // if not, start slideshow from the last slide
    else
        switchSlide(intSlideCount - 1);
}

function clearLoader():void {
    try {
        // get loader info object
        var li:LoaderInfo = slideLoader.contentLoaderInfo;
        // check if content is bitmap and delete it
        if(li.childAllowsParent && li.content is Bitmap){
            (li.content as Bitmap).bitmapData.dispose();
        }
    } catch(e:*) {}
}

function addSlideContent():void {
    // empty current slide and delete previous bitmap
    while(currentContainer.numChildren){Bitmap(currentContainer.getChildAt(0)).bitmapData.dispose(); currentContainer.removeChildAt(0);}

    // create a new bitmap with the slider content, clone it and add it to the slider container
    var bitMp:Bitmap =  new Bitmap(Bitmap(slideLoader.contentLoaderInfo.content).bitmapData.clone());
    currentContainer.addChild(bitMp);
}

// init slideshow
initSlideshow();

【问题讨论】:

    标签: flash actionscript-3 slideshow


    【解决方案1】:

    如果您在服务器上使用 PHP,例如

    header('Content-type: applicaiton/xml;charset=utf-8');
    $dir = dir('images');
    echo '<images>', "\n";
    foreach ($dir as $file) {
        if ($file == '.' || $file == '..') {
            continue;
        }
        echo '<image>', $file, '</image>', "\n";
    }
    echo '</images>';
    

    应该可以解决问题。

    只需将您的幻灯片应用指向 PHP 脚本,它就会动态创建列表。

    【讨论】:

      【解决方案2】:

      如果你想跳过加载,你可以简单地将 XML 集成到 actionscript 中:

      var xmlSlideshow:XML = <images><image /><image /></ images>;
      

      如果您也想跳过它,您可以以某种方式命名图像,这样您就可以盲目地加载它们(image0.jpg、image1.jpg、 等),直到您加载由于 imageN 不存在(虽然这很脏!),因此序列会引发错误(您当然会捕获)。

      在这种情况下,最好的办法是有一个小型服务器端脚本,列出给定文件夹中的所有文件,并将它们的引用输出到可以从 SWF 中检索的 XML 中。

      【讨论】:

      • 如何让它播放随机图片?
      • 用索引创建一个数组并随机播放。
      • 我该怎么做?我是菜鸟。
      【解决方案3】:

      除非您通过 AIR 部署 Flash 应用程序,否则您无权访问系统,因此您无法读取文件夹的所有内容。

      您可以使用 PHP 或您选择的服务器端语言来读取文件夹的内容并构建您将加载的 xml。

      只需将strXMLPath 更改为http://example.com/yourphpfile.php,它就会加载为一个xml 文档。您应该将 mimetype 更改为 mimetype="application/xml"

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-03-18
        • 1970-01-01
        • 1970-01-01
        • 2017-03-23
        • 2012-05-01
        • 2021-04-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多