Cocos2dx-Lua中Sprite精灵的3种创建方法

---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
原文地址:https://www.cnblogs.com/aibox222/p/8695889.html