Reputation: 315
I'm trying to use an array of sprites as an inventory system, but to do so I need three methods to use with the array.
I have a solution for the second one, and I've tried a few times to use add and remove methods for the other two, but I don't know exactly how to call those methods correctly, as the ways I tried just got me error messages. As I have found the online documentation too confusing for me, can please tell me of the methods I'm looking for?
Also, I've tried creating the array in 2 formats:
let inventory = [];
and
let inventory = [null, null, null, null, null, null, null, null, null, null];
Do the methods I'm looking for change depending on which one I use.
If it helps, I'm using Phaser 3 in VSCode, employing arcade physics.
Upvotes: 1
Views: 167
Reputation: 105
You should just be able to use the push
and splice
methods of the array.
Add Sprite to Array:
const array = []
const sprite = [REFERENCE TO SPRITE]
array.push(sprite)
Remove Sprite from Array:
const array = []
const sprite = [REFERENCE TO SPRITE]
array.splice(array.indexOf(sprite))
Upvotes: 1