about Scheduler in lua


#1

hi, i’m a newer, please help me!

in c**, schedule can be used like this:
layer::schedule);
it seems that this schedule is bound to the layer, when we replace to another scene->replaceScene), this schedule will be stopped automatically, isn’t it?
but in lua, i can use CCScheduler::scheduleScriptFunc only, if we replace, must invoke CCScheduler::unscheduleScriptEntry manually.
in lua, is there any interface like in c**?


#2

There is CCNode:scheduleUpdateWithPriorityLua(LUA_FUNCTION nHandler, int priority), I think that is what you’ll want. You can use it with CCLayer too as it is a subclass of CCNode. It is not a timer, the handler will be called every update. You will get Delta Time as the first parameter to your handler, so it shouldn’t be a problem even if you want to trigger stuff in bigger time scales. Example for having something happen every second (this is off the top of my head, I can’t test it atm so hope there are no mistakes) :

local myLayer = CCLayer:create()

local timePassed = 0
local function myHandler(dt)
    timePassed = timePassed + dt
    if timePassed > 1 then
        -- do stuff..
        timePassed = 0
    end
end
myLayer:scheduleUpdateWithPriorityLua(myHandler, 1)

Reading the docs it seems you can only have one of these “update selectors” active on a given node at one time, so you should keep that in mind too.


#3

You can use multiple schedulers in Lua.
This is part of the scheduler example re-implemented in Lua works just fine.

local function TwoSchedulers(scene, layer)
    local jump1 = CCJumpBy:create(4, ccp(0,0), 100, 4);
    local jump2 = jump1:reverse();
    local seq = tolua.cast(CCSequence:create(CreateArray{jump2, jump1}), 'CCActionInterval');
    local action = CCRepeatForever:create(seq);
    --
    -- Center
    --
    local grossini = CCSprite:create("Images/grossini.png");
    layer:addChild(grossini);
    grossini:setPosition(ccp(s.width/2,100));
    grossini:runAction(action:copy():autorelease());
    --
    -- Left:
    --
    -- Create a new scheduler, and link it to the main scheduler
    local sched1 = CCScheduler:create();
    scheduler:scheduleUpdateForTarget(sched1, 0, false);
    -- Create a new ActionManager, and link it to the new scheudler
    local actionManager1 = CCActionManager:create();
    sched1:scheduleUpdateForTarget(actionManager1, 0, false);
    for i = 0, 9 do
        local sprite = CCSprite:create("Images/grossinis_sister1.png");
        -- IMPORTANT: Set the actionManager running any action
        sprite:setActionManager(actionManager1);
        layer:addChild(sprite);
        sprite:setPosition(ccp(30+15*i,100));
        sprite:runAction(action:copy():autorelease());
    end
    --
    -- Right:
    --
    -- Create a new scheduler, and link it to the main scheduler
    local sched2 = CCScheduler:create()
    scheduler:scheduleUpdateForTarget(sched2, 0, false);
    -- Create a new ActionManager, and link it to the new scheudler
    local actionManager2 = CCActionManager:create()
    sched2:scheduleUpdateForTarget(actionManager2, 0, false);
    for i = 0, 9 do
        local sprite = CCSprite:create("Images/grossinis_sister2.png");
        -- IMPORTANT: Set the actionManager running any action
        sprite:setActionManager(actionManager2);
        layer:addChild(sprite);
        sprite:setPosition(ccp(s.width-30-15*i,100));
        sprite:runAction(action:copy():autorelease());
    end
    local sliderCtl1, sliderCtl2
    sliderCtl1 = sliderCtl(0, 2, 1, function() sched1:setTimeScale(sliderCtl1.value) end);
    sliderCtl1:setPosition(ccp(s.width / 4.0, VisibleRect.top.y - 20));
    layer:addChild(sliderCtl1.node);
    sliderCtl2 = sliderCtl(0, 2, 1, function() sched2:setTimeScale(sliderCtl2.value) end);
    sliderCtl2:setPosition(ccp(s.width / 4.0*3.0, VisibleRect.top.y-20));
    layer:addChild(sliderCtl2.node);
    scene:registerScriptHandler(
        function (tag)
            if tag == 'exit' then
                scheduler:unscheduleAllForTarget(sched1);
                scheduler:unscheduleAllForTarget(sched2);
            end
        end)
end

Good luck.
Andre


#4

thanks,
CCNode:scheduleUpdateWithPriorityLua is a usable implementation,
“multiple schedulers”, a little complex to me :slight_smile: , i must read the docs and do more coding