I have an array that is used as an inventory between scenes. I make it accessible across scenes by declaring it in main.js, like so:
let config = {
type: Phaser.Canvas,
physics: {
default: 'arcade',
arcade: {
debug: true,
gravity: {
x: 0,
y: 0
}
}
},
width: 1600,
height: 900,
scene: [Title, LivingRoom, DiningRoom, StairRoom, Credits]
//inventory: [null, null, null, null, null, null, null, null, null, null]
};
let keyA, keyD, keyW, keyT, keyG, keyV;
let borderUISize = config.height / 15;
let borderPadding = borderUISize / 3;
let inventory = [];
let game = new Phaser.Game(config);
Across the 3 scenes I currently have in the game, I use push and splice methods to add and take stuff out of the array. I do this with 3 items currently: 2 needles and a spool of thread. Note that the following pieces of code come from 3 separate scenes:
In the first scene
if (this.checkCollision(this.p1, this.spool) && Phaser.Input.Keyboard.JustDown(this.keyT)){
inventory.push("spool");
this.spool.destroy();
}
In the second scene:
if (this.checkCollision(this.p1, this.needleOne) && Phaser.Input.Keyboard.JustDown(this.keyT)){
inventory.push("needleOne");
this.needleOne.destroy();
}
In the third scene:
if (this.checkCollision(this.p1, this.needleTwo) && Phaser.Input.Keyboard.JustDown(this.keyT)){
inventory.push("needleTwo");
this.needleTwo.destroy();
}
The code checks if the player character is touching the item and if they press the pick-up button, then destroys the sprite of the item and pushes a string of the item's name to the array.
I'm having some trouble with the spool item, as it is for some reason making other items disappear from the inventory. The program works fine if the spool is collected first, but if either or both of the needles are collected before the spool, then their strings disappear from the array the moment the spool is collected. I have no idea why this happens, and the only difference I can think of about the spool is that it is located in the first scene of the game, not counting the title scene. Does anyone have a clue why this is happening?
If it helps lead to a solution, I'm using Phaser 3 in VSCode employing arcade physics.