MrToenails
MrToenails

Reputation: 389

Pass parameters through scene.switch() in phaser 3?

Is there a way to pass data through the scene.switch() function? I've seen this in other scene changing functions, but not this one and I don't know how to receive the data. How could I do that if my code looks something like this, where message is the second parameter passed through scene.switch() function.

class MyScene1 extends Phaser.Scene {
    constructor() {
        super('scene1')
    }

    create() {
        this.scene.switch('scene2', 'greetings from scene 1')
    }
}

class MyScene2 extends Phaser.Scene {
    constructor() {
        super('scene2')
    }

    create() {
        // Set message to the value that was passed through scene.switch()
        console.log(message)
    }
}

Upvotes: 1

Views: 453

Answers (1)

MrToenails
MrToenails

Reputation: 389

Ok, so I did some research and it turns out you can do this in the init() function, like so:

class MyScene1 extends Phaser.Scene{
    constructor(){
    super('scene1')
    }
    create(){
    this.scene.switch('scene2', 'greetings froms scene 1')
    }
}
class MyScene2 extends Phaser.Scene{
    constructor(){
    super('scene2')
    }
    init(message){
    console.log(message)
    }
}

Upvotes: 1

Related Questions