Is there a way to use Tweens outside of the Scene? This is my situation: I have a global audio playing (bg music for the title and game scenes) while I switch between scenes, but in one of the scenes (the game over scene) I want to be able to switch the global audio bg music playing, by fading it out, and then fading in the new music loop. But from what I can understand, the tweens are part of the scenes? I can create a sound from the Phaser.Game class, but not a tween? How can I achieve this?
I guess your audio is playing in a sort of “background” scene. If this is the case you can simply access it from within your your “gameOver” scene like so:
const backgroundScene = this.scene.get('Background')
Here is a full example with 3 scenes:
export class PreloadScene extends Phaser.Scene {
constructor() {
super({ key: 'PreloadScene', active: true })
}
preload() {
this.load.audio('music', ['assets/sounds/0781.ogg'])
}
create() {
this.scene.start('BackgroundScene')
this.scene.start('GameOver')
}
}
export class BackgroundScene extends Phaser.Scene {
music
constructor() {
super({ key: 'BackgroundScene' })
}
create() {
this.music = this.sound.add('music', {
loop: true
})
this.music.play()
}
}
export class GameOver extends Phaser.Scene {
constructor() {
super({ key: 'GameOver' })
}
create() {
const backgroundScene = this.scene.get('BackgroundScene')
this.tweens.add({
delay: 2000,
targets: backgroundScene.music,
volume: 0,
duration: 5000
})
}
}
So I guess thats my struggle to understand, why can I only access the tweens inside the scene? What if I want to tween some other objects properties outside of the scene?
If I want to tween anything outside of the scene I’m going to have to pass it to the active scene and tell it to do the tween.
The Tween Manager, along with most internal systems in Phaser 3, is fundamentally bound to a Scene System’s object. This separates each Scene’s tweens, which makes the system more stable, especially with multiple Scenes running at once.
http://labs.phaser.io/view.html?src=src\scenes\transition%20test%201.js
Take a look at this example and see whether you can use scene transitions to achieve this; you can transition out your game over scene before its systems shut down.