Corona - create regular display objects from sprite sheets?

戏子无情 提交于 2019-12-11 18:18:12

问题


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



来源:https://stackoverflow.com/questions/5655055/corona-create-regular-display-objects-from-sprite-sheets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!