Corona - create regular display objects from sprit

2019-08-28 23:01发布

问题:

This is normal way of displaying an image:

local img = display.newImage("image.png");

But doesn't it save memory to put all your images in one large image and export from Zwoptex? There is documentation for creating animated sprites from sprite sheets, but what about just pulling a single image from a sprite sheet?

local zwoptexData = require "sheet1"
local data = zwoptexData.getSpriteSheetData()
//then what?

回答1:

The commands to make a static image from a tile sheet look like this:

local tileSheet = sprite.newSpriteSheet("tiles.png", 64, 64)
local tileSet = sprite.newSpriteSet(tileSheet, 1, 10)
local tile = sprite.newSprite(tileSet)
tile.currentFrame = 5

That assumes all the tiles on the sheet are 64x64 but you could easily adapt those commands to use your sprite sheet data. The important things to note are newSprite() and .currentFrame

EDIT: You commented that you can't figure out how to use sprite data with this, so the modified code is

local data = require("tiles.lua")
local tileSheet = sprite.newSpriteSheetFromData("tiles.png", data.getSpriteSheetData())
local tileSet = sprite.newSpriteSet(tileSheet, 1, 3)
local tile = sprite.newSprite(tileSet)
tile.currentFrame = 2

To learn how this works refer to http://developer.anscamobile.com/reference/sprite-sheets