Medium/Hard level of tutorial. Not beginner stuff.
Required file :
http://dl.dropbox.com/u/8822351/earth.PNGPart 1 :
'// Sprite sheet tutorial.
'// This demo requires QB64 and Earth.png - It is medium/advanced and not aimed at the begineer.
'//
'// Sprite sheets are used for simple animation of sprites. Learning how to use sprite sheets is an essential
'// part of programming games.
'// This demo covers how to use both equally and non equally spaced sprites sheet using a rectangle method.
'//
'// Create a graphics screen.
SCREEN _NEWIMAGE(600, 400, 32)
'//
'// First create a type array to hold all the rectangle's variables.
'//
TYPE Rectangle
X AS INTEGER
Y AS INTEGER
Width AS INTEGER
Height AS INTEGER
END TYPE
'//
'// Load the sprite sheet image into memory
'//
Sprite& = _LOADIMAGE("Earth.png")
'//
'// The sprite we are using has 12 frames, it has 4 frames on the X axis and 3 on the Y axis.
'//
'// So the easiest way to loop through a set of rectangles is to create an array of them.
DIM SpriteImageRect(12) AS Rectangle
'//
'// As we are dealing with an equally spaced sprite sheet we can load all the x/y points in a set of for loops.
'//
FOR j% = 0 TO _HEIGHT(Sprite&) - (_HEIGHT(Sprite&) / 3) STEP (_HEIGHT(Sprite&) / 3)
FOR i% = 0 TO _WIDTH(Sprite&) - (_WIDTH(Sprite&) / 4) STEP (_WIDTH(Sprite&) / 4)
SpriteImageRect(rectcnt%).X = i%
SpriteImageRect(rectcnt%).Y = j%
SpriteImageRect(rectcnt%).Width = _WIDTH(Sprite&) / 4
SpriteImageRect(rectcnt%).Height = _HEIGHT(Sprite&) / 3
rectcnt% = rectcnt% + 1
NEXT
NEXT
'//
'// Draw the images in turn.
FOR i% = 0 TO 11
_PUTIMAGE (0, 0), Sprite&, , (SpriteImageRect(i%).X, SpriteImageRect(i%).Y)-(SpriteImageRect(i%).X + SpriteImageRect(i%).Width, SpriteImageRect(i%).Y + SpriteImageRect(i%).Height)
_DISPLAY
_DELAY 1
NEXT
'// It's not pretty code at the moment, but we will clean that up in part two and make it usabel for more tha jsut this one sprite.