您的位置:首页 > 移动开发 > Cocos引擎

[cocos2dx-lua]Cocos2dx-Lua中Sprite精灵的3种创建方法

2015-12-24 19:10 465 查看
---1.从图片文件创建
--适合于要显示的这张图片的全部区域或部分区域
function TestTest:CreateSprite1()
local png = "lobby/lobby.png" --文件路径
local sprite = cc.Sprite:create(png)
self:addChild(sprite)

local sprite2 = cc.Sprite:create(png,cc.rect(0,0,100,100))
self:addChild(sprite2)
sprite2:setPosition(display.width/2,display.height/2)
end

---2.从SpriteFrame对象创建
function TestTest:CreateSprite2()
local resPath = "shared/shared_ui.pvr.ccz"
local plist = "shared/shared_ui.plist"
display.addSpriteFrames(plist,resPath)--载入图像到帧缓存

local spriteFrame = display.newSpriteFrame("shouye_shouye_n.png")
local sprite0 = cc.Sprite:createWithSpriteFrame(spriteFrame)
sprite0:setPosition(display.width/2,display.height/2+200)
self:addChild(sprite0)

local sprite = cc.Sprite:createWithSpriteFrameName("shouye_shouye_s.png")
sprite:setPosition(display.width/2,display.height/2+400)
self:addChild(sprite)

local fullPath = cc.FileUtils:getInstance():fullPathForFilename(plist)
local dict = cc.FileUtils:getInstance():getValueMapFromFile(fullPath)
for imgName,v in pairs(dict.frames) do
print(imgName,v)
end
end

---3.从缓存纹理创建
function TestTest:CreateSprite3()
local resPath = "game/game_ui.pvr.ccz"
local textureCache = cc.Director:getInstance():getTextureCache()
local pTexture = textureCache:addImage(resPath)
--上面两行= local pTexture = display.loadImage(resPath)
local sprite = cc.Sprite:createWithTexture(pTexture)
sprite:setPosition(display.width/2+400,display.height/2+400)
self:addChild(sprite)
end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: