Ultimately it comes down to two commands. One to load the sprite sheet - which extracts all the data fom the XML and one to play the sprite animation:
girl = LoadSpriteSheet("girl.png")
PlaySpriteSheet(girl, GetAnimID(girl, "right"))
Here is the Sprite Sheet code:
type tSpritesheet
spr as integer
xml as integer
qtyAnims as integer
filename as string
name as string
frame as tFrame
anim as tAnim[]
endtype
type tFrame
qty as integer
w as integer
h as integer
endtype
type tAnim
name as string
first as integer
last as integer
loops as integer
speed as float
endtype
global spriteSheet as tSpritesheet[]
/********** LoadSpriteSheet( fn$, ) **************************
Description: Loads a spritesheet and stores it in an array ready to use
Parameters: fn$ - the filename of the spritesheet to load
Returns: the ID (index pointer) within the spritesheet array */
function LoadSpriteSheet( fn$ )
spr as tSpritesheet
spr.name = GetFileName(fn$)
if GetFileExists(spr.name + " spritesheet.xml") = FALSE
Message("LoadSpriteSheet()"+CRLF$+"Called on a file without a spritesheet XML file")
exitfunction -1
endif
img = LoadImage(fn$)
spr.xml = XML_Read(1, spr.name + " spritesheet.xml")
spr.frame.qty = val(XML_GetAttributeValue(spr.xml, "info", "frames", "qty"))
spr.frame.w = val(XML_GetAttributeValue(spr.xml, "info", "frames", "width"))
spr.frame.h = val(XML_GetAttributeValue(spr.xml, "info", "frames", "height"))
spr.qtyAnims = val(XML_GetElementContent(spr.xml, "info", "qtyanims"))
spr.spr = CreateSprite(img)
SetSpriteAnimation(spr.spr, spr.frame.w, spr.frame.h, spr.frame.qty)
if spr.qtyAnims > -1
anim as tAnim
for k = 0 to spr.qtyAnims
anim.name = XML_GetElementContent(spr.xml, "anim" +str(k), "name")
anim.first = val(XML_GetAttributeValue(spr.xml, "anim" +str(k), "frames", "first"))
anim.last = val(XML_GetAttributeValue(spr.xml, "anim" +str(k), "frames", "last"))
anim.speed = val(XML_GetElementContent(spr.xml, "anim" +str(k), "speed"))
anim.loops = XML_GetElementContent(spr.xml, "anim" +str(k), "loop") = "yes"
spr.anim.insert(anim)
next k
endif
spriteSheet.insert(spr)
endfunction spriteSheet.length
/********** GetAnimID( sheetID, name$ ) **************************
Description: Returns the ID of a sprite animation based on its name
Parameters: sheetID - The index in the spritesheet[] array that refers to this animation
name$ - The name of the animation whose ID we are trying to find
Returns: The animation ID stored in the spritesheet[] array */
function GetAnimID(sheetID, name$)
if sheetID<0 or sheetID > spritesheet.length
message("GetAnimID() called"+CRLF$+"with an illegal spritesheet ID")
endif
if spritesheet[sheetID].frame.qty < 0
message("GetAnimID() called"+CRLF$+"on a spritesheet with no defined animations")
endif
for k = 0 to spriteSheet[sheetID].anim.length
if spriteSheet[sheetID].anim[k].name = name$
exitfunction k
endif
next k
message("No animations exist in spritesheet["+str(sheetID)+"] called "+name$)
endfunction -1
/********** PlaySpriteSheet( sheetID, animID ) **************************
Description: Works like AGKs "PlaySprite()" but with a predefined animation within the spritesheet
Parameters: sheetID - The index in the spritesheet[] array that refers to this animation
animID - The specific animation to play
Returns: {nothing} */
function PlaySpriteSheet(sheetID, animID)
PlaySprite(spriteSheet[sheetID].spr, spriteSheet[sheetID].anim[animID].speed, spriteSheet[sheetID].anim[animID].loops, spriteSheet[sheetID].anim[animID].first, spriteSheet[sheetID].anim[animID].last)
endfunction
/********** SetAnimationSpeed( sheetID, animID, speed# ) **************************
Description: Changes the predfined (within the XML) speed that an animation should play
If the animation is currently playing it will not change speed until the
animation is called again.
Parameters: sheetID - The index in the spritesheet[] array that refers to this animation
animID - The specific animation to cahnge
speed# - The new speed to play the animation
Returns: {nothing} */
function SetAnimationSpeed(sheetID, animID, speed#)
if sheetID<0 or sheetID > spritesheet.length
message("SetAnimationSpeed() called"+CRLF$+"with an illegal spritesheet ID")
endif
if spritesheet[sheetID].frame.qty < 0
message("SetAnimationSpeed() called"+CRLF$+"on a spritesheet with no defined animations")
endif
spriteSheet[sheetID].anim[animID].speed = speed#
endfunction
/********** SetAnimationLoops( sheetID, animID, loops ) **************************
Description: Changes the predfined (within the XML) status of whether or not an animation should loop
If the animation is currently set to loop and is currently looping then it will continue
to do so until stopped or the animation is called again.
Parameters: sheetID - The index in the spritesheet[] array that refers to this animation
animID - The specific animation to cahnge
loops - a TRUE/FALSE boolean - should this animation loop?
Returns: {nothing} */
function SetAnimationLoops(sheetID, animID, loops)
if sheetID<0 or sheetID > spritesheet.length
message("SetAnimationLoops() called"+CRLF$+"with an illegal spritesheet ID")
endif
if spritesheet[sheetID].frame.qty < 0
message("SetAnimationLoops() called"+CRLF$+"on a spritesheet with no defined animations")
endif
spriteSheet[sheetID].anim[animID].loops = loops
endfunction
You can
#include that in your project but you will need some of my other files
#included too. The rar file in the post above has everything in a nice stand-alone package. including two sprite sheets to demonstrate the functionality.