-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
122 lines (89 loc) · 3.44 KB
/
index.html
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
<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
<title>Gravity Maze</title>
<script src="phaser.js"></script>
</head>
<body style="margin: 0px;">
<script type="text/javascript">
window.onload = function () {
var gravity;
function deviceOrientationListener(event) {
gravity = event;
}
if (window.DeviceOrientationEvent) {
window.addEventListener("deviceorientation", deviceOrientationListener);
} else {
alert("Sorry, your browser doesn't support Device Orientation");
}
var game = new Phaser.Game(window.innerWidth, window.innerHeight, Phaser.AUTO, '', { preload: preload, create: create, update: update });
var player;
var walls;
var jumptimer = 0;
var jumpEffects;
function preload() {
game.load.image('green', 'assets/green.png');
game.load.image('red', 'assets/red.png');
game.load.image('black', 'assets/black.png');
}
function create() {
game.physics.startSystem(Phaser.Physics.ARCADE);
player = game.add.sprite(250, 250, "green");
game.physics.arcade.enable(player);
player.body.collideWorldBounds = true;
walls = game.add.group();
walls.enableBody = true;
var floor = game.add.sprite(0, 350, "red");
floor.width = window.innerWidth;
walls.add(floor);
floor.body.immovable = true;
var ceiling = game.add.sprite(0, 0, "red");
ceiling.width = window.innerWidth;
walls.add(ceiling);
ceiling.body.immovable = true;
var leftWall = game.add.sprite(0, 0, "red");
leftWall.height = 350;
walls.add(leftWall);
leftWall.body.immovable = true;
var rightWall = game.add.sprite(window.innerWidth - 32, 0, "red");
rightWall.height = 350;
walls.add(rightWall);
rightWall.body.immovable = true;
jumpEffects = {};
}
function update() {
player.body.gravity.x = gravity.gamma * 10;
player.body.gravity.y = gravity.beta * 10;
//player.body.gravity.x = 0 * 10;
//player.body.gravity.y = 90 * 10;
game.physics.arcade.collide(player, walls);
if (game.input.activePointer.isDown && !player.body.touching.none) { //player is on the ground, so he is allowed to start a jump
jumptimer = game.time.time;
jumpEffects = { x: undefined, y: undefined };
if (player.body.touching.down)
jumpEffects.y = -900;
else if (player.body.touching.up)
jumpEffects.y = 900;
if (player.body.touching.left)
jumpEffects.x = 900;
else if (player.body.touching.right)
jumpEffects.x = -900;
} else if (game.input.activePointer.isDown && (jumptimer != 0)) { //player is no longer on the ground, but is still holding the jump key
if (game.time.time - jumptimer > 600) { // player has been holding jump for over 600 millliseconds, it's time to stop him
jumptimer = 0;
jumpEffects = {};
}
} else if (jumptimer != 0) { //reset jumptimer since the player is no longer holding the jump key
jumptimer = 0;
jumpEffects = {};
}
if (jumpEffects.x)
player.body.gravity.x = jumpEffects.x;
if (jumpEffects.y)
player.body.gravity.y = jumpEffects.y;
}
};
</script>
</body>
</html>