class Example extends Phaser.Scene {
super({ key: "Example" });
"https://assets.codepen.io/9367036/Dark_lvl1_selected.png"
this.load.image("cube", "https://assets.codepen.io/9367036/cube.png");
"https://assets.codepen.io/9367036/GandalfHardcore+Warrior.png",
this.platforms = this.physics.add.staticGroup();
.create(30, config.height, "ground")
.create(192, config.height, "ground")
.create(192 * 2, config.height, "ground")
.create(192 * 3, config.height, "ground")
.create(config.width / 4, config.height / 1.33, "ground")
this.player = this.physics.add.sprite(config.width / 4, 200, "dude");
this.player.setSize(16, 46);
this.player.setOffset(32, 18);
this.player.setBounce(0);
this.player.setCollideWorldBounds(true);
frames: this.anims.generateFrameNumbers("dude", { start: 10, end: 17 }),
frames: this.anims.generateFrameNumbers("dude", { start: 0, end: 4 }),
frames: this.anims.generateFrameNumbers("dude", { start: 30, end: 37 }),
this.cubes = this.physics.add.group();
const cube1 = this.cubes.create(150, 200, "cube");
const cube2 = this.cubes.create(300, 200, "cube");
const cube3 = this.cubes.create(450, 300, "cube");
this.cubes.children.iterate((cube) => {
cube.setMaxVelocity(50, 500);
const allObjects = [this.player, this.cubes, this.platforms];
this.physics.add.collider(allObjects, allObjects);
this.cursors = this.input.keyboard.addKeys({
left: Phaser.Input.Keyboard.KeyCodes.A,
right: Phaser.Input.Keyboard.KeyCodes.D,
shift: Phaser.Input.Keyboard.KeyCodes.SHIFT,
jump: Phaser.Input.Keyboard.KeyCodes.SPACE
this.physics.add.overlap(this.cubes, this.cubes, (cubeA, cubeB) => {
this.physics.world.collide(cubeA, cubeB);
let velocity = walkSpeed;
if (this.cursors.shift.isDown) {
if (this.cursors.left.isDown) {
this.player.setVelocityX(-velocity);
this.player.flipX = false;
} else if (this.cursors.right.isDown) {
this.player.setVelocityX(velocity);
this.player.flipX = true;
this.player.setVelocityX(0);
if (this.cursors.jump.isDown && this.player.body.blocked.down) {
this.player.setVelocityY(-500);
if (!this.player.body.blocked.down) {
if (this.player.body.velocity.y < 0) {
} else if (this.player.body.velocity.y > 0) {
if (this.player.body.velocity.x !== 0) {
this.cubes.children.iterate((cube) => {
if (cube.y > config.height) {
const randomX = Phaser.Math.Between(0, config.width);
cube.setPosition(randomX, 0);
const game = new Phaser.Game(config);
activateControls(["A", "D", "shift", "space"]);