Now we’re going to make the ship shoot. In this example, instead of choosing a static image for the shot, we think it’s better to use a sprite, so we need to begin by creating a sprite definition based on the image shot.png. Go back to the scene editor by clicking on the button, which we’ve used before.
Once we’re in the scene editor, click on the button at the bottom of the Sprites panel to create a new sprite. In the image selection window that pops up, select shot_85x50.png and click on Apply. Double-click on it and rename it Shot.
Just like we did earlier for the ship, also rename the default animation animation0 to shot. The animation is very simple as it only has two frames of 85 pixels in width by 50 pixels in height. Copy the properties of the following image:
If you don’t have the action layer selected in the Asset Manager or any of its objects, select it now so you can drag in the shot sprite that we just created. Drag the Shot sprite into the scene and leave it in front of the ship.
The idea is that every time the user presses the spacebar, a shot will be fired. To do this, we’re going to use this object that we’ve just dragged in as a model. That is, every time a shot is fired, what we’ll do is clone this object and apply to it a movement to the right. We’ll repeat this process every time the key is pressed, so that we can have many shots simultaneously.
To make the logic easier, we’re going to have the shot object be the daughter of the ship. For this, in the Asset Manager tab, drag the Shot object over to the Spaceship object. Now adjust the position of the shot to position it just in front of the ship. You can use the data in the following image:
When the game begins, we don’t want the shot we’ve just places in front of the ship to come out, because, as we’ve said, we’re just using this as a model to clone, which should only appear when the player makes a shot. So, we’re going to make it invisible by changing its visible property to false. Now the shot can’t be seen, but we know it’s there because we can still see its selection box, and because we can access it from the Asset Manager panel.
Let’s go back to the logic editor to create the management of the shots. If you aren’t in the Spaceship script, go there now, create a new script, and call it CreateShot.
Remember that the fire output connector from the Input script told us about each time the spacebar was pressed, so we need to connect fire to the in1 connector of the script we’re designing so that a shot is created with every press.
We’re going to start by changing the name of the input connector from in1 to do. To do that, we double-click on it to go in, select the input connector, and in its name property, write do.
When they press the spacebar, the first thing we have to do is create a clone of the shot. This is done with an ActionOnParam box. Drag the shot object from the Asset Manager panel to the logic panel.
As we’ve seen before, this creates an ActionOnParam automatically, and it just so happens that the default action is clone, meaning we don’t need to change it. However, we have to fill in the second parameter, the parent input. Parent is the object that is going to contain the new cloned object. In our case, we want it to do this in the action layer, so we need to drag the action layer from the Asset Manager panel to the parameter.
NOTE: In order to see the types of variables a parameter accepts, click on it when it’s empty and consult the connectableTo property in the properties tab.
Since we’re going to manipulate the clone, we need to create a variable in the clone output parameter, double-clicking on it. Now connect the do input connector to the do activator.
At this point, we now have a clone of the shot. If you test it in the preview, you’ll see that nothing happens. This is because the model we’re cloning is invisible, so its copy is, too.
Drag the clone output parameter variable and drop it next to the first box.
In the new ActionOnParam, select the action setVisibility. In the visibility parameter, you can indicate if you want the target object to be visible or invisible. Since we want it to be visible, double-click on it and in its content property, select true. Connect it to the done trigger in the last box.
If you go to the preview now, you’ll see that when you press the spacebar, a shot comes out of the top left.
This is because we’ve told it to clone in the action layer as a daughter, and the editor positions the clone in the same (x, y) position that the original would have had with respect to its original mother. The original was in the position (73, -15), so it’s placed its new action mother in that position with respect to its new mother action, which is (0,0). The problem is that our original shot was a daughter of the ship, so the position is referred to its origin, which was (115, 290). In reality, this isn’t a problem, because by adding the position of the clone to the position of the ship, the shot will be situated where we want it. We need two ActionOnParam to do this. Drag the Spaceship object from the Asset Manager.
Select the getPosition action from its action property. This action saves the last object’s position in the parameter of the target input, in the position output parameter. Double-click to create a variable of the latter and that way you can use it next. Connect it to the last box.
Now we’re going to add the position of the ship to that of the clone. Drag the clone variable from any parameter it’s being used in and drop it below the first box.
In the new ActionOnParam, select the translate action. Translate adds the position you give it in the translation parameter to the position of the target object, in this case, the position of the ship to the position of the clone. Since we need to assign the position of the ship, something we’ve already done in the last step, drag the position variable out and drop it on the translation input parameter. Finally, connect the box to the last one.
If you do a preview, you’ll see that now the shot is situated correctly in front of the ship. If you press the spacebar more than once, several more clones will appear, but they’ll all pile up on top of each other. In the next step, we’re going to make them move to the right.
We’re going to use yet another ActionOnParam box. Drag a new clone variable out and select the action setTranslationSpeed, which we’ve already used before on the background effect. On this occasion, set a speed of (900,0). That is, double-click on TranslatePerSecond, and in the x property put a value of 900. Since y is already 0, we don’t need to do anything else.
Now if you do a preview, you’ll see that finally, the ship is shooting and all the shots are going as they should.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?