Corona - 如何使精灵表与动态图像分辨率兼容

发布于 2024-11-01 14:32:15 字数 501 浏览 1 评论 0原文

Corona 有一种创建图像的方法,该图像将根据设备分辨率动态显示:

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

很好,但是如果所有图像都在 Sprite 表中(为了性能而推荐),该怎么办?然后你必须做这样的事情来显示图像:

local data = require("sheet1")
local tileSheet = sprite.newSpriteSheetFromData("sheet1.png", data.getSpriteSheetData())
local tileSet = sprite.newSpriteSet(tileSheet, 1, 3)
local img = sprite.newSprite(tileSet)
img.currentFrame = 1

How do you createdynamicsizedimagesfromspritesheets?

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 tileSheet = 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

把梦留给海 2024-11-08 14:32:15

这是我调整背景动画大小的方法。它由 2 个帧组成,每个帧为 794 x 446。它需要是全屏、横向模式。请参阅下面的步骤 6。

--> 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()

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文