加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 16:48:23 所属栏目:百科 来源:网络整理
导读:---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.re

---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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读