-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
63 lines (61 loc) · 2.06 KB
/
main.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
enchant();
window.onload = function () {
var game = new Game(320, 320);
game.rootScene.backgroundColor = 'green';
game.preload('gray.jpg', 'red.jpg');
game.onload = function () {
var score = 0;
// Player's block.
var redBlock = enchant.Class.create(enchant.Sprite, {
initialize: function(){
enchant.Sprite.call(this, 15, 15);
this.image = game.assets['red.jpg'];
this.moveTo(175,175);
this.frame = 15;
game.rootScene.addChild(this);
}
});
var player = new redBlock();
// Vertical block.
var vBlock = enchant.Class.create(enchant.Sprite, {
initialize: function() {
enchant.Sprite.call(this, 15, 15);
this.image = game.assets['gray.jpg'];
this.moveTo(Math.floor(Math.random() * 320), 0);
this.scaleY = 1;
this.tl.moveBy(0, 360, Math.floor(Math.random() * 100));
game.rootScene.addChild(this);
}
});
// Horizontal block.
var hBlock = enchant.Class.create(enchant.Sprite, {
initialize: function() {
enchant.Sprite.call(this, 15, 15);
this.image = game.assets['gray.jpg'];
this.moveTo(0, Math.floor(Math.random() * 320));
this.scaleX = 1;
this.tl.moveBy(360, 0, Math.floor(Math.random() * 100));
game.rootScene.addChild(this);
}
});
// Spawn blocks.
game.rootScene.tl.then(function() {
var hblock = new hBlock();
var vblock = new vBlock();
}).delay(20).loop();
// Move red block.
game.rootScene.on('touchmove', function(evt) {
player.moveTo(evt.localX, evt.localY);
});
// Detect collision on enter frame.
game.rootScene.on('enterframe', function() {
if (vBlock.intersect(redBlock) || hBlock.intersect(redBlock)) {
alert("Game Over\nScore: " + score);
game.stop();
}
score++;
});
game.pushScene(game.rootScene);
};
game.start();
};