当前位置:网站首页>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 .

边栏推荐
- What are the cache interfaces of nailing open platform applet API?
- 输油管的布置数学建模matlab,输油管布置的数学模型
- Oracle Chinese sorting Oracle Chinese field sorting
- CDB 实例的启动与关闭
- Oracle日期格式转换 to_date,to_char,to_timetamp 相互转换
- Windows Oracle 开启远程连接 Windows Server Oracle 开启远程连接
- word如何转换成pdf?word转pdf简单的方法分享!
- Precautions for RTD temperature measurement of max31865 module
- 如何快速进阶自动化测试?听听这3位BAT大厂测试工程师的切身感想....
- Pandora IOT development board learning (HAL Library) - Experiment 8 timer interrupt experiment (learning notes)
猜你喜欢

5. 数据访问 - EntityFramework集成

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

自动化测试的好处

100million single men and women supported an IPO with a valuation of 13billion

Common interview questions in Android, 2022 golden nine silver ten Android factory interview questions hit

鱼和熊掌可以兼得!天翼云弹性裸金属一招鲜!

基于FPGA的超声波测距

Oracle Chinese sorting Oracle Chinese field sorting

Technology sharing | common interface protocol analysis

Applet modification style (placeholder, checkbox style)
随机推荐
What are the cache interfaces of nailing open platform applet API?
Go语言学习教程(十五)
Notion 类生产力工具如何选择?Notion 、FlowUs 、Wolai 对比评测
Precautions for RTD temperature measurement of max31865 module
紧固件行业供应商绩效考核繁琐?选对工具才能轻松逆袭!
R language uses lubridate package to process date and time data
Analysis of postman core functions - parameterization and test report
Get wechat avatar and nickname with uniapp
【历史上的今天】7 月 5 日:Google 之母出生;同一天诞生的两位图灵奖先驱
2022年5月腾讯云开发者社区视频月度榜单公布
IFD-x 微型红外成像仪(模块)关于温度测量和成像精度的关系
Debezium系列之:记录mariadb数据库删除多张临时表debezium解析到的消息以及解决方法
面试官:Redis 过期删除策略和内存淘汰策略有什么区别?
Oracle date format conversion to_ date,to_ char,to_ Timestamp mutual conversion
Isprs2020/ cloud detection: transferring deep learning models for cloud detection between landsat-8 and proba-v
Teach you to deal with JS reverse picture camouflage hand in hand
Tupu software digital twin smart wind power system
A cloud opens a new future of smart transportation
cf:B. Almost Ternary Matrix【對稱 + 找規律 + 構造 + 我是構造垃圾】
5年经验Android程序员面试27天,2022程序员进阶宝典