My scenario is as follows:
When a user clicks on a button in Phaser.io, I create a new Phaser.sprite and store it in a local variable called newSquare
. This new sprite is then added to an array called Squares
.
At a later point, I call a function to destroy the newSquare
sprite and set the variable to null. Even though the sprite is successfully removed from the screen after calling destroy()
, I am still able to access it from the Squares
array...
I believed that setting the newSquare
variable to null would also affect all other references to it since it is an object. Why is the reference in the Squares
array not being set to null?
Here is an excerpt of the code:
eventFunction ( e, i ) {
let newSquare = Game.add.sprite( Lines.attack.a, 0, 'square');
// Other methods are called here
Squares[ e.target.dataset.line ].push( newSquare );
setTimeout( () => {
if ( newSquare ) {
newSquare.destroy();
newSquare = null;
console.log(Squares); // The array iteration corresponding to newSquare object is still present when it should be set to null to clean the array with _.pull()
}
}, ( 2 * 1000 ) );
}
What am I overlooking here? Thank you.