开发者

Corona - how to make sprite sheets compatible with dynamic image resolution

Corona has a method for creating images that will be displayed dynamically based on device resolution:

img = display.newImageRect("image.png", 100, 100)

Great, but what if all your images are in a sprite sheet, which is recommended for performance? Then you have to do something like this to display the image:

local data = require("sheet1")
local tileSh开发者_Python百科eet = sprite.newSpriteSheetFromData("sheet1.png", data.getSpriteSheetData())
local tileSet = sprite.newSpriteSet(tileSheet, 1, 3)
local img = sprite.newSprite(tileSet)
img.currentFrame = 1

How do you create dynamically sized images from sprite sheets?


use display.contentScaleX http://developer.anscamobile.com/reference/index/displaycontentscalex

here's how http://developer.anscamobile.com/forum/2010/12/08/dynamic-retina-spritesheets-heres-how


Here's how I resized my background animation. It consisted of 2 frames, each 794 x 446. It needed to be full-screen, landscape mode. Refer to step 6 below.

--> Initialize background animations

-- 1. Data = define size and frames
local bgData = { width=794, height=446, numFrames=2, sheetContentWidth=1588, sheetContentHeight=446 }
-- 2. Sheet = define the sprite sheet
local bgSheet = graphics.newImageSheet( "hkc.png", bgData )
-- 3. Animation = define animation characteristics
local bgAnim = {
    { name = "bgAnimation", start = 1, count = 2, time = 800, loopCount = 0, -- repeat forever
      loopDirection = "forward"
    }
}
-- 4. Display the sprite (can't set size here unlike display.newImageRect)
local bg = display.newSprite( bgSheet, bgAnim )
-- 5. Center the animation
bg:setReferencePoint( display.CenterReferencePoint )
bg.x = display.contentCenterX
bg.y = display.contentCenterY
-- 6. Resize to match screen size based on a ratio with the actual background pixel dimensions
bg:scale( display.contentWidth / 794, display.contentHeight / 446 )
-- 7. Play the animation
bg:play()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜