forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
starstruck.js
130 lines (96 loc) · 3.11 KB
/
starstruck.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.tilemap('level1', 'assets/games/starstruck/level1.json', null, Phaser.Tilemap.TILED_JSON);
game.load.image('tiles-1', 'assets/games/starstruck/tiles-1.png');
game.load.spritesheet('dude', 'assets/games/starstruck/dude.png', 32, 48);
game.load.spritesheet('droid', 'assets/games/starstruck/droid.png', 32, 32);
game.load.image('starSmall', 'assets/games/starstruck/star.png');
game.load.image('starBig', 'assets/games/starstruck/star2.png');
game.load.image('background', 'assets/games/starstruck/background2.png');
}
var map;
var tileset;
var layer;
var player;
var facing = 'left';
var jumpTimer = 0;
var cursors;
var jumpButton;
var bg;
function create() {
game.stage.backgroundColor = '#000000';
bg = game.add.tileSprite(0, 0, 800, 600, 'background');
bg.fixedToCamera = true;
map = game.add.tilemap('level1');
map.addTilesetImage('tiles-1');
map.setCollisionByExclusion([ 13, 14, 15, 16, 46, 47, 48, 49, 50, 51 ]);
layer = map.createLayer('Tile Layer 1');
// Un-comment this on to see the collision tiles
// layer.debug = true;
layer.resizeWorld();
game.physics.gravity.y = 250;
game.physics.setBoundsToWorld();
player = game.add.sprite(32, 32, 'dude');
player.body.bounce.y = 0.2;
player.body.minVelocity.y = 5;
player.body.collideWorldBounds = true;
player.body.setRectangle(16, 32, 8, 16);
// Un-comment this on to see the body collision areas / data
// player.debug = true;
player.animations.add('left', [0, 1, 2, 3], 10, true);
player.animations.add('turn', [4], 20, true);
player.animations.add('right', [5, 6, 7, 8], 10, true);
game.camera.follow(player);
cursors = game.input.keyboard.createCursorKeys();
jumpButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
}
function update() {
game.physics.collide(player, layer);
player.body.velocity.x = 0;
if (cursors.left.isDown)
{
player.body.velocity.x = -150;
if (facing != 'left')
{
player.animations.play('left');
facing = 'left';
}
}
else if (cursors.right.isDown)
{
player.body.velocity.x = 150;
if (facing != 'right')
{
player.animations.play('right');
facing = 'right';
}
}
else
{
if (facing != 'idle')
{
player.animations.stop();
if (facing == 'left')
{
player.frame = 0;
}
else
{
player.frame = 5;
}
facing = 'idle';
}
}
if (jumpButton.isDown && player.body.onFloor() && game.time.now > jumpTimer)
{
player.body.velocity.y = -250;
jumpTimer = game.time.now + 750;
}
}
function render () {
if (player.debug)
{
game.debug.renderPhysicsBody(player.body);
game.debug.renderBodyInfo(player, 16, 24);
}
}