Is there an Update Event I can give to an individual gameObject?

Hi! I come from a Unity background, where I can define looping behaviors in anUpdate() function, and it gets called every frame. Is there an equivalent way of doing this in Phaser?

I’m familiar with using on() to listen for, say, mouse clicks, but is there an “update” event that gets called every frame?

There is a scene system called the Update List, which calls the preUpdate method on all items in it. If you’re using a custom class for your Game Objects, they will automatically get added to the Update List by scene.add.existing if they have a preUpdate method.

If you want to manually add a Game Object to the Update List, you can get it with this.sys.updateList, then use its add method. Note that this will result in an error if you don’t first add a preUpdate function to the Game Object (something like object.preUpdate = function(){...}), so I don’t recommend it.

If you want to update your Game Object when your Scene updates, you can either manually update it from the Scene’s update method (this would involve calling a method on the Game Object or even emitting a custom event with object.emit).

If you have a custom class and you don’t want it to depend on the Scene, you can use the Scene’s UPDATE event (called before update) or POST_UPDATE event (called after update). Of course, you should make absolutely sure you clean up the event listener when your Game Object gets destroyed.

1 Like

That’s a lot of options.

  1. Are there best practices when using updateList?
  2. What happens to anything created with this.add.image(...) if there’s no assignment?

Generally, you shouldn’t touch it directly. As I said in my previous post, it could easily break if you add the wrong objects to it and the errors you’d get would be hard to trace if you don’t expect them. It’s a better idea to use custom classes for more complicated Game Objects and let Phaser manage it.

The methods in the Game Object Factory (which is what scene.add is) create the respective Game Object through its constructor (new Phaser.GameObjects.Image(scene, ...)), then add it to the Scene. Usually, this means adding it to the Display List (scene.children or scene.sys.displayList) and sometimes to the Update List. Assigning the created Game Object to a variable simply gives you an easy way to access and identify it in the future from your own code. If you don’t need to do anything with the Game Object, it’s perfectly safe to just add it without storing it anywhere.

Are images meant to have update attached to them, or is that outside the scope of their purpose?

Basically, I have a for loop generating a grid of unassigned images, and I want them to rotate slowly, forever. I thought this might be the way to go but I’m also still super new to Phaser

Images don’t have an update method by default because they don’t need one to provide their functionality. If you want to extend it, you can absolutely add an update method to them. Don’t get confused by their name; they’re called “images” solely because they display a single frame of their texture, without support for animation (Sprites, on the other hand, can animate, which is the only way they differ from an Image).

In your case, the easiest solution is to store the Images in an array, then loop through that array in your Scene’s update method to change their rotation. Creating your own class would work, too, but it’s needlessly complicated (unless, of course, you’re planning to give them more functionality).

1 Like