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

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.

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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