Reputation: 315
I'm working on an inventory system, for my game, and I'm having some trouble with the code that gets rid of used items. This is the code the makes visible items disappear, which is activated when the quest involving that item is completed:
if(tutorial == "complete"){
this.spot = 0;
while (this.spot < 18){
if (this.pack[this.spot] == "key"){
this.pack[this.spot] = null;
break;
}
else {
this.spot += 1;
}
}
this.key.destroy();
}
For some reason the item that is displayed in the inventory, in this case a key, doesn't disappear when this code is triggered. I'm wondering if it has something to do with the following code, which makes the key appear in the first place:
if (this.pack[0] == "key"){
this.key = this.physics.add.sprite(180, 150, 'key').setInteractive();
this.key.body.immovable = true;
this.key.body.allowGravity = false;
this.key.setScale(4);
this.key.texture.setFilter(Phaser.Textures.FilterMode.NEAREST);
}
I'm wondering the code that makes the image bigger or clearer is messing something up, but I'm aware it could easily be something else. Anyone know for sure?
If it helps, I'm using Phaser 3 in VSCode employing arcade physics.
Upvotes: 1
Views: 54
Reputation: 14880
If the code is in the update
function, the problem that you are creating multiple images (ever second), you should move the image creation into the create
function, OR check if the this.key
is set, like this only one image will be created.
if (this.pack[0] == "key" && this.key == null){
this.key = this.physics.add.sprite(180, 150, 'key').setInteractive();
...
}
Upvotes: 0