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:
-
class com.pixelbreaker.number.LoopRange
-
{
-
public var min:Number;
-
public var max:Number;
-
-
private var _current:Number;
-
-
function LoopRange( min:Number, max:Number, start:Number )
-
{
-
this.min = min;
-
this.max = max;
-
_current = start || min;
-
}
-
-
public function next():Number
-
{
-
_current = _current+1 <= max? _current+1 : min;
-
return _current;
-
}
-
-
public function previous():Number
-
{
-
_current = _current-1>= min? _current-1 : max;
-
return _current;
-
}
-
-
public function set current( value:Number ):Void
-
{
-
if( value>= min && value <= max ) _current = value;
-
else throw new GenericError( 'number ' + value + ' is out of range [ ' + min + ', ' + max + ' ]' );
-
}
-
-
public function get current():Number
-
{
-
return _current;
-
}
-
}
To implement this on an array you would do the following
Actionscript:
-
import com.pixelbreaker.number.LoopRange;
-
-
var slideShowImages:Array = [];
-
for( var i:Number = 0; i<xmlNode.childNodes.length; i++ )
-
{
-
slideShowImages.push( xmlNode.childNodes[ i ].attributes.src );
-
}
-
-
var loopRange:LoopRange = new LoopRange( 0, slideShowImages.length-1 );
-
-
// to load the next image:
-
myMovieClip.loadMovie( slideShowImages[ loopRange.next() ] );
I'll add the AS3 class soon, it's barely any different
10 Comments so far
Leave a reply
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.
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.
no modulo operator?
no, if the 'min' prop is not 0, then modulo isn't really useful without adding more conditionals and fiddly math. No point imo...
Cool function man. FYI, the inline styling on the code makes it unreadable on a white background (i.e. my RSS reader).
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...
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.
thanks...
no modulo operator?
thanks