EmptyStone
EmptyStone

Reputation: 315

Phaser Putting sprites in and taking them out of an array

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.

  1. one that puts sprites in the array
  2. one that shows if a certain sprite is in the array or not
  3. one that takes the sprites out of 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

Answers (1)

Dumbass
Dumbass

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

Related Questions