Skip to content

Commit c8cbb34

Browse files
committed
Flesh out the lesson
1 parent 7a39a04 commit c8cbb34

File tree

4 files changed

+171
-10
lines changed

4 files changed

+171
-10
lines changed

lessons/3-objects/README.md

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
## Goals
44

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
69

710
## Getting set up
811

@@ -12,14 +15,97 @@
1215

1316
- Open up `index.html`
1417

18+
1519
## objects.js
1620

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+
}
1843

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.
2049

2150
## Your assignment
2251

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!
23107

24108
## Free time
25109

110+
Add some objects to your map.
111+

lessons/3-objects/character.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,28 @@ game.addEventListener("load", function(){
7474
}
7575
}
7676

77+
78+
// Set character.blocked = true to stop moving when colliding when objects
79+
if (character.blocked) {
80+
x = this.x + 2 * (this.x - x)
81+
y = this.y + 2 * (this.y - y)
82+
83+
setTimeout(function() {
84+
character.blocked = false
85+
}, 50)
86+
}
87+
88+
7789
this.x = x
7890
this.y = y
7991

8092
}
8193
character.addEventListener("enterframe", movement)
8294

95+
function resetCharacter() {
96+
character.x = 100
97+
character.y = 100
98+
}
99+
game.rootScene.addEventListener("reset", resetCharacter)
100+
83101
})

lessons/3-objects/lib/game.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,33 @@ var game = new Game(240, 240)
44
game.scale = 2
55
game.fps = 25
66
game.preload('images/map.png', 'images/boy.gif', 'images/girl.gif')
7-
var map, map, character
7+
var map, character
8+
9+
function resetGame(){
10+
document.body.setAttribute('style', 'background-color:#911')
11+
setTimeout(function() {
12+
document.body.setAttribute('style', '')
13+
}, 100)
14+
setTimeout(function() {
15+
document.body.setAttribute('style', 'background-color:#911')
16+
}, 200)
17+
setTimeout(function() {
18+
document.body.setAttribute('style', '')
19+
}, 300)
20+
21+
event = new enchant.Event("reset")
22+
game.rootScene.dispatchEvent(event)
23+
}
24+
25+
function endGame(){
26+
document.body.setAttribute('style', 'background-color:#6CC644')
27+
setTimeout(function() {
28+
document.body.setAttribute('style', '')
29+
}, 100)
30+
setTimeout(function() {
31+
document.body.setAttribute('style', 'background-color:#6CC644')
32+
}, 200)
33+
setTimeout(function() {
34+
document.body.setAttribute('style', '')
35+
}, 300)
36+
}

lessons/3-objects/objects.js

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ game.addEventListener("load", function(){
66
pit.frame = 9
77
pit.x = 16 * 5
88
pit.y = 16 * 5
9-
pit.scaleX = 2
10-
pit.scaleY = 2
119
game.rootScene.addChild(pit)
1210

13-
pit.addEventListener("enterframe", function(){
14-
//
15-
})
11+
function pitframe() {
1612

13+
if ( character.intersect(pit) ) {
14+
}
15+
16+
}
17+
pit.addEventListener("enterframe", pitframe)
1718

1819

1920
// A moveable pot
@@ -22,6 +23,33 @@ game.addEventListener("load", function(){
2223
pot.frame = 26
2324
pot.x = 16 * 10
2425
pot.y = 16 * 10
25-
game.rootScene.addChild(pot)
26+
27+
potVisible = false
28+
29+
function potframe() {
30+
31+
if ( character.intersect(pot) ) {
32+
character.blocked = true
33+
}
34+
35+
}
36+
pot.addEventListener("enterframe", potframe)
37+
38+
39+
// Some flowers
40+
flowers = new Sprite(16, 16)
41+
flowers.image = game.assets['images/map.png']
42+
flowers.frame = 18
43+
flowers.x = 16 * 2
44+
flowers.y = 16 * 10
45+
game.rootScene.addChild(flowers)
46+
47+
48+
function flowersframe() {
49+
if ( character.intersect(flowers) && !potVisible) {
50+
potVisible = true
51+
}
52+
}
53+
flowers.addEventListener("enterframe", flowersframe)
2654

2755
})

0 commit comments

Comments
 (0)