CocosCreator realizes skill CD effectThere are skills in many games. After the player clicks the skill button, the skill will have a cooldown time. After the cooldown time is over, the skill can be used again. It is very simple to achieve this effect in Cocos. You need to use the sprite component. First, drag the image of the skill button to the canvas. Then create a new label under the skill button Then create a new TS script and copy and paste the following code into it const {ccclass, property} = cc._decorator; @ccclass export default class NewClass extends cc.Component { @property(cc.Sprite) skill:cc.Sprite = null; //Skill sprite @property(cc.Label) time_label:cc.Label = null; //Show the text of the remaining time of skill cooling @property time:number = 3; //Skill cooling time @property isshow_label:boolean = true; //Whether to display text onLoad(){ this.skill.fillRange = 1; //When the game starts, the skill fill range is 1 } update(dt:number){ if(this.skill.fillRange != 1){//If the skill wizard's fill is not 1, it means that the skill has been usedthis.skill.fillRange += dt / this.time;//The value restored per frame for the skill is frame rate / skill cooldown timethis.time_label.string = Math.floor(((1 - this.skill.fillRange) * this.time)).toString();//Update the remaining time of the skill per frame//The remaining time of the skill is first 1 - the filling degree of the skill wizard and then * the skill cooldown time, and finally Math.floor roundedif(this.isshow_label == true){//If the text can be displayedthis.time_label.node.active = true;//Show the remaining time of the skill cooldown} } if(this.skill.fillRange == 1){//If the skill sprite's fill is 1, it means the skill has not been used yet this.skill.getComponent(cc.Button).interactable = true;//Start button this.time_label.node.active = false;//Hide the remaining time of skill cooldown} } onbtn(){//Event when the skill button is pressed this.skill.fillRange = 0;//Skill fill range is set to 0 console.log("Skills used"); //Print log this.skill.getComponent(cc.Button).interactable = false; //disable button} } I have written detailed comments for each line of code. Hang the written script on the skill button and bind the node Can be modified as needed
Writing code is not enough. You also need to do some settings for the skill button. You need to modify the sprite component and add a button component to the skill button. Adjust according to the picture
Finally, add the button component to the skill button The bound event is onbtn. To make it look better, change the Transition of the button component to COLOR. You're done. Click Run to see it. Really good When you click the skill button, just put the code in onbtn. Just put it in here For example, you can play a special effect animation when you press a skill button The above is the details of how CocosCreator implements the skill cooling effect. For more information about CocosCreator skill cooling, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: How to implement Docker volume mounting
>>: MySQL 5.5.27 installation graphic tutorial
When the software package does not exist, it may ...
React is a JAVASCRIPT library for building user i...
When faced with a SQL statement that is not optim...
Note: I use Centos to install docker Step 1: Inst...
The configuration is very simple, but I have to c...
This article uses an example to describe how to r...
1. Command method Run the nginx service in the cr...
We know that the commonly used events in JS are: ...
Running Docker requires root privileges. To solve...
echarts word cloud is an extension of echarts htt...
Table of contents MySQL case sensitivity is contr...
Preface In the process of developing a mini progr...
Publish Over SSH Plugin Usage Before using Publis...
Table of contents 1. Phenomenon 2. Solution 3. Su...
This post focuses on a super secret Flutter proje...