A game just isn’t a game if it doesn’t have a score, so we need to include one to count the number of asteroids we destroy.
Let’s start by going to the scene editor (remember that you do that by clicking the button ) and creating a new layer to place the score in. To do this, click on the button in the game scene, and select the Create layer option. To finish, change its name to scoreboard, to make it more legible.
Next, we’re going to create a text object that will serve as our scoreboard. Click on the button in the scoreboard layer and select the
Create text option in the dropdown menu.
This will create a text object in the scene. Change its name to score, the fontSize property to 60, the text property to 0, and place it in the upper left corner.
Now, go back to the logic editor by pressing . Go to the root level and create a script called Scoreboard.
Go into it. Scoreboards have at least two functions. The first is to add points, and the second is to reset to zero. We’re going to start by created two input connectors that will activate these two functions. Drag two input connectors out and call them addPoint and reset.
The logic for the scoreboard is very simple. We just need a numeric variable to store the score, and every time the addPoint input connector is activated, add one and assign it to the score text object we just created. So drag out a number element from the Basic category in the Parameters tab. When the ActionOnParam is created, select the adding option represented by the “+” symbol. Click on the target variable and rename it points.
Since we’re going to only add one at a time, double-click on the valueToAdd parameter and write 1 in its content property. To see it in the box itself, also write 1 in its name property. Since we want the result to be saved in the same points variable, drag it from the target input parameter to the result output parameter. Finally, connect the addPoint input connector to the do activator in ActionOnParam.
Now, to assign a value the points variable to the scene’s text object, drag score from the AssetManager panel and in the ActionOnParam that’s created, select the action setText. Next, drag the points variable to the text input parameter. Connect the done trigger to the sum of the do activator in setText. With this, we now have the counter done.
Now we’re going to do the reset. So, we’ll use the setValue action in ActionOnParam. Drag the points variable close to the reset connector, and you’ll see that the default action is setValue. This action assigns the value of the newValue parameter to the variable of the first target parameter. Since we want to assign a zero, double-click on the newValue parameter (set at zero by default). Connect the box to the reset connector. Now, all we have to do is assign this zero to the score text object, so we’ll connect the done trigger to the do in setText.
With this simple graph, we have now finished the scoreboard’s logic. However, we still need one thing–what’s going to activate these connectors? Go up a level.
You can clearly see that nothing is activating the Scoreboard connectors. It’s obvious that what knows when an asteroid is destroyed is the Spaceship script, so we should get out an output connector every time one is destroyed. Go to Spaceship ? Collisions ? Shots-Asteroids. Add a destroyed output connector and connect it to the done trigger in the last box; that is, after finishing the process of destroying an asteroid.
Go up a level. We’re going to continue firing the output connector above. Drag out an output connector, call it asteroidDestroyed, and connect it to the destroyed trigger in the Shots-Asteroids script.
Go up another level. Repeat the same process, but now connect the output connector to the asteroidDestroyed trigger.
If you go up another level, you’ll see that we already have an output connector in Spaceship that we can connect to the addPoint activator in Scoreboard. Connect asteroidDestroyed to addPoint. We’ll leave the reset for a bit later on.
Do a preview and check that the score goes up every time you destroy an asteroid.08/03/2016 / 2 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?