Jump to content

problem with using phaser with classes


Tommy_
 Share

Recommended Posts

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

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.

 

 

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...