-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.js
92 lines (83 loc) · 2.89 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
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
//初始化整个游戏的精灵,作为游戏开始的入口
import {ResourceLoader} from "./js/base/ResourceLoader.js";
import {BackGround} from "./js/runtime/BackGround.js";
import {DataStore} from "./js/base/DataStore.js";
import {Director} from "./js/Director.js";
import {Land} from "./js/runtime/Land.js";
import {Birds} from "./js/player/Birds.js";
import {StartButton} from "./js/player/StartButton.js";
import {Score} from "./js/player/Score.js";
import {BeginButton} from "./js/player/BeginButton";
export class Main {
constructor() {
this.canvas = wx.createCanvas();
this.ctx = this.canvas.getContext('2d');
this.dataStore = DataStore.getInstance();
this.director = Director.getInstance();
const loader = ResourceLoader.create();
loader.onLoaded(map => this.onResourceFirstLoaded(map));
}
//创建背景音乐
createBackgroundMusic() {
const bgm = wx.createInnerAudioContext();
bgm.autoplay = true;
bgm.loop = true;
bgm.src = 'audios/bgm.mp3';
}
onResourceFirstLoaded(map) {
this.dataStore.canvas = this.canvas;
this.dataStore.ctx = this.ctx;
this.dataStore.res = map;
this.createBackgroundMusic();
// const examples = new ApiExamples();
// examples.getUserInfo();
// examples.login();
// examples.getSettings();
// examples.httpExample();
// examples.socketExample();
// examples.download();
// this.init();
this.director.isGameOver = true;
this.dataStore
.put('background', BackGround)
.put('beginButton',BeginButton);
this.dataStore.get('background').draw();
this.dataStore.get('beginButton').draw();
this.registerEvent();
}
init() {
//首先重置游戏是没有结束的
this.director.isGameOver = false;
this.dataStore
.put('pencils', [])
.put('background', BackGround)
.put('land', Land)
.put('birds', Birds)
.put('score', Score)
.put('startButton', StartButton);
this.registerEvent();
//创建铅笔要在游戏逻辑运行之前
this.director.createPencil();
this.director.run();
}
registerEvent() {
// this.canvas.addEventListener('touchstart', e => {
// //屏蔽掉JS的事件冒泡
// e.preventDefault();
// if (this.director.isGameOver) {
// console.log('游戏开始');
// this.init();
// } else {
// this.director.birdsEvent();
// }
// });
wx.onTouchStart(() => {
if (this.director.isGameOver) {
console.log('游戏开始');
this.init();
} else {
this.director.birdsEvent();
}
});
}
}