-
Notifications
You must be signed in to change notification settings - Fork 5
/
implementing-acceleration.html
224 lines (201 loc) · 7.02 KB
/
implementing-acceleration.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<!DOCTYPE html>
<html>
<head>
<center>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/phaser.js"></script>
</center>
</head>
<body style="background-color: black">
<li><a href="V1copy.html">V1copy.html</a></li>
<script>
//var Swidth = screen.width;
// var Sheight = screen.height;
var Sheight = window.screen.availHeight * 0.85;
var Swidth = window.screen.availWidth * 1;
var config = {
type: Phaser.AUTO,
width: Swidth,
height: Sheight,
// game.scale.pageAlignHorizontally = true;
// game.scale.pageAlignVertically = true;
//game.scale.refresh();
physics: {
default: "arcade",
},
scene: {
preload: preload,
create: create,
update: update,
},
};
var game = new Phaser.Game(config);
var paused = 0;
//Game Objects
var platforms;
var map;
var player;
//Keyboard controls
var cursors;
var keys;
var space;
var graphics;
var acceleration = 2000;
var maxSpeed = 750;
function preload() {
//this.load.image('sky', 'https://cdn.glitch.global/b2968947-5cd8-4be4-84fd-aed54077cdce/thumbnails%2Fbackground.png?1658781131738');
this.load.image(
"sky",
" https://images-wixmp-ed30a86b8c4ca887773594c2.wixmp.com/f/0ca0366a-2924-452d-8c9d-0f8617807e22/dcatq78-8b3818c2-9e03-4179-9601-e87ac825a0ef.gif?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1cm46YXBwOjdlMGQxODg5ODIyNjQzNzNhNWYwZDQxNWVhMGQyNmUwIiwiaXNzIjoidXJuOmFwcDo3ZTBkMTg4OTgyMjY0MzczYTVmMGQ0MTVlYTBkMjZlMCIsIm9iaiI6W1t7InBhdGgiOiJcL2ZcLzBjYTAzNjZhLTI5MjQtNDUyZC04YzlkLTBmODYxNzgwN2UyMlwvZGNhdHE3OC04YjM4MThjMi05ZTAzLTQxNzktOTYwMS1lODdhYzgyNWEwZWYuZ2lmIn1dXSwiYXVkIjpbInVybjpzZXJ2aWNlOmZpbGUuZG93bmxvYWQiXX0.kk1b_iX4C7FU7Usd6TfmN7UlSVFXjRWK4zMxk7wcL_0"
);
this.load.image(
"platform",
"https://cdn.glitch.global/b2968947-5cd8-4be4-84fd-aed54077cdce/thumbnails%2Fplatform.png?1658777114483"
);
this.load.atlas(
"slime",
"https://cdn.glitch.global/b2968947-5cd8-4be4-84fd-aed54077cdce/slimesheet.png?1658777114399",
"slimesheet.json"
);
}
function create() {
//Set the background origin to be at (0, 0) or top left corner of the image rather than the center of the image asset
let background = this.add
.tileSprite(0, 0, game.scale.width, game.scale.height, "sky")
.setOrigin(0, 0);
//create all animations
let playerAnimations = {};
var idle = this.anims.create({
key: "slime_idle",
frames: this.anims.generateFrameNames("slime", {
prefix: "slime_idle",
end: 5,
zeroPad: 2,
}),
repeat: -1,
frameRate: 10,
});
playerAnimations["idle"] = idle;
var left = this.anims.create({
key: "slime_left",
frames: this.anims.generateFrameNames("slime", {
prefix: "slime_left",
end: 5,
zeroPad: 2,
}),
repeat: -1,
frameRate: 10,
});
playerAnimations["left"] = left;
var right = this.anims.create({
key: "slime_right",
frames: this.anims.generateFrameNames("slime", {
prefix: "slime_right",
end: 5,
zeroPad: 2,
}),
repeat: -1,
frameRate: 10,
});
playerAnimations["right"] = right;
var maxSpeed;
//Create the platforms and the player character set to collide with the platforms
createPlatforms(this);
player = new Player(this, 400, 400, "slime", playerAnimations);
this.physics.add.collider(player, platforms);
player.play(player.animations["idle"]);
//Set up user input
cursors = this.input.keyboard.createCursorKeys();
keys = this.input.keyboard.addKeys("A, D");
space = this.input.keyboard.addKey(
Phaser.Input.Keyboard.KeyCodes.SPACE
);
space.on("down", pause);
graphics = this.add.graphics();
}
function createPlatforms(scene) {
platforms = scene.physics.add.staticGroup();
//basePlatform is the floor of the game
let basePlatform = platforms.create(
game.scale.width / 2,
game.scale.height - 30,
"platform"
);
basePlatform.setScale(3, 1).refreshBody();
platforms.create(500, 250, "platform");
platforms.create(950, 400, "platform");
}
function update() {
// console.log(paused);
//console.log(player.x);
//Player will not move in the x-axis unless a movement key is being pressed
//player.setVelocityX(0);
//Player has "drag" on the x-axis meaning they slide a bit after an input
player.setDragX(1000);
//Handle player movements
if (cursors.left.isDown || keys.A.isDown) {
if (player.body.velocity.x > -maxSpeed) {
player.setAccelerationX(-acceleration);
} else {
player.setVelocityX(-maxSpeed);
}
player.play(player.animations["left"], true);
} else if (cursors.right.isDown || keys.D.isDown) {
player.setAccelerationX(acceleration);
if (player.body.velocity.x > maxSpeed) {
player.body.velocity.x = maxSpeed;
}
player.play(player.animations["right"], true);
} else {
player.setAccelerationX(0);
player.play(player.animations["idle"], true);
}
if (player.x < 0) {
player.setPosition(Swidth, player.y);
}
if (player.x > Swidth) {
player.setPosition(0, player.y);
}
//let bounds = player.getBounds();
//graphics.clear();
//graphics.lineStyle(1, 0xff0000);
//graphics.strokeRectShape(bounds);
if (player.body.touching.down) {
jump();
}
}
function pause() {
paused = paused + 1;
if (paused == 1) {
player.disableBody();
}
if (paused == 2) {
paused = 0;
player.enableBody();
}
//let pauseX = player.x;
//let pauseY = player.y;
//player.setAccelerationX(0);
//player.setPosition(pauseX,pauseY);
//player.setVelocityY(0);
//console.log(pauseX)
}
function jump(event) {
if (player.body.touching.down) {
player.setVelocityY(-600);
}
}
class Player extends Phaser.Physics.Arcade.Sprite {
animations;
constructor(scene, x, y, spritesheet, animations) {
super(scene, x, y, spritesheet);
this.animations = animations;
scene.add.existing(this);
scene.physics.add.existing(this);
this.setScale(2);
this.setCollideWorldBounds(false);
this.setGravityY(1000);
}
}
</script>
</body>
</html>