Quick-cocos2d-x game development [7] -- timer, cocos2dx Timer

Source: Internet
Author: User

Quick-cocos2d-x game development [7] -- timer, cocos2dx Timer

There are still a lot of timer operations, and many logic judgments in the game are executed at each frame. Quick encapsulates schedule in the lua file scheduler. If it is the first time you contact quick, an error may be reported when writing a timer based on the official api, indicating that schedule is a nil value because other modules are loaded during initialization, this scheduler is not loaded, so when using it, the first thing is to introduce this module,

local scheduler = require("framework.scheduler")

The rest can be written through the api. before writing the quick timer, review the timer Writing Method of cocos2dx native lua.

Calls each frame,

Void scheduleUpdateWithPriority (int priority)

Void scheduleUpdateWithPriorityLua (int nHandler, int priority)


Specify the call interval,

Unsigned int scheduleScriptFunc (unsigned int nHandler, float fInterval, bool bPaused)


There is also a timer cancellation event

Void unscheduleScriptEntry (unsigned int uScheduleScriptEntryID)


Quick scheduler is mainly used to encapsulate the next two functions. In c ++ cocos, we use a timer, which is nothing more than a single frame call. There are countless calls at intervals, with the specified number of calls at intervals and one call at intervals, cancel the call.

Let's look at it one by one,

Call each frame,

    local time = 0    local function update(dt)        time = time + 1        label:setString(string.format("%d", time))    end    scheduler.scheduleUpdateGlobal(update)

Call at a certain interval,

    local time = 0    local function onInterval(dt)        time = time + 1        label:setString(string.format("%d", time))    end    scheduler.scheduleGlobal(onInterval, 1)


Call once at intervals. This encapsulation is good and is very common.

    local time = 0    local function onInterval(dt)        time = time + 1        label:setString(string.format("%d", time))        print("over")    end    scheduler.performWithDelayGlobal(onInterval, 1)

You can see how this is achieved,

function scheduler.performWithDelayGlobal(listener, time)    local handle    handle = sharedScheduler:scheduleScriptFunc(function()        scheduler.unscheduleGlobal(handle)        listener()    end, time, false)    return handleend

In fact, after a certain interval, stop it and execute a callback function.


The last encapsulation is to stop these timers,

scheduler.unscheduleGlobal()

Its parameters are the handles returned by the previous timer, so if you need to stop them later, remember to leave a returned value in the created one.


However, in the game, we may make a countdown, that is, the specified number of calls at a certain interval. This is not encapsulated in quick, but we can do it ourselves, the principle is also very simple. The number is counted every time the task is executed, and the timer is stopped when the specified number of times is reached,

    local handle    local interval = 1    local repeatIndex = 3    local index = 0    local sharedScheduler = CCDirector:sharedDirector():getScheduler()    handle = sharedScheduler:scheduleScriptFunc(function()        index = index + 1        label:setString(string.format("%d", index))        if index >= repeatIndex then            scheduler.unscheduleGlobal(handle)            print("over")        end    end, interval, false)


Effect



This is OK. You can try it on your own.

The timer is like this.



How to set the time for the s7 300 Timer

Call the time relay that connects to the delay, allocate T, and then input S5T # 1M30S in the time input, for example, to set the time to 1 minute 30 seconds. If the time is 1 hour, 23 minutes, 37 seconds, write S5T # 1H23M37S, Which is prefixed with S5T #, followed by a few seconds

The PLC timer instruction in S7-300 is triggered ()

Select

Siemens S7-300/400 timer difference
SIEMENS S7-300/400 series PLC has a total of 5 timer commands, they are: S_PULSE (pulse timer), S_PEXT (Extended pulse timer), S_ODT (on delay timer) s_ODTS, S_OFFDT, and S_OFFDT ). The following describes the differences between the five timer commands.

1. S_PULSE (pulse timer)

Its features are as follows: the input is 1, the timer starts timing, the timer bit is 1, the timer time is up, the timer stops working, and the timer bit is 0. If the input changes to 0 when the scheduled time is not reached, the timer stops working and the timer bit changes to 0.

2. S_PEXT (Extended pulse timer)

Its operation features are: when the input starts from 0 to 1, the timer starts to work, the timer bit is 1, the timer time to, the timer bit is 0. The disconnection of the input signal does not affect the timer (the timer continues timing ).
3. S_ODT (enable the delay timer)

Its operation features: the input signal is 1, the timer starts timing (the timer bit is 0), the timer time is up, and the timer bit is 1. When the timer time reaches, if the input signal is disconnected, the timer bit is changed to 0. If the input signal changes to 0 when the timer time is not reached, the timer stops timing.
4. S_ODTS (persistence-type on-delay timer)

It has the following features: the input signal is 1, the timer starts to work and timing, the timer time is up, and the timer bit is 1. The input signal is only used to trigger the timer. the disconnection of the input signal does not affect the timer and timer bit. The timer bit must use the reset command to change to 0 and trigger the scheduled operation of the next timer.
5. S_OFFDT (power-off delay timer)

Its operation features: when the input signal is from 0 to 1, the timer bit is 1 (but the timer does not start timing); when the input signal is from 1 to 0, the timer starts timing, when the timer time is reached, the timer bit is changed to 0. During the timer process, the input signal is reset from 0 to 1, and the timer starts again from 1 to 0.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.