-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
75 lines (59 loc) · 2.04 KB
/
main.js
File metadata and controls
75 lines (59 loc) · 2.04 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
73
74
75
const config = {
type: Phaser.AUTO,
width: 800,
height: 600,
pixelArt: true,
physics: {
default: 'arcade',
arcade: {
gravity: { y: 0 },
debug: false
}
},
scene: { preload, create, update }
};
let player, cursors;
const game = new Phaser.Game(config);
function preload() {
this.load.image('tiles', 'assets/tilesets/town.png');
this.load.tilemapTiledJSON('map', 'assets/tilemaps/map.json');
this.load.spritesheet('player', 'assets/player.png', {
frameWidth: 32,
frameHeight: 32
});
}
function create() {
const map = this.make.tilemap({ key: 'map' });
const tileset = map.addTilesetImage('1', 'tiles');
map.createLayer('Tile Layer 1', tileset, 0, 0);
const collisionLayer = map.createLayer('Collision', tileset, 0, 0);
collisionLayer.setCollisionByProperty({ collides: true });
// collisionLayer.renderDebug(this.add.graphics(), {
// tileColor: null,
// collidingTileColor: new Phaser.Display.Color(255, 0, 0, 100),
// faceColor: new Phaser.Display.Color(0, 255, 0, 255)
// });
player = this.physics.add.sprite(100, 100, 'player', 0);
this.physics.add.collider(player, collisionLayer);
this.cameras.main.startFollow(player);
cursors = this.input.keyboard.createCursorKeys();
this.anims.create({
key: 'walk',
frames: this.anims.generateFrameNumbers('player', { start: 0, end: 1 }),
frameRate: 10,
repeat: -1
});
}
function update() {
const speed = 150;
player.body.setVelocity(0);
if (cursors.left.isDown) player.body.setVelocityX(-speed);
else if (cursors.right.isDown) player.body.setVelocityX(speed);
if (cursors.up.isDown) player.body.setVelocityY(-speed);
else if (cursors.down.isDown) player.body.setVelocityY(speed);
if (cursors.left.isDown || cursors.right.isDown || cursors.up.isDown || cursors.down.isDown) {
player.anims.play('walk', true);
} else {
player.anims.stop();
}
}