To create the collisions, we’ll use two new concepts: collections and sharing variables.
Collections, as its name suggests, are lists of elements. They’re very convenient because they help us organize elements that we need to process jointly to get some information. In the game we’re developing, for example, we need to calculate three collisions:
- shots against asteroids. We have to check if each shot flying through space impacts an asteroid.
- the ship with asteroids. We need to check if the ship hits an asteroid.
- the ship with the borders of the scene. We need to check if the ship ever hits the edges of the scene.
As you’ve probably guessed, in the first two cases, it would be very useful to have a collection with all the shots, and another collection with all the asteroids. That way, to check if they hit each other, we wouldn’t have to do anything but run through the collection of shots and calculate for each element if it collides with an asteroid in the asteroid collection. We’ll leave the third case for later.
Creating a collection and adding elements to it is very simple. We’re going to start by creating a collection for the shots. So, go to the CreateShot script (in Spaceship).
Open the Parameter tab and open the 2D category menu. You’ll see that to the right of each type of variable, there’s a yellow rectangle . This symbol means collection. If, to start dragging it, you click on the parameter’s gray area, a variable of that type will be created. But if you do so from the yellow box, a collection of the selected variable type will be created.
Since the shots and asteroids are sprites, we’re going to create a collection of sprites.
NOTE: All the elements of a collection are the same type. It is not possible to mix different types of variables within the same collection.
Drag a collection of sprites from the Parameters tab to the logic panel (make sure to click on the yellow area).
The first difference you’ll see is the name. When we drag out a collection, the box that is automatically created is ActionOnCollection, which indicates that we’re going to perform actions on a collection, not just on an individual element like ActionOnParam does. Another difference is the distinctive icon that appears to the right of the collection type variables , as is the case with target. Also, the parameters that collections accept have Collection as their type, making them easy to identify.
Let’s continue with the game’s logic. Just like we did with other variables, click on the target parameter and change its name property to shots.
We need to select the addItems action, but since that’s the one that appears by default, we don’t have to do anything. This action requires us to assign the element we want to include in the collection. In our case, we want to introduce all the clones of shots that we create, so drag the clone variable from any parameter to the item(s) 1 input parameter.
And that’s it. Every time a shot is created, we’ll put it in the shots collection. You’ll see that a new parameter appears to put more elements into should it be necessary. Finally, connect ActionOnCollection to the done trigger in the last one.
At the beginning of this section, we mentioned that we would see two new concepts. The first of these was the collections one, which we’ve just seen. The second is the sharing of variables. The problem is the following. If we want to access this collection in another script, how can we if the variable is still inside the script? Well, it’s quite easy, by dragging the shots collection to the gray bar that on the upper part of the logic panel, as you can see in the following image.
You’ll see that when you get to the bar, it will turn a lighter shade of gray, indicating that you can drop the variable.
Now go up a level in the scripts by double-clicking on the background (or using the other alternatives we’ve seen before).
¡Voilà! Now we have a parameter in the CreateShot script with the collection we’ve just shared. To share it, we can drag it to other scripts, which will in turn make them appear in the shared bar of those scripts. Let’s see how it works.
Create a new script and rename it Collisions. We’re going to include all the logic related to the collisions in this script.
Go into it and add another input connector and call them on and off. Go up a level to connect those on and off connectors you just created to Collisions.
Since with Collisions we’re going to start by detecting if the shots and the asteroids collide, we need to have access to both collections. So, we’re going to share the shots collection with Collisions.
You’ll see that when you drag the collection over the script, it lights up, and the icon appears, indicating that you can drop it. When you drop it, you should see the shots collection as a parameter in Collisions.
If you go into Collisions, you should see the collection in the shared bar.
Since we need the asteroid collection, we’re going to share it in order to be able to access it from this script. So, we browse over to the Asteroids script. We have to repeat the same process we just did with the shots. First, drag a collection of sprites from the Parameters tab to the logic panel.
Just like before, the default action is addItems, so we don’t need to change it. Rename the collection to something more appropriate, like asteroids.
Drag out a clone variable to the second input parameter (remember that in this case the clone variable contains a copy of an asteroid).
And connect ActionOnCollection to the last box. To share the asteroids collection, drag it to the shared bar.
Once we’ve shared asteroids, we need to share it among all the scripts until we get to Collisions.
Go up a level and share asteroids with the Spaceship script, and then go into that last one.
Since we’ve shared it, it’s now available on Spaceship’s shared bar. To access asteroids from Collisions, we’re going to drag the variable from the bar to the Collisions script.
Now you should see both collections as shared variables in Collisions.
Well, now we have everything we need to create the logic of collisions for our game. Let’s get started.
Go into Collisions. As I said at the beginning of this section, we have to detect three types of collisions: shots with asteroids, the ship with asteroids, and the ship with the edges of the scene. To keep the logic structure clean, we’re going to use a script for each type of collision.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?