MDN self-hosted library · e01.org

Player paddle and controls

Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.

js
class ExampleScene extends Phaser.Scene {
  ball;
  paddle;
  // ...
}
js
class ExampleScene extends Phaser.Scene {
  // ...
  preload() {
    this.load.image("ball", "img/ball.png");
    this.load.image("paddle", "img/paddle.png");
  }
  // ...
}
js
this.paddle = this.add.sprite(
  this.scale.width * 0.5,
  this.scale.height - 5,
  "paddle",
);
js
this.paddle.setOrigin(0.5, 1);
js
this.physics.add.existing(this.paddle);
js
class ExampleScene extends Phaser.Scene {
  // ...
  update() {
    this.physics.collide(this.ball, this.paddle);
  }
}
js
this.paddle.body.setImmovable(true);
js
this.ball.body.setBounce(1);
js
this.paddle.x = this.input.x;
js
this.paddle.x = this.input.x || this.scale.width * 0.5;
js
this.ball = this.add.sprite(
  this.scale.width * 0.5,
  this.scale.height - 25,
  "ball",
);
js
this.ball.body.setVelocity(150, -150);