-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.js
More file actions
72 lines (70 loc) · 2 KB
/
main.js
File metadata and controls
72 lines (70 loc) · 2 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
65
66
67
68
69
70
71
72
import Phaser, { Game } from "phaser";
let platforms;
let player;
let cursors;
const myGame = new Game({
type: Phaser.AUTO,
width: 850,
height: 695,
physics: {
default: "arcade",
arcade: {
gravity: { y: 300 },
debug: true,
},
},
scene: {
preload() {
// Code that needs to run before the game is on the screen
this.load.image("background", "./assets/forest.png");
this.load.image("floor", "./assets/floor.png");
this.load.spritesheet("person", "./assets/sprite.png", {
frameWidth: 32,
frameHeight: 32,
});
cursors = this.input.keyboard.createCursorKeys();
},
create() {
// Code that runs as soon as the game is on the screen
this.add.image(400, 300, "background");
platforms = this.physics.add.staticGroup();
platforms.create(400, 395, "floor").setScale(0.5).refreshBody();
player = this.physics.add.sprite(200, 100, "person");
player.setBounce(0.5);
player.setCollideWorldBounds(true);
this.physics.add.collider(player, platforms);
this.anims.create({
key: "right",
frames: this.anims.generateFrameNumbers("person", { start: 0, end: 7 }),
frameRate: 10,
repeat: -1,
});
this.anims.create({
key: "left",
frames: this.anims.generateFrameNumbers("person", {
start: 8,
end: 15,
}),
frameRate: 10,
repeat: -1,
});
},
update() {
// Code that runs for every frame rendered in the browser
if (cursors.right.isDown) {
player.setVelocityX(160);
player.anims.play("right", true);
} else if (cursors.left.isDown) {
player.setVelocityX(-160);
player.anims.play("left", true);
} else if (cursors.up.isDown) {
player.setVelocityY(-160);
} else {
player.setVelocityX(0);
player.setVelocityY(0);
player.anims.play("right", false);
player.anims.play("left", false);
}
},
},
});