I’ve been working on an idea for a game but ran into a problem. The goal is to add an item to the inventory and have it update,. If I click the button in my codepen demo, I can add an item to the inventory. What I’m having trouble with is handling with the selection, it neither operates the way I want it to. I’d prefer to stay in its original state, unselected, not greyed out.
I’ve been following a video tutorial.
I made a code pen to demonstrate this problem. here
Any help or suggestions would be much appreciated!
class ToolInventoryScene extends Phaser.Scene {
constructor() {
super("ToolInventoryScene")
this.rows = 1
this.uiScale = 1.5
this.gridSpacing = 4
this.margin = 8
this._tileSize = 32
this.inventorySlots = []
}
preload() {
this.load.atlas("items", "https://i.ibb.co/L8YLMvt/tools.png", json)
}
init(data) {
this.inventory = data.inventory
this.maxColumns = data.inventory.maxColumns
this.maxRows = data.inventory.maxRows
}
get tileSize() {
return this._tileSize * this.uiScale
}
refresh() {
for (let index=0; index < this.maxColumns * this.rows; index++) {
let x = this.margin + this.tileSize / 2 + (index * (this.tileSize + this.gridSpacing)) + 90;
let y = this.margin + this.tileSize / 2
let inventorySlot = this.add.sprite(x,y, "items", "panel.png")
inventorySlot.setScale(this.uiScale)
let item = this.inventory.getItem(index)
if (item) {
inventorySlot.item = this.add.sprite(inventorySlot.x, inventorySlot.y, "items", items[item.name].frame)
}
this.inventorySlots.push(inventorySlot)
}
this.updateSelected()
}
create() {
this.button = this.add.sprite(30,30, "items", "fire.png").setInteractive()
this.button.on("pointerdown", () => {
console.log("clicked")
console.log(this.inventory)
this.inventory.addItem({name: "Short Sword", quantity: 1})
this.refresh()
console.log(this.inventory.items)
})
this.input.keyboard.on("keydown-ONE", () => {
this.inventorySlots[0].tint = 0xffff00
this.inventorySlots[1].tint = 0xfffff
})
this.input.keyboard.on("keydown-TWO", () => {
this.inventorySlots[1].tint = 0xffff00
this.inventorySlots[0].tint = 0xfffff
})
this.refresh()
}
updateSelected() {
for (let i= 0; i < this.maxColumns; i++) {
this.inventorySlots[i].tint = this.inventory.selected === i ? 0xffff00 : 0xfffff
}
}
}