cocos2dx 通过plist文件 创建精灵

前端之家收集整理的这篇文章主要介绍了cocos2dx 通过plist文件 创建精灵前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

cocos2dx 版本3.13

很多时候我们都是通过将一些小图片合成一张大图片,导出plist文件和png文件

Sprite可以通过纹理创建,记录一下:

local dict = cc.FileUtils:getInstance():getValueMapFromFile("people/animal.plist")

local framesMap = dict["frames"]

local img_map = {}
for key,value in pairs(framesMap) do
for kk,vv in pairs(value) do
if kk == "frame" then
local i = string.find(vv,",")
local x = tonumber(string.sub(vv,3,i-1))


local j = string.find(vv,i+1)
local y = tonumber(string.sub(vv,i+1,j-2))


local ii = string.find(vv,j+1)
local width = tonumber(string.sub(vv,j+2,ii-1))


local jj = string.find(vv,"}",ii+1)
local height = tonumber(string.sub(vv,ii+1,jj-1))
img_map[key] = {x,y,width,height}
end
end
end

local texture = cc.Director:getInstance():getTextureCache():addImage("people/animal.png")

local info = img_map["run_3_6.png"]
local rect = cc.rect(info[1],info[2],info[3],info[4])


local run_sprite = cc.Sprite:createWithTexture(texture,rect) run_sprite:setLocalZOrder(300) run_sprite:setPosition(1200,-30) father:addChild(run_sprite)

猜你在找的Cocos2d-x相关文章