I've been playing around with the combination of
Spine,
TexturePacker and AppGameKit to create animated sprites.
Having figured out how to use the LoadSubImage and AddSpriteAnimationFrame commands, I've run into an issue when packing sprites more efficiently onto spritesheets using TexturePackers Trim function. More images on the sheet means smoother animation and more efficient use of memory.
When using TexturePackers Trim option, AppGameKit wants to resize the image of the sprite frame, making the sprite expand and contract when played. Heres my code:
rem
rem AGK Application
rem
rem A Wizard Did It!
global iPreviousImageCount as integer
type animtype
iImageID as integer
iImage as integer
sName as string
endtype
spriteA = Setup_Atlas("eggTorchA")
setSpriteposition(spriteA, 20,20)
setSpriteSize(spriteA,16,-1)
PlaySprite( spriteA, 20, 1)
spriteB = Setup_Atlas("eggTorchB")
setSpriteposition(spriteB, 60,20)
setSpriteSize(spriteB,16,-1)
PlaySprite( spriteB, 20, 1)
do
Sync()
loop
function Setup_Atlas(sTexturename as string)
//read all the image names from subimages into an array
sPath as string : sPath = sTexturename + " subimages.txt"
fileid = OpenToRead(sPath)
MaxLines = 0
//find out how many lines are in the subimages file
while FileEOF(fileid) = 0
inc MaxLines, 1
currentline$ = ReadLine(fileid)
endwhile
CloseFile(fileid)
//now ser up an array based to contain the lines data
dim animData[Maxlines] as animtype
fileid = OpenToRead(sPath)
for i = 1 to MaxLines -1 // because last line = EoF
currentline$ = ReadLine(fileid)
string$ = SplitString(currentline$, ".", 0) //get first segment only (before .png)
animData[i].sName = string$
next i
CloseFile(fileid)
inc animcount
dim anim[animcount, MaxLines] as animtype
AtlasIMG = AtlasIMG + iPreviousImageCount
//create the target sprite
Atlas_IMG = LoadImage (sTexturename + ".png")
mySprite = CreateSprite(Atlas_IMG)
//add images from the spritesheet to the sprites animation frames
for i = 1 to MaxLines -1
anim[animcount,i].iImageID = Atlas_IMG + i
anim[animcount,i].sName = animData[i].sName + ".png"
LoadSubImage (anim[animcount,i].iImageID, Atlas_IMG, anim[animcount,i].sName )
AddSpriteAnimationFrame(mySprite,anim[animcount,i].iImageID)
next i
iPreviousImageCount = i
endfunction mySprite
function SplitString(pString as string, pDelimiter as string, pSegment)
delimitCount as integer
myReturn as string
myReturn = ""
charCount as integer
segmentCount as integer
exitSearch as integer
i as integer
pStringLength as integer
pStringLength = len(pString)
for i = 0 to pStringLength - 1
if mid(pString, i + 1, 1) = pDelimiter
delimitCount = delimitCount + 1
endif
next i
if delimitCount = 0 or pSegment < 0 or pSegment > delimitCount
exitfunction pString
endif
if pSegment > 0
for i = 0 to pStringLength - 1
charCount = charCount + 1
if mid(pString, i + 1, 1) = pDelimiter
segmentCount = segmentCount + 1
if segmentCount = pSegment
exit
endif
endif
next i
endif
if charCount >= pStringLength
myReturn = ""
exitfunction myReturn
endif
while exitSearch = 0 and charCount < pStringLength
if mid(pString, charCount + 1, 1) = pDelimiter
exitSearch = 1
else
myReturn = myReturn + mid(pString, charCount + 1, 1)
charCount = charCount + 1
endif
endwhile
endfunction myReturn
SpriteA on the left plays as expected. SpriteB on the right gets bigger and smaller according to the size of the Sub Image used to create it. The goal is to have the sprite just animate, not resize.
I've fiddled are with using SetSpriteAnimation as the docs say
Quote: ""Storing an animation image on an atlas texture is supported.
""
but doesn't say how to achieve this.
My understanding is that loading subimages from a spritesheet is the most efficient way of cramming images into memory, have I missed something?
The spritesheets are attached with the program as a .zip