Monday, December 15, 2008

Slideshow with autoplay and reset delay

This code starts with a long pause then starts looping through images. If the user interacts with the slideshow, the timer is reset then starts playing again later if the user hasnt interacted.



//setup---------------------------------------------
var resetTime:uint=5000;
var pauseBetweenSlides:uint=5000;
var slideCounter:uint=1;
var prevSlide:uint=0;

var startShowTimer:Timer=new Timer(resetTime,1);
startShowTimer.addEventListener(TimerEvent.TIMER, setUpTimer);
var slideShowTimer:Timer=new Timer(pauseBetweenSlides,0);
slideShowTimer.addEventListener(TimerEvent.TIMER, autoAdvance);

//auto advance
function autoAdvance(e:TimerEvent):void {
nexttSlide();
}

//next
function nexttSlide():void {
prevSlide=slideCounter;
slideCounter++;
if (slideCounter==6) {
slideCounter=1;
}
goToSlide();
}
//prev
function prevvSlide():void {
prevSlide=slideCounter;
slideCounter--;
if (slideCounter==0) {
slideCounter=5;
}
goToSlide();
}
//display next slide and hide previous
function goToSlide():void {
slideshow_mc["slide"+slideCounter+"_mc"].gotoAndPlay("show");
if (prevSlide>0) {
slideshow_mc["slide"+prevSlide+"_mc"].gotoAndPlay("hide");
}
}
//pause slideshow and reset timer
function resetTimer():void {
slideShowTimer.stop();
startShowTimer.reset();
startShowTimer.start();
}
//called when reset pause is finished
function setUpTimer(e:TimerEvent):void {
startShowTimer.reset();
slideShowTimer.start();
}


resetTimer();
goToSlide();


//Controls--------
function showControls():void {
slideShowPrev_mc.gotoAndPlay("show");
slideShowNext_mc.gotoAndPlay("show");
}
function hideControls():void {
slideShowPrev_mc.gotoAndPlay("hide");
slideShowNext_mc.gotoAndPlay("hide");
}



slideShowNext_mc.addEventListener(MouseEvent.CLICK, invokeNextSlide);
function invokeNextSlide(e:MouseEvent):void {
resetTimer();
nexttSlide();
}
slideShowPrev_mc.addEventListener(MouseEvent.CLICK, invokePrevSlide);
function invokePrevSlide(e:MouseEvent):void {
resetTimer();
prevvSlide();
}

No comments: