2009-03-19 5 views
0

J'ai ce code diaporama AS3, qui charge un fichier XML avec une liste d'images. Qu'est-ce que je cherche, est un moyen d'ignorer la partie XML, et il suffit de charger toutes les images dans le dossier "images"?Charger les images du dossier dans le diaporama

Comment puis-je ajouter un booléen constant qui décide s'il doit lire les images au hasard ou non? const RANDOM: booléen = true; ...

// 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][email protected])); 
    } 
} 

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(); 

Répondre

1

Si vous utilisez PHP sur votre serveur quelque chose comme

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>'; 

devrait faire l'affaire.

Il suffit de pointer votre application de diaporama sur le script PHP et il va créer la liste de façon dynamique.

0

Eh bien vous pouvez simplement intégrer le XML dans actionscript si vous voulez sauter le chargement:

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

Si vous voulez sauter cela aussi bien que vous pouvez nommer les images sur un certain chemin de sorte que vous pouvez les charger aveuglément (image0.jpg, image1.jpg, etc.), jusqu'à ce que votre séquence de chargement jette une erreur (que vous attrapez bien sûr) que le imageN n'existe pas (c'est sale bien que!). Le meilleur dans cette situation est d'avoir un petit script côté serveur qui répertorie tous les fichiers dans un dossier donné et de sortie leurs références dans un XML qui peut être récupéré à partir de votre SWF.

+0

Comment puis-je l'obtenir pour lire des images aléatoires? –

+0

Créez un tableau avec les index et mélangez-le. –

+0

Comment ferais-je cela? Je suis un noob. –

0

Sauf si vous déployez votre application Flash via AIR, vous n'avez pas accès au système, vous ne pouvez donc pas lire tout le contenu d'un dossier.

Vous pouvez utiliser PHP ou la langue de votre choix pour lire le contenu d'un dossier et construire le fichier XML que vous allez charger.

Il suffit de changer strXMLPath à http://example.com/yourphpfile.php et il se chargera comme un document XML. Vous devez changer le type MIME en mimetype="application/xml".

Questions connexes