Quick-Cocos2d-x 社区版 spine 新增 Lua 接口绑定setAttachment、findBone、findSlot

These features will coming in next stable release: Quick-Cocos2d-x 3.6.2 Release

spine 换装接口 setAttachment

首先你在制作骨骼的时候,需要给一个 slot 设置多个 attachment,spine在一个时刻只会显示其中的一个 attachment,动态切换 attachment 调用 SkeletonRenderer::setAttachment接口。这个接口自动绑定并未提供,社区版手动绑定提供给开发者使用。

注:只有一个参数时,表示去掉slot的Attachment,不显示图片。

接口示例:

local hero = sp.SkeletonAnimation:create("build_yellowlightfinished.json", "build_yellowlightfinished.atlas")
hero:setAttachment("changegun", "pc_gungirl_crossbow3")

获取 bone 信息接口 findBone

spine的runtime还提供了 findBone 与 findSlot这样的接口,可获取内部动画播放某一时刻的信息,这些信息是可读、不建议写入的信息。社区版手动绑定为 table 提供给开发者,这样可有效避免内存管理混乱的问题。
目前只暴露了部分有用的信息。

接口示例:

dump(hero:findBone("changegun"))

output:

[LUA-print] - "<var>" = {
[LUA-print] -     "worldFlipX"    = false
[LUA-print] -     "worldFlipY"    = false
[LUA-print] -     "worldRotation" = 0
[LUA-print] -     "worldScaleX"   = 1
[LUA-print] -     "worldScaleY"   = 1
[LUA-print] -     "worldX"        = -3.9000000953674
[LUA-print] -     "worldY"        = 8.4882717132568
[LUA-print] - }

获取 slot 信息接口 findSlot

接口示例:

dump(hero:findSlot("changegun"))

output:

[LUA-print] - "<var>" = {
[LUA-print] -     "attachmentHeight" = 172
[LUA-print] -     "attachmentWidth"  = 139
[LUA-print] - }

完整测试代码

function MainScene:ctor()
    cachedData = sp.SkeletonData:create("build_yellowlightfinished.json",
                "build_yellowlightfinished.atlas")
    local spineAnimation = sp.SkeletonAnimation:create(cachedData)

    local hero = spineAnimation:pos(display.width / 2, display.height / 2)
        :addTo(self)
    hero:setAnimation(0, "animation", true)
    -- button
    local images = {
        normal = "ButtonPressed.png",
        pressed = "ButtonPressed.png",
        disabled = "ButtonPressed.png",
    }

    cc.ui.UIPushButton.new(images, {scale9 = true})
        :setButtonSize(200, 60) --设置大小
        :onButtonClicked(function(event) -- 按钮的clicked事件处理
            hero:setAttachment("changegun", "pc_gungirl_crossbow3")
            dump(hero:findBone("changegun"))
            dump(hero:findSlot("changegun"))
        end)
        :align(display.CENTER, display.width - 80, 80)
        :addTo(self)
end

资源文件:setAttachment_res.zip.zip

标签: Lua

添加新评论