In an Es6 extending Phaser.Scene, the physics engine is not accessible.
With the following code, "this.physics" ends up with an error:
TypeError: Cannot read property 'add' of undefined
therefore, this.physics shows up as undefined.
This was the code I used:
class GameScene extends Phaser.Scene {
constructor(config) {
super({
key: 'GameScene',
physics: {
system: 'impact',
gravity: 100,
setBounds: {
width: 800,
height: 600,
}
}
});
console.log(config);
}
preload() {
this.load.image('mushroom', './assets/images/mushroom2.png')
}
create() {
console.log(this.physics);
this.mushroom = this.physics.add.image(300, 300, 'mushroom').setActive().setVelocity(0, 0).setBounce(0);
}
}
export default GameScene
When using Impact physics, I believe you have to refer to it as this.impact instead of this.physics. I'm not familiar with the particulars of why this is but if you switch to that your code will work.
Took a slightly modified version of your sample code for some quick tests in the Labs. Looks like this.physics is reserved to Arcade Physics. As @iamchristopher noted, use this.impact instead.
Also, that scene configuration is wrong. Should be something like this:
{
key: 'GameScene',
physics: {
impact: {
gravity: 100,
setBounds: {
width: 800,
height: 600
}
}
}
}
Sample code
class GameScene extends Phaser.Scene {
constructor() {
super({
key: 'GameScene',
physics: {
arcade: {
// Will enable Arcade Physics
},
matter: {
// Will enable matter.js
},
impact: {
gravity: 100,
setBounds: {
width: 800,
height: 600
}
}
}
});
}
preload() {
this.load.image('disk', 'assets/sprites/copy-that-floppy.png');
}
create() {
// {
// impact: /* ImpactPhysics { ... } */,
// matter: /* MatterPhysics { ... } */,
// physics: /* ArcadePhysics { ... } */
// }
console.log(this);
this.disk = this.impact.add.image(100, 100, 'disk').setActive().setVelocity(0, 0).setBounce(0);
}
}
const game = new Phaser.Game({
type: Phaser.AUTO,
width: 800,
height: 600,
backgroundColor: '#2d2d2d',
parent: 'phaser-example',
scene: [GameScene]
});
Yes, physics is Arcade Physics, for legacy reasons. You can redefine it in the InjectionMap if you'd like to change it, to whatever you wish.
this.impact is undefined starting with Phaser v3.23
See this demo. It works in v3.22 but not newer
http://phaser.io/examples/v3/view/game-objects/tilemap/collision/weltmeister-map-and-impact
@stahlmanDesign that's correct, this was removed in 3.23. From the Change Log:
### Removed
The following features have been removed in this version of Phaser:
* Impact Physics has been removed completely and is no longer a choice of physics system. The resulting `Scene.impact` property and Impact config object have also been removed.
- Impact Physics has been removed completely and is no longer a choice of physics system. The resulting
Scene.impactproperty and Impact config object have also been removed.
Sad to see support removed for a game engine I loved, but I totally understand the desire to remove features and to make the codebase tighter and more focused.
So few people used it, it didn't warrant keeping as part of the core API I'm afraid.
Most helpful comment
Took a slightly modified version of your sample code for some quick tests in the Labs. Looks like
this.physicsis reserved to Arcade Physics. As @iamchristopher noted, usethis.impactinstead.Also, that scene configuration is wrong. Should be something like this:
Sample code