-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
64 lines (54 loc) · 2.06 KB
/
main.js
File metadata and controls
64 lines (54 loc) · 2.06 KB
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
var main = {
preload: function () {
// This function will be executed at the beginning
// That's where we load the game's assets
'use strict'
game.load.image('paddle', 'assets/paddle.png');
game.load.image('brick', 'assets/brick.png');
game.load.image('ball', 'assets/ball.png');
},
create: function() {
// This function is called after the preload function
// Here we set up the game, display sprites, etc.
game.physics.startSystem(Phaser.Physics.ARCADE);
this.cursor = game.input.keyboard.createCursorKeys();
this.paddle = game.add.sprite(200, 400, 'paddle');
game.physics.arcade.enable(this.paddle);
this.paddle.body.immovable = true;
this.bricks = game.add.group();
this.bricks.enableBody = true;
for(var i = 0; i < 5; i++){
for(var j = 0; j < 5; j++){
game.add.sprite(55+i*60, 55+j*35, 'brick', 0, this.bricks);
}
}
this.bricks.setAll('body.immovable', true);
this.ball = game.add.sprite(200, 300, 'ball');
game.physics.arcade.enable(this.ball);
this.ball.body.velocity.x = 200;
this.ball.body.velocity.y = 200;
this.ball.body.collideWorldBounds = true;
this.ball.body.bounce.x = 1;
this.ball.body.bounce.y = 1;
},
update: function() {
// This function is called 60 times per second
// It contains the game's logic
if(this.cursor.right.isDown){
this.paddle.body.velocity.x = 350;
} else if (this.cursor.left.isDown){
this.paddle.body.velocity.x = -350;
} else {
this.paddle.body.velocity.x = 0;
};
game.physics.arcade.collide(this.paddle, this.ball);
game.physics.arcade.collide(this.ball, this.bricks, this.hit, null, this);
},
hit: function(ball, brick){
brick.kill();
}
};
// Initialize Phaser, and start our 'main' state
var game = new Phaser.Game(400, 450, Phaser.AUTO, 'gameDiv');
game.state.add('main', main);
game.state.start('main');