当前位置:网站首页>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 .
边栏推荐
- The road of enterprise digital transformation starts from here
- 图扑软件数字孪生智慧风电系统
- R语言可视化散点图(scatter plot)图、为图中的部分数据点添加标签、始终显示所有标签,即使它们有太多重叠、ggrepel包来帮忙
- Isprs2022/ cloud detection: cloud detection with boundary nets
- Debezium系列之:修改源码支持unix_timestamp() as DEFAULT value
- [performance test] jmeter+grafana+influxdb deployment practice
- Oracle故障处理:Ora-10873:file * needs to be either taken out of backup or media recovered
- #夏日挑战赛# HarmonyOS - 实现消息通知功能
- JAD的安装、配置及集成IDEA
- Web3.0时代来了,看天翼云存储资源盘活系统如何赋能新基建(下)
猜你喜欢
在线协作产品哪家强?微软 Loop 、Notion、FlowUs
面试官:Redis 过期删除策略和内存淘汰策略有什么区别?
Interviewer: what is the difference between redis expiration deletion strategy and memory obsolescence strategy?
Mysql database indexing tutorial (super detailed)
Go语言 | 03 数组、指针、切片用法
企业级数据安全,天翼云是这样理解的
公司破产后,黑石们来了
Word查找红色文字 Word查找颜色字体 Word查找突出格式文本
You can have both fish and bear's paw! Sky wing cloud elastic bare metal is attractive!
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
随机推荐
Debezium系列之:记录mariadb数据库删除多张临时表debezium解析到的消息以及解决方法
MySQL数据库索引教程(超详细)
How to quickly advance automated testing? Listen to the personal feelings of the three bat test engineers
Debezium系列之:解析默认值字符集
Go语言学习教程(十五)
国内低代码开发平台靠谱的都有哪些?
cf:B. Almost Ternary Matrix【对称 + 找规律 + 构造 + 我是构造垃圾】
Windows Oracle open remote connection Windows Server Oracle open remote connection
2022最新大厂Android面试真题解析,Android开发必会技术
1亿单身男女撑起一个IPO,估值130亿
Powerful tool for collection processing
Thoroughly understand why network i/o is blocked?
Interviewer: what is the difference between redis expiration deletion strategy and memory obsolescence strategy?
一朵云开启智慧交通新未来
Technology sharing | common interface protocol analysis
5. Data access - entityframework integration
R language uses lubridate package to process date and time data
Debezium系列之:IDEA集成词法和语法分析ANTLR,查看debezium支持的ddl、dml等语句
#夏日挑战赛#数据库学霸笔记,考试/面试快速复习~
The era of Web3.0 is coming. See how Tianyi cloud storage resources revitalize the system to enable new infrastructure (Part 2)