Traverse Class - Useful Utility
Tue Dec 29 05:49:39 2009
How often have you had to make a photo gallery or something else that requires an index to be traversed? I know that I have come across this a lot and quite honestly I am tired I rewriting the same code over and over again. Thus, I've finally forced myself to write a class that will traverse an index and easily allow the assignment of objects that control the incrementation/decremental of said index... and I just thought I would share it with whoever is interested.
It's actually more of a helper/convenience class than anything. But I know I will personally be using it a lot in the future. The example below shows two arrows linked to an index displayed in a textfield. Click the arrows to see what I mean.
What It Does
Code Example
import Traverser; Traverser.init(MovieClip, "inc/dec", limit, callback);
How to Use This Class
Simply import the class as in the code example and then initiate the class using the Traverser.init()
call. This class requires no instantiation. The first parameter labelled MovieClip
should received the MovieClip that is to act as a button, like the two arrows in the swf example above.
The second parameter is a String
where "inc" makes the MovieClip passed in the first parameter increment the index of the Traverser class, and as you would imagine, "dec" makes the MovieClip passed decrement the index.
The limit
parameter determines the maximum limit the index will increment to before the index returns to zero. The limit
of swf example is 5, and therefore the textfield will never display a number higher than 5.
Finally, the dispatchEvent(new Event())
calling a public static const
, although all it really does is calls the function passed from inside the Traverser class. I thought this would work better than the alternative as I am using the Singleton approach and this is because I didn't feel the need to instantiate for as little as an index number, which is essentially what we are dealing with. Anyway, in the swf example above, I am using the callback function to update the TextField and give it a TextFormat. Without the callback to update the TextField, the TextField would not display any changes to the index.
Also, the index is contained inside a public
variable and so can be access via Traverser.index
to pull the current value. Again I didn't feel the need for getters and setters in this case, but if that's what you would prefer, feel free to update the code.
Download the files from the link below for a closer look at an example and for the code behind the arrow buttons.
Download the Traverse Class HereHappy traversing!
Tweet