I have been tinkering around with my code and I believe I found a crude answer, but still not automatic as it should be (See the NOTE: bit below for pseudo code that gets around my problem).
Furthermore, it is counter-intuitive to the workflow and code that corona supplies, so I shall attempt to describe it here.
Corona documents suggest that you do the following:
- Create a sprite sheet from an image (newSpriteSheet)
- Split the sprite sheet into thematically related sprite sets. (newSpriteSet)
- Create animations based on the sprite sets. (sprite.add)
- Create sprites and change their animations as you see fit (newSprite, prepare, play, stop etc.)
To have proper flexibility, I have to do the following:
- Create mutliple sprite sheets from an image, one per animation (newSpriteSheetFromData()
- Assign a single default sprite set (newSpriteSet)
- Create a single animation (sprite.add)
- Create a sprite (newSprite), but to change animations you must delete it and create a new one since each animation now uses its own sprite sheet (despite sharing an image).
The key to getting repeated frames and frames out of sequence is to set up the SPRITE SHEET using newSpriteSheetFromData() instead of newSpriteSheet, as it allows you to chop up the sprite image into as many frames as you want, in any order. One assumes it does not actually split up the image itself, merely storing a set of appropriate UV coordinates (saving on texture memory)
NOTE: Thinking about what I just wrote, it would be possible to avoid the problem of the final step in my list above, but it does mean you can’t dynamically add animations.
This is not really a problem though, but it means a change in the order of the work, IE normally you’d define the sprite sheet, then the sets then the animations, but here you need to specify the animations before the sprite sheet.
The theory is that you would create a super table that contains all the animation frames appended. Since you can repeat frames, you then just need a way of tracking the information.
Pseudo code using my animation examples above might be something similar to (ignoring things like loop and duration for simplicity of viewing):
[code]masterFrameList = {}
anims = {}
defineAnim( “walk”, {1, 2, 3, 4, 5, 6})
defineAnim( “run”, {1, 3, 5})
defineAnim( “stutter”, {1, 2, 3, 3, 3, 3, 4})
sheet, set = createSpriteAndAnimations( anims, masterFrameList)
function defineAnim( animName, frames)
firstFrame = #masterAnim + 1
totalFrames = #frames
anims[animName] = [firstFrame, totalFrames]
table.append(masterFrameList, anims)
end
function createSpriteSheetAndAnims( anims, masterFrameList )
– Build up sprite sheet frame data from the masterFrameList, however you deem appropriate)
framesData = {}…
spriteSheet = sprite.newSpriteSheetFromData(framesData)
– Create single sprite set
spriteSet = sprite.newSpriteSet(spriteSheet, 1, #framesData)
– Create the animations
for k,v in pairs(anims) do
sprite.add( spriteSet, k, v[1], v[2], duration, loop)
end
– Return the data
return spriteSheet, spriteSet
end[/code]
As you can see it isn’t that tough to do, but flies in the face of the sheet->set->anims logic that corona implements.
So, for now, I believe I can do what I want, and that at least makes me happy, but it clearly isn’t intuitive, and will require a fair amount of code, and assuredly it comes with a memory overhead (how much remains to be seem, hopefully negligible).
Barry [import]uid: 46639 topic_id: 18342 reply_id: 70297[/import]