关闭

quick-3.3学习笔记之状态机及骨骼动画的使用

60人阅读 评论(0) 收藏 举报

此博客仅作为自己的学习总结使用

function MainScene:ctor()

   local manager = ccs.ArmatureDataManager:getInstance()--获取骨骼动画管理器单例
   manager:addArmatureFileInfo("Hero/wuzhe.ExportJson")--添加文件到缓存里面
   local armature = ccs.Armature:create("wuzhe")
    armature:getAnimation():playWithIndex(0)
    armature:setPosition(240, 160);
     self:addChild(armature)
end


--状态机的实现

local Player = class("Player",function()
        return display.newSprite("#player1-1-1.png")
    end)

function Player:ctor()
    local StateMachine = require("framework.cc.components.behavior.StateMachine")
    self.fsm =  StateMachine.new() --创建状态机
    --from 可以由那几种状态   --to
    self.fsm:setupState({
        events = {
            {name = "walkAction", from = {"none","attackA","attackB"},   to = "walk" },
            {name = "hurtAction", from = "walk",  to = "hurt" },
            {name = "deadAction", from = "hurt", to = "dead" },
            {name = "attackAAction",  from = "walk",    to = "attackA"},
            {name = "attackBAction", from = "walk",    to = "attackB" },
     --none表示初始的状态,固定的
        },

--onbefore /on /onleave +状态,onbefore表示该状态激活前的回调函数,on表示正在激活的时候的回调函数

--onleave表示离开该状态的回调函数
        callbacks = {
            onbeforewalkAction = function(event) self:stopAllActions(); end,--执行该动作前,停止动作
            onwalkAction = function(event) self:walk() end,--执行的帧动画
            onleavewalkAction = function(event)  end,

            onbeforehurtAction = function(event) self:stopAllActions(); end,
            onhurtAction = function(event)
                    self:hurt();
                    self:performWithDelay(function() self.fsm:doEvent("deadAction") end, 1.0); --延迟函数
                end,
            onleavehurtAction =  function(event)  end,

            onbeforedeadAction = function(event) self:stopAllActions(); end,
            ondeadAction = function(event)self:dead() end,
            onleavedeadAction = function(event) end,

            onbeforeattackAAction = function(event) self:stopAllActions(); end,
            onattackAAction = function(event) self:attackA() end,
            onleaveattackAAction = function(event) end,

            onbeforeattackBAction = function(event)  self:stopAllActions(); end,
            onattackBAction = function(event) self:attackB() end,
            onleaveattackBAction = function(event) end,
        },
    })

end
function Player:walk()

--帧动画的实现,第一个参数表示图片命名格式,第二个参数表示起始的图片,第三个表示最后一张

     local frames = display.newFrames("player1-1-%i.png", 1, 4)--行走
    local animation = display.newAnimation(frames, 0.5 / 4) --创建动画
    self:playAnimationForever(animation)--该动画永久执行
end
function Player:hurt()
    local frames = display.newFrames("player1-4-%i.png", 1, 2)--被攻击
    local animation = display.newAnimation(frames, 0.5 / 2)
    self:playAnimationOnce(animation)
end
function Player:attackA()
    local frames = display.newFrames("player1-2-%i.png", 1, 4)--劈刺
    local animation = display.newAnimation(frames, 0.5 / 4)
    self:playAnimationForever(animation)
end
function Player:attackB()
    local frames = display.newFrames("player1-5-%i.png", 1, 4)--直刺
    local animation = display.newAnimation(frames, 0.5 / 4)
    self:playAnimationForever(animation)
end

function Player:dead()
 local frames = display.newFrames("player1-3-%i.png", 1, 4)--死亡
    local animation = display.newAnimation(frames, 0.5 / 4)
    self:playAnimationOnce(animation)
end

return Player



--以下是测试函数

local MainScene = class("MainScene", function()
    return display.newScene("MainScene")
end)

function MainScene:ctor()
    local Player = require("app.scenes.Player")
    local player = Player:new();
    player:setPosition(cc.p(480,320));
    self:addChild(player);
    player.fsm:doEvent("walkAction")--执行状态机的动作

   --1
    cc.ui.UIPushButton.new()
            :setButtonLabel(cc.ui.UILabel.new({text = "walk", size = 32, color = display.COLOR_WHITE}))
            :onButtonClicked(function()
                print("1");
                if player.fsm:canDoEvent("walkAction") then--判断是否可以执行该状态
                    print("1")
                    player.fsm:doEvent("walkAction")
                end
            end)
            :align(display.CENTER, 50, 100)
            :addTo(self)
    --2
    cc.ui.UIPushButton.new()
            :setButtonLabel(cc.ui.UILabel.new({text = "hurt", size = 32, color = display.COLOR_WHITE}))
            :onButtonClicked(function()
                print("2");
                if player.fsm:canDoEvent("hurtAction") then
                    player.fsm:doEvent("hurtAction")
                end
            end)
            :align(display.CENTER, 150, 100)
            :addTo(self)

    --3
            cc.ui.UIPushButton.new()
            :setButtonLabel(cc.ui.UILabel.new({text = "dead", size = 32, color = display.COLOR_WHITE}))
            :onButtonClicked(function()
                print("3");
                if player.fsm:canDoEvent("deadAction") then
                    player.fsm:doEvent("deadAction")
                end
            end)
            :align(display.CENTER, 250, 100)
            :addTo(self)
    --4
            cc.ui.UIPushButton.new()
            :setButtonLabel(cc.ui.UILabel.new({text = "attackA", size = 32, color = display.COLOR_WHITE}))
            :onButtonClicked(function()
                print("4");
                if player.fsm:canDoEvent("attackAAction") then
                    player.fsm:doEvent("attackAAction")
                end
            end)
            :align(display.CENTER, 350, 100)
            :addTo(self)

     --5
            cc.ui.UIPushButton.new()
            :setButtonLabel(cc.ui.UILabel.new({text = "attackb", size = 32, color = display.COLOR_WHITE}))
            :onButtonClicked(function()
                print("5");
                if player.fsm:canDoEvent("attackBAction") then
                    player.fsm:doEvent("attackBAction")
                end
            end)
            :align(display.CENTER, 450, 100)
            :addTo(self)
    

end

function MainScene:onEnter()
end

function MainScene:onExit()
end

return MainScene

1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:61次
    • 积分:11
    • 等级:
    • 排名:千里之外
    • 原创:1篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章存档