Had a go at making a function for this simply because I wanted to see what was possible with the current commands and it wasn't particularly easy. I could have just created each frame as a new image but in order to make it run faster at runtime I auto-created a subimage text file so that the frames could be loaded as sub images. That way you can use a single image and still use PlaySprite as usual. This won't work if you have a subimages text file for the image already in the media folder.
// set window properties
SetWindowSize( 1024, 768, 0 )
// set display properties
SetVirtualResolution( 1024, 768 )
SetOrientationAllowed( 1, 1, 1, 1 )
// load image
img = LoadImage("test.png")
CreateSubimageFile(img, 256, 256, 16)
// create animated sprite
spr = CreateSprite(img)
SetSpriteSize(spr, 256, 256)
SetSpritePositionByOffset(spr, 512, 384)
SetSpriteAnimationFlipFlop( spr, img, 1, 16 )
PlaySprite(spr, 30, 1)
do
Print( ScreenFPS() )
Sync()
loop
function CreateSubimageFile(imageId, frameWidth, frameHeight, frameCount)
// calculate image lines
iw = GetImageWidth(imageId)
ih = GetImageHeight(imageId)
nx = iw/frameWidth
ny = ih/frameHeight
// create file
imageFileName$ = GetImageFilename(imageId)
filename$ = Left(imageFileName$, Len(imageFileName$)-4) + " subimages.txt"
fn = OpenToWrite(filename$)
x = 0
y = 0
for i=1 to frameCount
line$ = str(i) + ":" + str(x) + ":" + str(y) + ":" + str(frameWidth) + ":" + str(frameHeight)
WriteLine(fn, line$)
x = x + frameWidth
if x>=iw
x = 0
y = y + frameHeight
endif
next
CloseFile(fn)
// reload file
DeleteImage(imageId)
LoadImage(imageId, imageFileName$)
endfunction
function SetSpriteAnimationFlipFlop( spriteId, imageId, frameStart, frameEnd )
// load subimages
imgs as integer[]
cnt = 1
for i=frameStart to frameEnd
imgs.insert(LoadSubImage(imageId, str(i)))
inc cnt
next
for i=frameEnd-1 to frameStart+1 step -1
imgs.insert(LoadSubImage(imageId, str(i)))
inc cnt
next
// set animation frames
for i=0 to imgs.length
AddSpriteAnimationFrame(spriteId, imgs[i])
next
SetSpriteFrame(spriteId, 1)
endfunction
EDIT: Scraggles method is fine but requires some additional management to set the animation in reverse once it has run one way and to then reverse it. It might be just as easy to create a small management function that looks after flip-flop animations. Also rather than create the subimages text file every time I would probably check if the file exists first... I'll add a check in for this now.
EDIT2: Separated out the loading / subimage file creation. Much neater and quicker now.
Using AppGameKit V2 Tier 1