|
2 | 2 |
|
3 | 3 | ## Goals
|
4 | 4 |
|
5 |
| -- Add a new spirte to the map |
| 5 | +- Change the location of an existing sprite |
| 6 | +- Reset the game when your character runs into the pit |
| 7 | +- Make an object appear based on user interaction |
| 8 | +- Add a new sprite to the map |
6 | 9 |
|
7 | 10 | ## Getting set up
|
8 | 11 |
|
|
12 | 15 |
|
13 | 16 | - Open up `index.html`
|
14 | 17 |
|
| 18 | + |
15 | 19 | ## objects.js
|
16 | 20 |
|
17 |
| -## What's a spirte? What's part of the map? |
| 21 | +We're going to put all of our objects into this file. Right now it has a bunch of objects all set up using Sprites. Here's what one of them looks like: |
| 22 | + |
| 23 | +``` javascript |
| 24 | +// A pit of dispair |
| 25 | +pit = new Sprite(16, 16) |
| 26 | +pit.image = game.assets['images/map.png'] |
| 27 | +pit.frame = 9 |
| 28 | +pit.x = 16 * 5 |
| 29 | +pit.y = 16 * 5 |
| 30 | +game.rootScene.addChild(pit) |
| 31 | +``` |
| 32 | + |
| 33 | +This is the basic format for adding sprites. We tell it which image to use for sprites, which frame to use (just like the map), and then we tell it where to put the object with `x` and `y` coordinates. Since our tiles are 16 pixels, we can multiply single numbers by 16 to move it one tile at a time. |
| 34 | + |
| 35 | +After we put the sprite on the page, we set up a function to interact with the object. Right now this function is empty with a placeholder for when your character runs into the pit. |
| 36 | + |
| 37 | +``` javascript |
| 38 | +function pitframe() { |
| 39 | + |
| 40 | + if ( character.intersect(pit) ) { |
| 41 | + |
| 42 | + } |
18 | 43 |
|
19 |
| -- Layers things |
| 44 | +} |
| 45 | +pit.addEventListener("enterframe", pitframe) |
| 46 | +``` |
| 47 | + |
| 48 | +Anytime you want to create a new sprite, you can copy and paste this sample and rename the variables. |
20 | 49 |
|
21 | 50 | ## Your assignment
|
22 | 51 |
|
| 52 | +### Reset the game when you fall into the pit |
| 53 | + |
| 54 | +The first thing we want to to is make the game reset when you fall into the pit. I've already coded up a function called `resetGame` which will reset the game. But we need to call it when your character intersects with the pit. |
| 55 | + |
| 56 | +To do this, we'll call `resetGame()` inside of the function. |
| 57 | + |
| 58 | +``` javascript |
| 59 | +function pitframe() { |
| 60 | + |
| 61 | + if ( character.intersect(pit) ) { |
| 62 | + resetGame() |
| 63 | + } |
| 64 | + |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +### Move the flowers somewhere else |
| 69 | + |
| 70 | +The `x` and `y` properties of the sprite tell it where on the map the sprite should be. Let's move the flowers down to the corner by changing the `y` value. |
| 71 | + |
| 72 | +``` javascript |
| 73 | +flowers.x = 16 * 2 |
| 74 | +flowers.y = 16 * 14 |
| 75 | +``` |
| 76 | + |
| 77 | +### Make the pot appear when you walk over the flowers |
| 78 | + |
| 79 | +You might notice that we have a sprite called `pot` that isn't visible. I want this pot to appear when you walk over the flowers. There's already some logic in place, all we need to do is place the pot into the map. The pot sprite is missing one line the other sprites have: `game.rootScene.addChild(pot)` so let's add it in the logic. |
| 80 | + |
| 81 | +``` javascript |
| 82 | +function flowersframe() { |
| 83 | + if ( character.intersect(flowers) && !potVisible) { |
| 84 | + game.rootScene.addChild(pot) |
| 85 | + potVisible = true |
| 86 | + } |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +The pot should now show up when you walk over the flowers! |
| 91 | + |
| 92 | +### Add a treasure chest to the page |
| 93 | + |
| 94 | +To get some practice for your own game, let's add one more sprite to this page. I want to add a treasure chest sprite to the upper right of the map. We're going to copy and paste the code before, and rename the variables: |
| 95 | + |
| 96 | +``` javascript |
| 97 | +// A treasure chest |
| 98 | +chest = new Sprite(16, 16) |
| 99 | +chest.image = game.assets['images/map.png'] |
| 100 | +chest.frame = 25 |
| 101 | +chest.x = 16 * 13 |
| 102 | +chest.y = 16 * 2 |
| 103 | +game.rootScene.addChild(chest) |
| 104 | +``` |
| 105 | + |
| 106 | +Now you should have a treasure chest showing up! |
23 | 107 |
|
24 | 108 | ## Free time
|
25 | 109 |
|
| 110 | +Add some objects to your map. |
| 111 | + |
0 commit comments