|
1 | 1 | ---
|
2 | 2 | layout: post
|
3 |
| -title: "Shoot 'em up tutorial: 4 - Keyboard controls" |
| 3 | +title: "Shoot 'em up tutorial: 4 - Projectiles" |
4 | 4 | date: 2017-6-10 18:48:11 +0200
|
5 | 5 | author: arcadio
|
6 | 6 | categories: tutorial shootemup
|
7 | 7 | heropic: /pictures/shootemup0.png
|
8 | 8 | ---
|
| 9 | + |
| 10 | +It doesn’t matter if your ship is able to make the Kessel Run in less than 12 parsecs if it is unable to defend against hordes of villainous aliens, so the next step will be to add some firepower. |
| 11 | +First, let's add this spritesheet that defines some nice bright red projectiles: |
| 12 | + |
| 13 | +{% highlight xml %} |
| 14 | + <spritesheet name="playerFire" src="images/spaceships.png"> |
| 15 | + <states> |
| 16 | + <state name="Idle"> |
| 17 | + <layer name="Idle"></layer> |
| 18 | + </state> |
| 19 | + </states> |
| 20 | + <layers> |
| 21 | + <layer name="Idle" x="-50" y="-30"> |
| 22 | + <frame name="Idle1"></frame> |
| 23 | + <frame name="Idle2"></frame> |
| 24 | + <frame name="Idle1"></frame> |
| 25 | + <frame name="Idle3"></frame> |
| 26 | + </layer> |
| 27 | + </layers> |
| 28 | + <frames> |
| 29 | + <frame name="Idle1" x="400" y="0" w="100" h="100" t="50"></frame> |
| 30 | + <frame name="Idle2" x="400" y="100" w="100" h="100" t="50"></frame> |
| 31 | + <frame name="Idle3" x="400" y="200" w="100" h="100" t="50"></frame> |
| 32 | + </frames> |
| 33 | + </spritesheet> |
| 34 | +{% endhighlight %} |
| 35 | + |
| 36 | + |
| 37 | +Now, add this component to the list: |
| 38 | +{% highlight js %} |
| 39 | +{ |
| 40 | + name: "playerFire", |
| 41 | + sprite: "playerFire", |
| 42 | + events: [ |
| 43 | + { |
| 44 | + name: "#loop", code: function (event) { |
| 45 | + this.var.$y -= 10; |
| 46 | + if (this.var.$y < -50) { |
| 47 | + this.engine.destroy(this); |
| 48 | + } |
| 49 | + } |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "#collide", code: function (event) { |
| 53 | + this.engine.destroy(this); |
| 54 | + } |
| 55 | + }] |
| 56 | + } |
| 57 | +{% endhighlight %} |
| 58 | + |
| 59 | +As you can see, it will use the desired spritesheet, and will move upwards 10 pixels each frame. When its position is 50 px above the upper edge of the screen, it will delete itself using the `this.engine.destroy` function. If you have not guessed it yet, methods of `this` interact with the current object, while methods of `this.engine` interact with the whole level. It will also delete itself if it collides with something, but we will forget about that for now. Then, add the fire event to the list of events of playerShip: |
| 60 | +{% highlight js %} |
| 61 | +{ |
| 62 | + name: "fire", code: function (event) { |
| 63 | + if (Math.random() > 0.5) { |
| 64 | + this.engine.spawn("somePlayerFire", "playerFire", { $x: this.var.$x + 40, $y: this.var.$y, $z: this.var.$z - 1 }); |
| 65 | + } else { |
| 66 | + this.engine.spawn("somePlayerFire", "playerFire", { $x: this.var.$x + 70, $y: this.var.$y, $z: this.var.$z - 1 }); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | +{% endhighlight %} |
| 71 | + |
| 72 | +It will randomly shoot from either the left or right cannon, spawning a `playerFire` called somePlayerFire at the desired relative position. Deploy your game and press the space bar to see some nice fireworks! |
| 73 | + |
| 74 | +You can continue to [part 5 of this tutorial]({{ site.baseurl }}{% post_url 2017-6-10-shootemup5 %}), where you will learn how to add some enemies. |
0 commit comments