Adjustment for New Sprite API's SpriteSheet Object

Zwoptex still uses the old sprite method and also uses the deprecated module function. This format updates the program’s output as well as provides support for dynamic resolution. The new format uses the complex example found in http://www.coronalabs.com/blog/2012/03/06/image-sheets-image-groups-and-sprites/

coronasdk.coordinatesformat.txt

Put this in the file: coronasdk.coordinatesformat.txt inside Zwoptex application contents: Contents/Resources folder

local M = {}  
  
local getSheetOptions = function()  
 local options = {  
 frames = {  
 {% for sprite in spritesAndAliases %}  
 {  
 x = {{ sprite.textureRectX }},  
 y = {{ sprite.textureRectY }},  
 width = {{ sprite.textureRectWidth }},  
 height = {{ sprite.textureRectHeight }}  
 },  
 {% /for %}  
 },  
  
 sheetContentWidth = {{ metadata.sizeWidth }},  
 sheetContentHeight = {{ metadata.sizeHeight }}  
 }  
  
 return options  
end  
M.getSheetOptions = getSheetOptions  
  
return M  

Zwoptex Settings

Dynamic resolution requires each resolution of the sprite outputted by Zwoptex to have the exact same layout (e.g. if frame 1 is on the top left of the sprite png in the x1 sprite image, is should still be in the top left for @2x, and @4x). Use these settings to make your output images consistent:

  • _ Make sure all images are not trimmed _

  • Algorithm: Basic

  • Sort On: Name

  • Sort Order: Ascending

  • Sprite Spacing: 0px

Usage:

[lua]local options = require(“mysprite”).getSheetOptions()
local imageSheet = graphics.newImageSheet( “mysprite.png”, options )[/lua] [import]uid: 144908 topic_id: 28422 reply_id: 328422[/import]

If it is useful to anyone, I modified this code so that it keeps the offset from the original PNGs (for example, if you make a character “jump” inside the PNG canvas, you would need the code below to preserve the “jump” when imported into Corona).


local M = {}

local getSheetOptions = function()

    local options = {

        frames = {

        {% for sprite in spritesAndAliases %}

            {

                x = {{ sprite.textureRectX }},

                y = {{ sprite.textureRectY }},

                width = {{ sprite.textureRectWidth }},

                height = {{ sprite.textureRectHeight }},

                sourceX = ({{ sprite.sourceSizeWidth }} - {{ sprite.textureRectWidth }})/2+({{ sprite.offsetX }}),

                sourceY = ({{ sprite.sourceSizeHeight }} - {{ sprite.textureRectHeight }})/2-({{ sprite.offsetY }}),

                sourceWidth = {{ sprite.sourceSizeWidth }}, 

                sourceHeight = {{ sprite.sourceSizeHeight }}

            },

        {% /for %}

        },

        

        sheetContentWidth = {{ metadata.sizeWidth }},

        sheetContentHeight = {{ metadata.sizeHeight }}

    }

    return options

end

M.getSheetOptions = getSheetOptions

return M


Maybe there is a cleaner way of doing this, but I don’t know the Zwoptex scripting well enough.

Jorge

If it is useful to anyone, I modified this code so that it keeps the offset from the original PNGs (for example, if you make a character “jump” inside the PNG canvas, you would need the code below to preserve the “jump” when imported into Corona).


local M = {}

local getSheetOptions = function()

    local options = {

        frames = {

        {% for sprite in spritesAndAliases %}

            {

                x = {{ sprite.textureRectX }},

                y = {{ sprite.textureRectY }},

                width = {{ sprite.textureRectWidth }},

                height = {{ sprite.textureRectHeight }},

                sourceX = ({{ sprite.sourceSizeWidth }} - {{ sprite.textureRectWidth }})/2+({{ sprite.offsetX }}),

                sourceY = ({{ sprite.sourceSizeHeight }} - {{ sprite.textureRectHeight }})/2-({{ sprite.offsetY }}),

                sourceWidth = {{ sprite.sourceSizeWidth }}, 

                sourceHeight = {{ sprite.sourceSizeHeight }}

            },

        {% /for %}

        },

        

        sheetContentWidth = {{ metadata.sizeWidth }},

        sheetContentHeight = {{ metadata.sizeHeight }}

    }

    return options

end

M.getSheetOptions = getSheetOptions

return M


Maybe there is a cleaner way of doing this, but I don’t know the Zwoptex scripting well enough.

Jorge