Collisions between the ship and the edge of the scene
To finish off the collisions, we’re going to make sure that the ship can’t pass the upper and lower limits of the scene. The logic is very similar to what we’ve already seen. Just like before, we’ll make comparisons in every game cycle to see if the position of the ship passes the upper limit of the scene, in which case, we’ll situate the ship in the highest position possible without leaving the scene. We’ll do the same with the lower edge.
Let’s start by creating a script and calling it Ship-Bounds. For the sake of coherence with the earlier ones, create two connectors, on and off, and connect them to the on and off connectors of the Collisions script.
Let’s start by dragging out a KeepActive and connecting it to the on and off input connector.
And we’ll being by verifying that the ship won’t go over the upper limit. So, the first thing we’ll do is get its Y coordinate and check that it is not less than zero. Drag the Spaceship object from the AssetManager panel and in the ActionOnParam it creates, select the getPositionY action. Double-click on its Y position output parameter to collect the value in a variable. Finally, connect the updated1 trigger to the do activator.
Now we’re going to compare to see if the Y position value is less than zero. For this, we’ll use the Compare box that’s in the Stream category in the Blackboxes tab. Drag one to the panel and select the lessThan value in its comparison property. Compare requires two values to compare, which is what the parameters item1 and item2 are for. Complete the first parameter by dragging the Y position variable to the first parameter. Double-click on the item2 parameter to introduce a value to compare it with. Since the default value for the number variable is zero, we don’t need to modify anything. Connect the done trigger from before to the compare activator.
Now, when the true trigger is launched, we’ll know that the ship has a coordinate that’s less than zero; that is, it’s passing over the upper limit of the scene. What we’ll do is reposition the ship to position zero; that is, we’ll leave it at the limit. Drag out the Spaceship variable, and in the ActionOnParam that’s created, select the setPositionY action. Double-click on the second Y position parameter, which is set by default to zero, meaning you don’t need to do anything else. However, for greater clarity, change the name property to 0.
Now we’re going to calculate to keep the ship from passing the lower limit. For this, we’ll use the false trigger in Compare. That is, if the ship hasn’t passed the upper limit, we’re going to check that it hasn’t passed the lower limit. Drag another Compare box out and select greaterThan in its comparison property. Connect it to the false trigger of the first Compare. Assign the Y position variable to the first parameter. Double-click on the second, but on this occasion, put in a value of 576 (the height of the scene) in its content property. Also change the name property to 576 so you can more clearly see the variable.
When the true trigger is launched in the second Compare, we’ll know that the ship is passing the lower limit. In this case, we’ll do the same as we did before, but positioning it on the lower limit; that is, at the coordinate y=576. Drag the Spaceship variable next to the second Compare and in the ActionOnParam that’s created, select the setPositionY action. In the second parameter, we have to assign the value of a coordinate we wish to establish; in this case, 576. But since we already have a variable set to this value and which we just used in the comparison, drag it up to the second parameter. Finally, connect the true trigger to the do activator.
Do a preview and check that the ship doesn’t pass the limits of the scene. Remember that when we created the ship in the scene editor, we established its anchor as the center, so when we’re comparing its position, we’re using the center of the ship, meaning that when it reaches the edges, half of the ship disappears. If you’d prefer nothing to leave the scene, it’d be just a question of modifying the comparison values.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?