Hi All,
I am trying to create a stopwatch component by extending Phaser’s Container class. I ran into a problem while trying to modify properties of individual game objects within the Container.
Specifically, changing an object property (e.g. angle of a dial needle) works as expected when called from the Container’s constructor, but not from the container’s update function, which is triggered within a scene’s update function. In the latter case, the dial needle appears the same, even though its angle property is actually changed when console logged.
Below is a simplified code excerpt:
class Stopwatch extends Phaser.GameObjects.Container {
constructor(sceneObj, x, y) {
super(sceneObj, x, y)
...
this._addObjects()
}
_addObjects() { // called from the container's constructor
this.objects = {
stopwatch: this.sceneObj.add.image(0, 0, "stopwatch"),
dial_needle: this.sceneObj.add.image(0, 0, "dial_needle")
}
const { stopwatch, dial_needle } = this.objects
this.add([stopwatch, dial_needle, this.stopwatchGraphics])
setTimeout(() => dial_needle.setAngle(70), 4000) // this works!
}
update() { // triggered within scene's update function
...
if (currentTimestamp - lastTimestamp > timeDelta) {
let newAngle = needleAngle + angleDelta * actualTimeDelta
if (newAngle >= 360) {
// console log here shows objects with updated properties
this.emit('stopwatch-done')
return
}
this.objects.dial_needle.setAngle(newAngle)
// console log here shows that "dial_needle" angle is changed,
// but this doesn't reflect on the object's appearance on screen
}
}
...
}
I could perhaps avoid this issue by extending BaseGameScene instead of the Container and using the update function to move all objects in unison if needed, but I am interested in why my current approach fails. I am guessing it might have something to do with Display Lists of Container and Scene, but I hadn’t been able to figure out the exact cause so far.