I’m currently working on a small game, and recently I’ve been trying to incorporate scenes into my game so I can have a new scene after the player completes the level. However, every time I try to invoke this.scene.start('EndScene');, it returns TypeError: this.scene is undefined.
Here is a snippet of my code:
function playerCollideEnd(){
console.log('ya made it');
this.scene.start('EndScene');
}
In fact, any time I try to invoke this.scene.get('GameScene');, or any similar function in my GameScene, it will return the undefined error. Please let me know if you want to see anymore code.
console.log(this); will log function playerCollideEnd() to the console. The function itself is called when the player collides with the end marker. This is all located within class GameScene extends Phaser.Scene under the create() function.
console.log(this); outside of any functions but still within create() logs Object { sys: {…}, game: {…}, anims: {…}, cache: {…}, plugins: {…}, registry: {…}, scale: {…}, sound: {…}, textures: {…}, facebook: {…}, … }.
Okay, that makes sense. I tried using the arrow function but then unfortunately the playerCollideEnd() didn’t function as a callback for a collider. However! I just made an arrow function that playerCollideEnd() calls upon and it works! Thanks so much.
Here’s what I fully did:
function playerCollideEnd() {
console.log('ya made it');
endFunc(); //not a good name i know
}
const endFunc = () => {
this.scene.start('EndScene');
}