-
Notifications
You must be signed in to change notification settings - Fork 33
/
state1.js
56 lines (48 loc) · 1.58 KB
/
state1.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
demo.state1 = function(){};
var cursors, vel = 500, rocks, grass;
demo.state1.prototype = {
preload: function(){
game.load.tilemap('field', 'assets/tilemaps/field.json', null, Phaser.Tilemap.TILED_JSON);
game.load.image('grassTiles', 'assets/tilemaps/grassTiles.png');
game.load.image('rockTiles', 'assets/tilemaps/rockTiles.png');
game.load.image('adam', 'assets/sprites/adam.png');
},
create: function(){
game.physics.startSystem(Phaser.Physics.ARCADE);
game.stage.backgroundColor = '#DDDDDD';
addChangeStateEventListeners();
var map = game.add.tilemap('field');
map.addTilesetImage('grassTiles');
map.addTilesetImage('rockTiles');
grass = map.createLayer('grass');
rocks = map.createLayer('rocks');
map.setCollisionBetween(1, 9, true, 'rocks');
map.setCollision(11, true, 'grass');
adam = game.add.sprite(200, 200, 'adam');
adam.scale.setTo(0.2, 0.2);
game.physics.enable(adam);
cursors = game.input.keyboard.createCursorKeys();
},
update: function(){
game.physics.arcade.collide(adam, rocks, function(){ console.log('hitting rocks!'); });
game.physics.arcade.collide(adam, grass, function(){ console.log('hitting grass!'); });
if(cursors.up.isDown){
adam.body.velocity.y = -vel;
}
else if(cursors.down.isDown){
adam.body.velocity.y = vel;
}
else{
adam.body.velocity.y = 0;
}
if(cursors.left.isDown){
adam.body.velocity.x = -vel;
}
else if(cursors.right.isDown){
adam.body.velocity.x = vel;
}
else{
adam.body.velocity.x = 0;
}
}
};