Tommy_ Posted September 25, 2018 Share Posted September 25, 2018 I'm trying to use phaser with classes, here is my code: class Boot extends Phaser.Scene { constructor() { super({key:'Boot', active: true}); } preload() { this.load.image('example', 'assets/example.png'); } } module.exports = Boot const Phaser = require('phaser') const Boot = require('./Boot') const Main = require('./preload/Main') class MyPhaser extends Phaser.Game { constructor() { super({ type: Phaser.AUTO, width: 800, height: 600, }) this.scene.add('Boot', new Boot(), true); this.scene.add('Main', new Main(), true); } create2() { new Main().create2() } } module.exports = MyPhaser class Main extends Phaser.Scene { constructor() { super({key:'Main', active: true}); } create() { this.add.image(200, 200, 'example') } create2() { this.add.image(400, 400, 'example') } } module.exports = Main now in the class Main inside of create i have access to the example image but the problem is in the create2 I don't have access to image example! I guess it's because this is referring to MyPhaser or something? I'm now good with classes. what is the problem and how can I have access to image example from create2 ?? BTW is this the right way to call function inside of one of the scenes? ( new Main().create2() ) Thanks, Link to comment Share on other sites More sharing options...
blackhawx Posted September 25, 2018 Share Posted September 25, 2018 create2() is not a predefined method built into Phaser. its a custom function you created. You can create as many custom functions as you like, but you really don't need too, or need a create2() function. Simply add all your image assets into the create() method for the main scene. That is how Phaser works. Tommy_ 1 Link to comment Share on other sites More sharing options...
samme Posted September 25, 2018 Share Posted September 25, 2018 You need to start the Main scene from the Boot scene. See https://github.com/samme/brunch-phaser/tree/master/app/scenes (follow the this.scene calls). After creating the game, you shouldn't instantiate scenes yourself. Phaser will do that. See the links in https://github.com/samme/phaser3-faq/wiki#how-do-scenes-work for more background. Tommy_ 1 Link to comment Share on other sites More sharing options...
Recommended Posts