class Example extends Phaser.Scene {
"https://assets.codepen.io/9367036/catastrophi_tiles_16.png"
"https://assets.codepen.io/9367036/catastrophi_level2.csv"
"https://assets.codepen.io/9367036/spaceman.png",
{ frameWidth: 16, frameHeight: 16 }
this.map = this.make.tilemap({ key: "map", tileWidth: 16, tileHeight: 16 });
const mapWidth = this.map.widthInPixels;
const mapHeight = this.map.heightInPixels;
this.map.setCollisionBetween(54, 83);
const tileset = this.map.addTilesetImage("tiles");
const layer = this.map.createLayer(0, tileset, 0, 0);
this.player = this.physics.add.sprite(50, 100, "player", 1);
this.physics.add.collider(this.player, layer);
this.cameras.main.setBounds(0, 0, mapWidth, mapHeight);
this.cameras.main.startFollow(this.player);
this.keys = this.input.keyboard.addKeys({
up: Phaser.Input.Keyboard.KeyCodes.W,
down: Phaser.Input.Keyboard.KeyCodes.S,
left: Phaser.Input.Keyboard.KeyCodes.A,
right: Phaser.Input.Keyboard.KeyCodes.D
if (this.keys.left.isDown) {
this.player.body.setVelocityX(-160);
} else if (this.keys.right.isDown) {
this.player.body.setVelocityX(160);
this.player.body.setVelocityX(0);
if (this.keys.up.isDown) {
this.player.body.setVelocityY(-160);
} else if (this.keys.down.isDown) {
this.player.body.setVelocityY(160);
this.player.body.setVelocityY(0);
const game = new Phaser.Game(config);
activateControls(["W", "A", "S", "D"]);