I’m working on a little project using Phaser 3 and I’ve run into a problem. I have two sprites that are interactable, and I want it to change the player state depending on which is pressed. The items don’t move, I think of them as button that are set in another scene that is being launched.
So far my attempts have yielded only more questions about the logic.
export let ToolSelectScene = new Phaser.Class({
Extends: Phaser.Scene,
initialize: function (config) {
Phaser.Scene.call(this, { key: "ToolSelectScene" })
},
init: () => {
console.log("tool select scene initialized")
},
preload: function () {
this.load.multiatlas('items', 'assets/items.json', 'assets');
},
create: function () {
this.gameScene = this.scene.get("GameScene")
this.player = this.gameScene.players.getChildren()[0]
this.weaponSlot1 = this.add.sprite(120,37,"items", "swordsprite.png").setInteractive()
this.weaponSlot2 = this.add.sprite(150,37,"items", "firesprite.png").setInteractive()
this.input.on("gameobjectdown", (pointer, object) => {
console.log(object.frame.name)
if(object.frame.name == "swordsprite.png") {
this.player.playerStateMachine.transition("useSword", object)
}
if (object.frame.name == "firesprite.png") {
console.log("use fire")
this.player.playerStateMachine.transition("useFire", object)
}
})
},
update: function () {
}
})
So when I run the code, the state changes but then something funny happens, the first state continues to be active while the second state has started. I’ve implemented a state machine, originally had written it like this but all in state code, and using the rexUI plugins.
I have state machines for each entity, which handles all the animation and input.
How can I get this work properly?
I wrote this post on stackoverflow, but I thought I’d post it here as well.
If anyone has any advice on how to do this or any critiques and hints, I’d very much appreciate it.