Looping within a range

Something I have to do very often is looping a number within a range. This could be for a carousel navigation, a rotating banner or image slideshow, etc. The concept is simple, have a range, say 1 - 20, and simply call next() / previous() on it and return the new number, if it goes above 20, return to 1, if below 1, return to 20.

The AS2.0 class is as follows

Actionscript:
  1. class com.pixelbreaker.number.LoopRange
  2. {
  3.     public var min:Number;
  4.     public var max:Number;
  5.    
  6.     private var _current:Number;
  7.    
  8.     function LoopRange( min:Number, max:Number, start:Number )
  9.     {
  10.         this.min = min;
  11.         this.max = max;
  12.         _current = start || min;
  13.     }
  14.    
  15.     public function next():Number
  16.     {
  17.         _current = _current+1 <= max? _current+1 : min;
  18.         return _current;   
  19.     }
  20.    
  21.     public function previous():Number
  22.     {
  23.         _current = _current-1>= min? _current-1 : max;
  24.         return _current;   
  25.     }
  26.    
  27.     public function set current( value:Number ):Void
  28.     {
  29.         if( value>= min && value <= max ) _current = value;
  30.         else throw new GenericError( 'number ' + value + ' is out of range [ ' + min + ', ' + max + ' ]' );
  31.     }
  32.    
  33.     public function get current():Number
  34.     {
  35.         return _current;   
  36.     }
  37. }

To implement this on an array you would do the following

Actionscript:
  1. import com.pixelbreaker.number.LoopRange;
  2.  
  3. var slideShowImages:Array = [];
  4. for( var i:Number = 0; i<xmlNode.childNodes.length; i++ )
  5. {
  6.     slideShowImages.push( xmlNode.childNodes[ i ].attributes.src );
  7. }
  8.  
  9. var loopRange:LoopRange = new LoopRange( 0, slideShowImages.length-1 );
  10.  
  11. // to load the next image:
  12. myMovieClip.loadMovie( slideShowImages[ loopRange.next() ] );

I'll add the AS3 class soon, it's barely any different

10 Comments so far

  1. Si / July 26th, 2007 9:13 pm

    I love geeky little functions like this. Looks similar to Iterator-style classes from Other languages, which often lead onto Bindings and so on. So clever things like this can then be hidden as glue code. Do you plan to work with bindings at all? In that case I guess you'd bind the data source (the xml or whatever) with the slideshow and it would automatically read the data. You could also bind slideshow to an array or any other data source that fits the interface/protocol. I've never really done it in Flash though... Cocoa has good implementations of these things which are worth a look.

  2. gabriel / July 26th, 2007 10:46 pm

    yeah, it is similar to Iterators. I could build it up to some kind of binding system, I'll probably do that sort of stuff more on future projects. With XML it's pretty easy do something similar to thing using nextSibling/previousSibling etc.

    One thing I've been using a lot more are dataProviders, they are common as muck in Flex apps, so started using them a lot more there.

  3. k / July 27th, 2007 8:55 am

    no modulo operator?

  4. gabriel / July 27th, 2007 1:47 pm

    no, if the 'min' prop is not 0, then modulo isn't really useful without adding more conditionals and fiddly math. No point imo...

  5. meekish / July 27th, 2007 4:54 pm

    Cool function man. FYI, the inline styling on the code makes it unreadable on a white background (i.e. my RSS reader).

  6. gabriel / July 27th, 2007 5:22 pm

    hey, yeah, I noticed that when I opened NetNewsWire the other day, I'll have a look at stopping code from being 'styled' when it's sent as an RSS feed...

  7. Darren / August 24th, 2007 6:49 am

    I think you just need to set a background color for the styled code sections so that the lighter colors don't get lost when the background is white.

  8. youtube, rize / December 18th, 2007 4:44 pm

    thanks...

  9. resimler / March 29th, 2008 8:04 pm

    no modulo operator?

  10. bilgi yarışması / June 12th, 2008 12:43 pm

    thanks

Leave a reply

*
To prove that you're not a bot, enter this code
Anti-Spam Image