当前位置:网站首页>How to realize the Online timer and offline timer in the game
How to realize the Online timer and offline timer in the game
2022-07-05 19:09:00 【51CTO】
hi ! Hello everyone , The ant is me . Welcome to my WeChat official account. 【 Little ants teach you to play games 】, Learn more original game development tutorials .
There is often a need to count down in the game , For example, the countdown 10 Minutes to get 1 A little physical strength , count down 1 You can open a treasure chest in hours , Or get rewards according to the timing of the game .
Today, let's share how to use wechat game making tool to realize the functions of Online timer and offline timer .
Online timer
First, set up the scene .

In the scene shown in the figure , There's only one background , And a basic text , We will use basic text to show the countdown .
A key point to realize the countdown function is to convert time , How to make certain “ Number of seconds ”, How many points is it converted into , How many seconds , Then finally use “ branch : second ” Display in the form of .
We use “ division ” Operation to calculate the current number of minutes , And then use “ Remainder ” Operation to calculate the current number of seconds .
Assume that the current remaining time is 158 second .
Minutes = Rounding down (158 / 60)= Rounding down (2.6333) = 2
Number of seconds = 158 % 60 = 38
Finally, the minutes and seconds can be spliced , Namely “2:38”, Usually, the minutes and seconds are displayed with two digits , So you need to add a before the minutes 0 , That is, the final display time is "02:38" .
below , Let's take a look at the specific implementation .
First, create several global variables .

“ The rest of the time ” It records the total countdown time ( In seconds ),“ minute ” and “ Number of seconds ” It records the minutes and seconds calculated according to the remaining time ,“ Countdown number ” The final countdown of the record to be displayed .
next , by “ Basic text ” Add building block logic for countdown .

Building block logic is not complicated , Set the total countdown time 158 second , Calculate the corresponding minutes and seconds , Use splicing blocks to splice the minutes and seconds , Finally displayed on the text .
Run the scenario .

You can see , We will 158 The second is converted to “02:38” The form of correctly shows .
Let time move
Next , Let's let the countdown move !
Then add the following building blocks under the building blocks above , Let the timer run .

We used a “ repeat ” Building blocks to deal with the countdown to running , And then I used a “ wait for 1 second ” Building blocks realize every interval 1 Second to process . As for the conversion and display of time, it is the same as above , If there is time left , Is every 1 second , Process display once . If there is no time left , Then the countdown ends , Exit loop .
Run it , Look at the end result .

The timer runs normally .
Use functions to reuse building blocks
In the building block logic of the timer , About time conversion 、 Splicing 、 Display some building blocks , Actually, it was used twice , In this case , We can put these reusable building blocks into one “ function ” in .
We create a “ Time conversion ” Function of , Then put this reusable building block into the function .

All the building blocks of the final countdown become like this , It looks much simpler .

Usually we don't only need online timers in games , You also need an offline timer . So what is an offline timer ? When the player exits the game , The timer can still count .
for instance , stay “ delicate 1010” There is one “ Plant a little flower ” The function of , Players every 30 You can water the plants once in minutes , Plants can grow by watering , And generate gold coins . If the player has watered once , Quit the game , And then I waited 30 Minutes later , Back to the game again , At this time, the timing should be over , Players can water the flowers again . in other words , Although the player quit the game , But the timer didn't stop , Still timing , This is the offline timing function .

I want to realize the function of such an offline timer , We need the help of 2 Something .
The first 1 One thing is “ Local cache ” function , It can help us cache the data in the game locally , So when players open the game next time , You can go through “ Local cache ” Get the previously stored data .( There are specific ways to use local cache , You can see This article )
The first 2 This thing is called “ Time stamp ”, It refers to the number of seconds elapsed from a certain time . Generally speaking, timestamp refers to “UNIX Time ”.
Wikipedia for UNIX Definition of time :
UNIX Time , Or called POSIX Time is UNIX Or class UNIX The time representation used by the system : from UTC1970 year 1 month 1 Japan 0 when 0 branch 0 The total number of seconds from to now , Leap seconds not considered .
Generally speaking, this “ Time stamp ” It means from a certain time to the present , How much time has it taken . How can we use it to calculate offline time ? For example , I am here 8 I quit the game at o'clock , Then you can quit the game , Put the “ Time stamp ” Make a note , Use the cache function to record this time . then , I 9 At o'clock , Re entered the game . At this time, the current “ Time stamp ” Subtract the previously cached “ Time stamp ”, You can calculate the offline time .
This is the principle of calculating offline time . In fact, it doesn't mean after the game exits , Secretly run behind the clock , But through “ Local cache ” and “ Time stamp ” These two things , Calculate the offline time .
below , Let's realize the function of offline timing .
First , Open the plug-in of local cache .

After opening , You can use the building blocks related to the local cache .
next , Find get “ Time stamp ” Building blocks of , It is located in “ Calculation ” In category “ At the present time XX” In the building blocks , Click drop down , You can select to get “ Time stamp ”.

Last , Take a look at the building block logic of the implementation .

Let's sort out the whole logic , Every time you enter the game , First, calculate the offline time , adopt “ The current timestamp ” subtract “ Previously cached timestamp ”, Then through conversion , Get the total number of seconds of offline time , And then “ The rest of the time ” subtract “ Offline time ” Get the latest “ The rest of the time ”. If “ The rest of the time ” Greater than 0 Words , Start the countdown , Otherwise, it means the countdown is over .
One thing to note here is the two new cache building blocks in the countdown , whenever “ The rest of the time ” When something changes , We will all be the latest “ The rest of the time ” and “ Time stamp ” Cache . such , Whenever players quit the game , Or enter other scenes of the game , In the cache “ The rest of the time ” and “ Time stamp ” All records are the last moment .
Last , I made an example project of timer , It includes the implementation of Online timer and offline timer , It has been open source to the community , You can get the project by yourself for adaptation or learning .
That's all for today's sharing , I hope this article can help you thoroughly understand the principle and method of implementing a timer in the game .
I am a little ant who can play games and teach you to play games , Welcome to WeChat official account. 【 Little ants teach you to play games 】, Get the most complete wechat game development original tutorial materials in the whole network , Learn some game development knowledge every day .

边栏推荐
- Notion 类生产力工具如何选择?Notion 、FlowUs 、Wolai 对比评测
- 关于 Notion-Like 工具的反思和畅想
- 输油管的布置数学建模matlab,输油管布置的数学模型
- 跨境支付平台 XTransfer 的低代码实践:如何与其他中台融合是核心
- Startup and shutdown of CDB instances
- 紧固件行业供应商绩效考核繁琐?选对工具才能轻松逆袭!
- Isprs2022/ cloud detection: cloud detection with boundary nets
- Go语言 | 02 for循环及常用函数的使用
- Windows Oracle open remote connection Windows Server Oracle open remote connection
- cf:B. Almost Ternary Matrix【對稱 + 找規律 + 構造 + 我是構造垃圾】
猜你喜欢
![[performance test] jmeter+grafana+influxdb deployment practice](/img/32/f07792734d040829398a90a2040146.png)
[performance test] jmeter+grafana+influxdb deployment practice

cf:B. Almost Ternary Matrix【对称 + 找规律 + 构造 + 我是构造垃圾】

面试官:Redis 过期删除策略和内存淘汰策略有什么区别?

Tianyi cloud understands enterprise level data security in this way

618“低调”谢幕,百秋尚美如何携手品牌跨越“不确定时代”?

Applet modification style (placeholder, checkbox style)
![CF: B. almost Ternary Matrix [symétrie + règles de recherche + Construction + I am Construction Waste]](/img/5d/06229ff7cfa144dbcb60ae43d5c435.png)
CF: B. almost Ternary Matrix [symétrie + règles de recherche + Construction + I am Construction Waste]

数据库 逻辑处理功能

Oracle date format conversion to_ date,to_ char,to_ Timestamp mutual conversion

详解SQL中Groupings Sets 语句的功能和底层实现逻辑
随机推荐
flume系列之:拦截器过滤数据
[detailed explanation of AUTOSAR 14 startup process]
#夏日挑战赛# HarmonyOS - 实现消息通知功能
The worse the AI performance, the higher the bonus? Doctor of New York University offered a reward for the task of making the big model perform poorly
Powerful tool for collection processing
Is the performance evaluation of suppliers in the fastener industry cumbersome? Choose the right tool to easily counter attack!
Tianyi cloud understands enterprise level data security in this way
How much does the mlperf list weigh when AI is named?
什么是文本挖掘 ?「建议收藏」
2022全网最全的腾讯后台自动化测试与持续部署实践【万字长文】
HAC集群修改管理员用户密码
企业级数据安全,天翼云是这样理解的
You can have both fish and bear's paw! Sky wing cloud elastic bare metal is attractive!
视频融合云平台EasyCVR增加多级分组,可灵活管理接入设备
Mysql database indexing tutorial (super detailed)
Analysis of postman core functions - parameterization and test report
Technology sharing | common interface protocol analysis
Use file and directory properties and properties
2022 the most complete Tencent background automation testing and continuous deployment practice in the whole network [10000 words]
EasyCVR授权到期页面无法登录,该如何解决?