AutoPilot - cycle items in some seconds


/ Published in: ActionScript 3
Save to your folder(s)

This is lightwave script, made for banners/slideshows. You pass number items, time for slide and autostart. If you have 5 items and pass 2 seconds each, trought 2 seconds interval the numbers will be sldied (1,2,3,4,5,1,2,3,4,5,1,2,3..) and event will be passed. The code is self-explanary, for some real-world examples write me a comment


Copy this code and paste it in your HTML
  1. package {
  2.  
  3. import flash.utils.Timer;
  4. import flash.events.Event;
  5. import flash.events.TimerEvent;
  6. import flash.events.EventDispatcher;
  7.  
  8. public class AutoPilot extends EventDispatcher {
  9.  
  10. //static const for event dispatching
  11. public static const CHANGE:String = "slideChange"; // dispatched every time slide changes
  12. public static const STOP:String = "slideStop"; // dispatched on stop();
  13. public static const PLAY:String = "slidePlay"; // dispatched on play();
  14. public static const RESUME:String = "slideResume"; // dispatched on resume();
  15.  
  16. //autoPilot variables
  17. private var _slides:Number;
  18. private var _slideTime:Number;
  19. private var _currentSlide:Number;
  20. private var _timer:Timer;
  21.  
  22. /*
  23. Author: Chrysto Panayotov ([email protected] for chromeye dev team);
  24. parameters:
  25. @__slides:Number --> number of slides to be cycled;
  26. @_slideTime:Number --> time (in seconds) for the slides to change;
  27. @_autoStart:Boolean ( default true ) --> does the autoStart start automaticly or wait for start();
  28. */
  29.  
  30. public function AutoPilot(__slides:Number, __slideTime:Number, _autoStart:Boolean = true) {
  31. _currentSlide = 0;
  32. _slides = __slides;
  33. _slideTime = __slideTime;
  34.  
  35. _timer = new Timer(_slideTime*1000);
  36. _timer.addEventListener(TimerEvent.TIMER, changeSlide);
  37.  
  38. if(_autoStart){
  39. play();
  40. }
  41. }
  42.  
  43. //plays from 0
  44. public function play():void{
  45. _currentSlide = 0;
  46. _timer.start();
  47. dispatchEvent( new Event(AutoPilot.CHANGE) );
  48. dispatchEvent( new Event(AutoPilot.PLAY) );
  49. }
  50.  
  51. public function stop():void{
  52. _timer.stop();
  53. dispatchEvent( new Event(AutoPilot.STOP) );
  54. }
  55.  
  56. public function resume():void{
  57. _timer.start();
  58. dispatchEvent( new Event(AutoPilot.RESUME) );
  59. }
  60.  
  61.  
  62.  
  63. //private
  64.  
  65. private function changeSlide(event:Event = null):void{
  66. _currentSlide = ( ++_currentSlide % _slides );
  67. dispatchEvent( new Event(AutoPilot.CHANGE) );
  68. }
  69.  
  70. //getters
  71.  
  72. public function get currentSlide():Number{
  73. return _currentSlide;
  74. }
  75.  
  76. public function get nextSlide():Number{
  77. return _currentSlide < _slides-1 ? _currentSlide+1 % _slides : 0;
  78. }
  79.  
  80. public function get previousSlide():Number{
  81. return _currentSlide > 0 ? _currentSlide-1 : _slides-1;
  82. }
  83.  
  84. }//end
  85. }

URL: cycyle, slideshow, banner

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.