This is the 11th part of the “Everyone can Make Games” series. This series of tutorials is aimed at absolute novices without any game development experience. It’s not too difficult to make a micro channel game, and you can do it too.
The small game development tool used in the tutorial is wechat official small game production tool: wechat small game production tool
If you are interested in game development, welcome to follow my official account: Little Ant Game development.
In this section we will look at one of the most commonly used physical behaviors in games: collisions.
Meaning of collision
A collision, as the name implies, is when two objects come together. In our real world, if you throw something and it lands on the ground, you can say that it has collided with the ground. In game development, the “collision” we focus on is the moment it happens: who touches whom, and how the colliding party or the hit party should react.
Hit the enemy with a bullet
Let’s look at a specific game example.
In the game, players control “fighter planes” will fire “bullets” to attack the “enemy aircraft”, when the bullets hit the “enemy aircraft”, the “enemy aircraft” will be destroyed.
When the bullet hits the enemy plane, the bullet needs to collide with the enemy plane. After a collision, what the bullet needs to do is remove itself (since it already hit the enemy, the bullet should not pass behind the enemy), and what the enemy needs to do is explode.
Next, we’re going to do something like this: click on the plane, and the plane will fire a bullet that will fly up and hit the enemy plane on the other side.
First import the required resources and set up the scene briefly.
As shown, we imported “fighter”, “enemy” and a “blue bullet”. Place the enemy aircraft directly above the aircraft so that the bullets we fire can hit it.
Next, we implement the logic of the “warplane” : click and fire a bullet that flies up.
In “Hierarchy management” select the fighter, click “Add Events”, and select “When sprites are clicked”.
Next, add the following blocks.
These blocks do something like this: when the fighter is clicked, move the bullet to the fighter’s position and then keep it moving upwards. If you forget the logic of repetition, review it.
Next, add the logic that an enemy plane was hit by a bullet.
In “Hierarchy Management”, select “Enemy aircraft”, click “Add Event”, and select “When sprites collide”.
Choose to hit the “blue bullet”.
Here, we simply use the “Hide yourself” block to simulate an explosion.
And finally, it looks like this. The enemy plane hides when it encounters the Blue bullet.
Finally, let’s implement the “bullet hits the enemy” logic.
Similar to the logic of the enemy aircraft, the bullet also hides itself when it encounters the enemy aircraft.
Click “Preview scene” to see the effect.
Click on the fighter, fire a bullet, hit the enemy plane, then both the bullet and the enemy plane disappear.
Collision outline
At the end of this section, we’ll look at a new concept: collision contours.
In Layer Management, select Enemy aircraft, and in Layer Properties, click the Adjust Collision Profile button.
In the “Adjust Collision Outline” dialog box, you can set the collision outline of rectangle, circle and polygon.
As shown, we currently select the collision outline of the rectangle.
If an enemy uses a “rectangular” collision profile, the following happens at the time of collision.
Figure: If the collision profiles of enemy planes and bullets are both “rectangles”, they will collide at the “collision location” in the figure. Obviously the bullets didn’t hit the wings of the enemy aircraft, but the enemy aircraft had exploded, which was not what we wanted. Therefore, we need to adjust the collision profile of enemy aircraft.
Open “Adjust Collision Contour”, select “Polygon”, and drag the red dot with the mouse to adjust the contour to fit the shape of the enemy aircraft.
In this way, when the bullet touched the wings of the enemy aircraft, it was only when the collision occurred that it hit the enemy aircraft, which was more reasonable.
To summarize
We learned about collision events and building blocks, and used collision to implement an example of bullets attacking enemy aircraft. In addition, we learned a new concept: collision contour.
practice
Try to increase the ability of enemy planes to fire bullets to hit the player’s planes.