Building a Bamboo Cicada Toy Sim in Cocos with Physics Joints
Introduction
Hello everyone, I am Yiyuan Programmer, a lead programmer with 8 years of experience in the game industry.
I wonder if you've noticed recently that a toy called Bamboo Cicada has become popular. Did you play with one when you were a kid?
Looking back, it was a little gadget you could buy for a few bucks at a roadside stall. It was loud and noisy, and adults would often confiscate it before long.
Nowadays, finding a physical one isn't so easy anymore.
So, I decided to make one using Cocos. At the same time, give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime. In this issue, let's look together at how to implement a Bamboo Cicada mini-game in Cocos. Once you've learned it, remember to give a free little red heart, thank you everyone!
Note, this issue does not discuss the story behind the Bamboo Cicada, only how to quickly make a Bamboo Cicada mini-game, for learning and reference only.
1. Pre-Battle Preparations
Sharpening your axe won't delay your work of cutting wood. Make full preparations before the actual battle:
1. Download Cocos
Open the official website, we directly click download:
After jumping, click download again to complete the download and installation of CocosDashboard.
2. Install the Editor
Open CocosDashboard, click Install -> Install Editor -> Choose our suitable engine version -> Here we choose 3.8.7:
3. Create a New Project
After the editor is installed, we create a new 2D empty project named zhuzhiliao:
4. Prepare Assets
All game assets in this issue are generated by AI, please be discerning.
We find a partner to generate the two most critical components of the Bamboo Cicada (the bamboo stick and the cicada) and the background, and place them in the project's textures directory:
2. Scene Editing
1. UI Assembly
First, drag all our images under the Canvas:
Then, simply adjust the position of each image:
That's simple, anything else?
2. Add Components
Next, add collider and rigidbody components to the bamboo stick and the cicada respectively, and adjust the corresponding parameters according to your preference:
For those who don't know how to add them, just click the Add Component button at the bottom of the panel, search in the pop-up box, and select to add:
After adding, click the play button at the top to see the effect (Why does the cicada run away?):
So what to do?
3. Add Constraints
To prevent the cicada from running away, we need to add a constraint to it. Here we use a distance joint DistanceJoint2D.
Remember to also adjust the two anchor points of the joint to the correct positions:
Run it again, and you can see the cicada is constrained:
Isn't something missing? That's right, the rope!
3. Drawing the Rope
1. Create a New Script
In the Asset Manager, right-click the scripts directory and create a new script:
2. Edit the Script
Open the script with a script editor, add several properties at the top, and add the commonly used line-drawing component Graphics in the OnLoad method:
Then, simply and crudely draw the line in the update method:
3. Add the Script
To add a custom script, we can similarly just drag it up from below and set the specified properties:
4. Check the Effect
After running again, you can see the rope!
4. Interaction
The key interaction of the Bamboo Cicada toy is swinging. By swinging the bamboo stick, the cicada spins. Let's take a look together below.
1. Click Events
In the OnEnable method, add the four commonly used click events and their corresponding implementations:
2. Principle
The principle is actually very simple. Because we are using Cocos's built-in physics system, we only need to interact with the bamboo stick, and leave the rest of the cicada-swinging to the system. Those who want to know more can look through the official documentation.
3. Effect Demonstration
Complete the above processing to get the effect shown at the beginning of the article:
5. Going Further
Those who have read this far should have noticed that two souls are still missing. Due to space constraints, I'll just briefly describe them, and you can supplement them yourselves:
1. Sound: The cicada should make a sound when it spins, which can be implemented through the
AudioSourcecomponent.2. Physical Control: The actual battle involved swinging the cicada by pressing the screen. To truly simulate the toy, you also need to support swinging the phone to swing the cicada, which can be done through the device gravity sensor event
Input.EventType.DEVICEMOTION.
Conclusion
Have you all learned it? (To avoid some unnecessary trouble, the original project is not provided in this issue; it's all in the article.)
Where can you learn such clear and detailed game development thinking? Follow me, and keep up with my pace!
I am "Yiyuan Programmer", a lead programmer with 8 years of experience in the game industry. In game development, I hope to help you, and through you, I hope to help everyone.
To be honest, I want a like and a heart! Please share this article with other friends you think might need it. Thank you!
Recommended Articles:
Yiyuan Cocos Mini-Game Practical Collection 2.0
Yiyuan Cocos Mini-Game Practical Collection 1.0
This game worth 6.8 billion, aren't you going to get some hands-on practice? Let's arrange it!
A friend said my jigsaw puzzle game can't batch render because of the Mask...
Who can't make Tetris... Ah? A quicksand version?
A recently popular jigsaw puzzle game, the boss asked me to make one with Cocos 3.8...
Dare to challenge replicating the once-popular Cut the Rope game with Cocos 3.8?
Top 2 from juejin.cn, machine-translated. The original thread is authoritative.
Haha, you can even see this kind of article here.
Ah, this this, ah