Collisions between shots and asteroids
We’re going to start with the collisions between shots and asteroids. Create a script and rename it Shots-Asteroids. Since we’re going to need the shots and asteroids collections, drag them from the shared bar to the new script. Go into it and create a new input connector. Rename both on and off, and go up a level to connect them to the connectors with the same name.
The logic to detect collisions between shots and asteroids is very simple. In each game cycle, we’ll check each one of the asteroids to see if it’s collided with any shot. If affirmative, we’ll play the asteroid explosion animation and we’ll remove it from the asteroid collection.
NOTE: Games are continually processing logic and drawing it on screen. Each cycle of processing and drawing is called a game cycle. The more cycles per second, the more fluid the game. This speed is measured in FPS (Frames per second), and you can consult this in the lower part of the Preview panel.
We’ll use the KeepActive box to process our logic in each clock cycle. Drag it to the logic panel from the Stream category in the Blackboxes tab, and connect the on input connector to the on1 activator, and off to off.
Everything we connect to the updated1 trigger will be run in each game cycle. Now we need to go through the whole collection of asteroids and for each one, check to see if it’s collided with any shots. So, we’ll use ForEach; drag it out from the Collections category to the panel.
In the input parameter, we have to assign the collection we want to run through; in this case, the collection of asteroids.
When start is activated, ForEach will deposit an element in the collection of the item output parameter and launch the step trigger. It will repeat this process until it has gone through all the elements of the collection. When it’s through, it will launch the finished trigger. So, what we’ll do is create a variable to collect that element in each step by double-clicking on the item parameter. Don’t forget to connect the updated1 trigger in KeepActive to the start activator.
Now, every time step is launched, we’ll have the next asteroid in the asteroids collection available in the item parameter. We’re going to check if it collides with any shots. So, we’ll use BoundingCollision, which we can find in the 2D category. Drag it to the panel and connect step to the test activator.
BoundingCollision can work in two ways:
- it can check to see if two objects collide (the default option).
- it can check to see if an object collides with one or more objects in a collection.
If there should be a collision, a true trigger is launched and it deposits the objects that collide in the output parameters. Since we have the shots in a collection, we’ll use the second option. Select BoundingCollision (by clicking on its header) and in its properties, change the type property to object-objects.
Now we only have to complete the input parameters. Since we have each asteroid to check in item, drag the variable to the first object input parameter in BoundingCollision.
The second parameter is the collection with the elements whose collisions we need to check. In this case, the shots collection. Drag it from the shared bar.
With these three boxes, we’re checking if each asteroid collides with any shots in each game cycle. Now, we have to create the logic for when we detect a collision; that is, when BoundingCollision launches a true trigger. In this case, we’ll make the asteroid explode; that is, we’ll assign the explosion animation to the collided asteroid. So, we’ll use ActionOnParam on the collided asteroid which will be in the item variable. Drag item under the three boxes, and in the ActionOnParam that’s created, select the setAnimation action. Connect the true trigger to the do activator.
In the second parameter of the setAnimation action, we have to specify the name of the animation we would like to play. Remember that when we created the Asteroid sprite in the scene editor, we defined two animations: fly and explosion.
Double-click on the AnimationName parameter and change its content property to explosion. Also rewrite the name property to explosion to see it in the parameter itself without having to consult its properties.
The last step would be to eliminate the destroyed asteroid from the collection of asteroids, for which we’ll use an ActionOnCollection. Drag the asteroids variable to the panel to create an ActionOnCollection and select the removeItem action.
Connect the box with the done trigger in the last one.
All we have left to do is assign the second item input parameter, which is the element to be removed from the collection. Since we already have an item variable with the asteroid, drag it to the second parameter.
With this graph of five boxes, we’ve created the logic for the collisions of asteroids with shots.
If you check the preview, you should see how the asteroids explode when you shoot them. However, we still don’t have anything happening when an asteroid hits the ship. Let’s fix that.08/03/2016 / No Comments
My first game
- My first game with WiMI5
- Signing up
- Before getting started
- Designing the game
- The background
- The ship
- The asteroids
- Collisions between shots and asteroids
- Collisions between the asteroids and the ship
- Collisions between the ship and the edge of the scene
- Game over
- And now what?