I just learn Phaser vby this: Making your first Phaser 3 game
But when I run it in Chrome, I got an error in Chrome developer tool:
main.js:23
Uncaught TypeError: Cannot read property 'add' of undefined
at Scene.Create [as create] (main.js:23)
at SceneManager.create (SceneManager.js:612)
at SceneManager.loadComplete (SceneManager.js:506)
at LoaderPlugin.emit (index.js:183)
at LoaderPlugin.loadComplete (LoaderPlugin.js:940)
at LoaderPlugin.fileProcessComplete (LoaderPlugin.js:899)
at SpriteSheetFile.onProcessComplete (File.js:397)
at Image.data.onload (ImageFile.js:132)
The error line is: platforms.create(400, 568, 'ground').setScale(2).refreshBody();
How do solve this problem?
That error would actually be the result of the line platforms = this.physics.add.staticGroup(); and the error is saying that it can't find the property add of undefined. Since you are attempting to use scene.physics.add this indicates that scene.physics is undefined, and at that point in the tutorial it actually is supposed to be undefined. If you continue reading the next steps are how to setup the Arcade physics system. The important bit is adding
physics: {
default: 'arcade',
arcade: {
gravity: { y: 300 },
debug: false
}
},
to your Game config.
@Kasuko Yes, I didn't setting the physics.
Thank you.
I should have continued reading to the next part of the tutorial before running the code. I thought I was using the wrong source code or something.
Most helpful comment
That error would actually be the result of the line
platforms = this.physics.add.staticGroup();and the error is saying that it can't find the propertyaddofundefined. Since you are attempting to usescene.physics.addthis indicates thatscene.physicsisundefined, and at that point in the tutorial it actually is supposed to beundefined. If you continue reading the next steps are how to setup the Arcade physics system. The important bit is addingto your Game config.