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

前端之家收集整理的这篇文章主要介绍了[cocos2dx-lua]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,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.f2er.com/cocos2dx/340524.html

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