cocos2d-x第二个示例ActionManagerTest注释

来源:互联网 发布:js获取数组中的值 编辑:程序博客网 时间:2024/05/22 01:03
local    kTagNode = 0
local    kTagGrossini = 1
local    kTagSequence = 2
local    scheduler = cc.Director:getInstance():getScheduler()
--------------------------------------------------------------------
--
-- Test1
--
--------------------------------------------------------------------
--示例中的第一个 Test 1. Should not crash 
local function CrashTest()
    local ret = createTestLayer("Test 1. Should not crash")
    --创建一个精灵
    local  child = cc.Sprite:create(s_pPathGrossini)
    --设置位置
    child:setPosition( 200,200 )
    --将精灵child添加到图层ret中
    ret:addChild(child, 1)


    --runAction():执行一个动作并返回该动作;RotateBy:顺时针旋转一个节点对象;
    --将child旋转90度并持续1.5秒
    child:runAction(cc.RotateBy:create(1.5, 90))
    --Sequence:create()按顺序运行内部动作  DalayTime:动作延迟  FadeOut:改变透明度使动作淡出
    --顺序运行两个内部动作;第一个推迟1.4秒创建  第二个1.1秒内淡出
    child:runAction(cc.Sequence:create(cc.DelayTime:create(1.4),cc.FadeOut:create(1.1)))
    cclog("推迟".."     ".."淡出")--使用cclog可以在控制台输出数据 进行验证


    local function removeThis()
        --getParent返回父指针  removechild():从容器中删除子节点
        --删除ret图层
        ret:getParent():removeChild(ret, true)
        --Helper是在helper.lua中定义的表  
        Helper.nextAction()
    end
    --cclog("验证")
    --After 1.5 second, self will be removed.
    --旋转1.5秒后自己被移除  然后执行下一个动作  logicTest  CallFunc:一个需要被执行的函数  在此处是要执行removeThis函数
    ret:runAction( cc.Sequence:create(cc.DelayTime:create(1.4),cc.CallFunc:create(removeThis)))
    return ret
end




--------------------------------------------------------------------
--
-- LogicTest
--
--------------------------------------------------------------------
--第二个  logicTest
local function LogicTest()
    local ret = createTestLayer("Logic test")
    --加载精灵   s_pPathGrossini  是图的路径  在testResource中有定义
    local  grossini = cc.Sprite:create(s_pPathGrossini)
    --将精灵加载到ret图层中  第二个参数是优先级  第三个参数是成员标识可以通过这个参数访问该节点
    ret:addChild(grossini, 0, 2)
    grossini:setPosition(200,200)
    local function bugMe(node)
        --停止并删除活动列表中的所有动作   如果没有此代码  所有动作都会在运行  
        node:stopAllActions() --After this stop next action not working, if remove this stop everything is working
        --用持续时间和x,y轴相同的缩放比例创建ScaleTo动作   第一个参数是时间  第二个参数是xy的缩放比例
        --ScaleTo是通过修改scale属性使得节点缩放到特定大小  
        --缩放到原来的两倍
        node:runAction(cc.ScaleTo:create(2, 2))
    end
    --MoveBy:create()实现位移  第一个参数是时间  第二个参数是位移 此处是Vec2类型  
    grossini:runAction( cc.Sequence:create(cc.MoveBy:create(2, cc.p(150,0)) ,cc.CallFunc:create(bugMe)))
    return ret
end


--------------------------------------------------------------------
--
-- PauseTest
--
--------------------------------------------------------------------
--第三个例子
local function PauseTest()
    local ret = createTestLayer("Pause Test")
    local schedulerEntry = nil
    local function unpause(dt)
        --scheduler:实现定时器的功能 
        --取消脚本层的定时器
        scheduler:unscheduleScriptEntry(schedulerEntry)
        schedulerEntry = nil
        --在容器中通过标记得到相应的节点
        local  node = ret:getChildByTag( kTagGrossini )
        --getInstance()得到一个导演的实例
        local  pDirector = cc.Director:getInstance()
        --[[getActionManager获取与导演相关联的动作管理器  resumeTarget让目标恢复运行,在执行序列中所有被暂停的动作都将
        重新运行]]


        pDirector:getActionManager():resumeTarget(node)
    end


    local function onNodeEvent(event)
        --当进入当前页面时
        if event == "enter" then
            --得到窗口大小,以点为单位
            local  s = cc.Director:getInstance():getWinSize()
            local  l = cc.Label:createWithTTF("After 3 seconds grossini should move", "fonts/Thonburi.ttf", 16)
            ret:addChild(l)
            --设置锚点
            l:setAnchorPoint(cc.p(0.5, 0.5))
            l:setPosition( cc.p(s.width / 2, 245) )
            
            local  grossini = cc.Sprite:create(s_pPathGrossini)
            ret:addChild(grossini, 0, kTagGrossini)
            grossini:setPosition(cc.p(200,200))
            
            local  action = cc.MoveBy:create(1, cc.p(150,0))


            local  pDirector = cc.Director:getInstance()
            --增加一个动作  
            --第一个参数是动作  第二个是动作的目标  第三个表示是否暂停 true是暂停
            pDirector:getActionManager():addAction(action, grossini, true)
            --为脚本层定制的计时器,回调函数在指定的时间间隔触发
            --3秒触发
            schedulerEntry = scheduler:scheduleScriptFunc(unpause, 3.0, false)
            cclog("enter")
            --当离开当前页面时
        elseif event == "exit" then
            if schedulerEntry ~= nil then
                --scheduleScriptFunc返回的ID用于此   取消这个计时器
                scheduler:unscheduleScriptEntry(schedulerEntry)
                cclog("exit")
            end
        end
    end
    --注册一个监视器:注册基本事件 包括 触屏 层的进入 退出 事件   动作都需要注册监听事件
    --[[registerScriptTouchHandler 注册触屏事件
    registerScriptTapHandler 注册点击事件
    registerScriptKeypadHandler 注册键盘事件
    registerScriptAccelerateHandler 注册加速事件]]
    ret:registerScriptHandler(onNodeEvent)
    return ret
end


--------------------------------------------------------------------
--
-- RemoveTest
--
--------------------------------------------------------------------
local function RemoveTest()
    local ret = createTestLayer("Remove Test")
    local  l = cc.Label:createWithTTF("Should not crash", "fonts/Thonburi.ttf", 16)
    local  s = cc.Director:getInstance():getWinSize()
    ret:addChild(l)
    l:setAnchorPoint(cc.p(0.5, 0.5))
    l:setPosition( cc.p(s.width / 2, 245))
    --kTagGrossini=1
    local  pMove = cc.MoveBy:create(3, cc.p(200,0))
    --local  ppMove = cc.MoveBy:create(1, cc.p(200, 0))
    local function stopAction()
        --同过标记找到节点
        local  pSprite = ret:getChildByTag(kTagGrossini)
        --通过标记删除动作
        pSprite:stopActionByTag(kTagSequence)
    end
    --kTagSequence=2
    local callfunc = cc.CallFunc:create(stopAction)
    local  pSequence = cc.Sequence:create(pMove,callfunc)
    pSequence:setTag(kTagSequence)


    local  pChild = cc.Sprite:create(s_pPathGrossini)
   -- local  ppChild= cc.Sprite:create(s_pPathGrossini)


    --ppChild:setPosition(100, 100)
    pChild:setPosition( 200, 200 )


    --ppChild:runAction(cc.Sequence:create(ppMove))
    --ret:addChild(ppChild,2,3)


    ret:addChild(pChild, 1, kTagGrossini)
    pChild:runAction(pSequence)
    return ret
end




--------------------------------------------------------------------
--
-- ResumeTest
--
--------------------------------------------------------------------
local function ResumeTest()
    local ret = createTestLayer("Resume Test")


    local schedulerEntry = nil
    local function resumeGrossini(time)
        scheduler:unscheduleScriptEntry(schedulerEntry)
        schedulerEntry = nil
        local  pGrossini = ret:getChildByTag(kTagGrossini)
        local  pDirector = cc.Director:getInstance()
        pDirector:getActionManager():resumeTarget(pGrossini)
    end




    local function onNodeEvent(event)
        if event == "enter" then
            local  l = cc.Label:createWithTTF("Grossini only rotate/scale in 3 seconds", "fonts/Thonburi.ttf", 16)
            ret:addChild(l)
            local  s = cc.Director:getInstance():getWinSize()
            l:setAnchorPoint(cc.p(0.5, 0.5))
            l:setPosition( s.width / 2, 245)


            local  pGrossini = cc.Sprite:create(s_pPathGrossini)
            ret:addChild(pGrossini, 0, kTagGrossini)
            pGrossini:setPosition(200,200)


            pGrossini:runAction(cc.ScaleBy:create(2, 2))


            local  pDirector = cc.Director:getInstance()
            pDirector:getActionManager():pauseTarget(pGrossini)
            pGrossini:runAction(cc.RotateBy:create(2, 360))


            schedulerEntry = scheduler:scheduleScriptFunc(resumeGrossini, 3.0, false)
        elseif event == "exit" then
            if schedulerEntry ~= nil then
                scheduler:unscheduleScriptEntry(schedulerEntry)
            end
        end
    end


    ret:registerScriptHandler(onNodeEvent)


    return ret
end




function ActionManagerTestMain()
    cclog("ActionManagerTestMain")
    Helper.index = 1
    cc.Director:getInstance():setDepthTest(true)
    local scene = cc.Scene:create()


    Helper.createFunctionTable = {
        CrashTest,
        LogicTest,
        PauseTest,
        RemoveTest,
        ResumeTest
    }
    scene:addChild(CrashTest())
    scene:addChild(CreateBackMenuItem())
    return scene
end

0 0