The first two look good; assuming you have a fixed number of columns and that the tileset can then extend "infinately" vertically, and the tiles are given frame indices like:
0 1 2 3 4
5 6 7 8 9
...
If you index your tiles starting from 1... don't : start using 0 as first index, the math will be alot cleaner.. But, if you must use 1-based indexing; subtract 1 from the current frame before working with it in calculations..
One thing I would do; though:
left = (current frame % number of columns) * sprite width
top =<strong> int(</strong>current frame / number of columns<strong>) </strong>* sprite height
( where <strong>int </strong>is whatever the function is in your language to truncate a decimal to an integer - i.e. round by removing the fractional part )
If you're having problems understanding why it works: http://en.wikipedia.org/wiki/Modular_arithmetic
MattEvans
Veteran Poster
Moderator
1,386 posts since Jul 2006
Reputation Points: 522
Solved Threads: 64