Shalmak Posted November 5, 2018 Share Posted November 5, 2018 I am creating a custom game object, specifically a class that extends Phaser.Physics.Arcade.Sprite. In the preUpdate method I put all the logic that should happen for every game step, and this seems to work fine. But according to the documentation, it seems like this logic should be in the update method. When I add an update method to my class, it never gets called. GameObject's update method doesn't seem to be called either. Can someone please explain how (and why) to override the update method rather then preUpdate? MeiaGaspea 1 Link to comment Share on other sites More sharing options...
B3L7 Posted November 5, 2018 Share Posted November 5, 2018 Unlike Phaser 2, Phaser 3 requires that you call each objects update method. This can be done one of two ways: 1. Add the object to a group which has the property runChildUpdate property set to true. 2. Call the objects update method in the scene's update method: var SceneA = new Phaser.Class({ Extends: Phaser.Scene, initialize: function SceneA () { Phaser.Scene.call(this, { key: 'sceneA' }); }, create: function () { this.customObject = new customObject(x, y, etc); }, update: function (time, delta) { this.customObject.update(time, delta); } }); Olf, AhmedElyamani and Shalmak 2 1 Link to comment Share on other sites More sharing options...
Shalmak Posted November 5, 2018 Author Share Posted November 5, 2018 Got it, thanks. So the remaining question is why - what would be the difference between using update as you suggested versus putting the same logic in preUpdate? Link to comment Share on other sites More sharing options...
B3L7 Posted November 5, 2018 Share Posted November 5, 2018 I've never used the preUpdate method. According to the API: Quote The pre-update step. Handles Game Objects that are pending insertion to and removal from the list. Link to comment Share on other sites More sharing options...
samme Posted November 5, 2018 Share Posted November 5, 2018 I think it's fine to override the preUpdate method if you like. A lot of the examples do this. Nb. It won't work for Game Objects without a preUpdate method (e.g., Image). You have to remember to call the parent class's preUpdate method. preUpdate comes a little earlier than update. For instance, with ArcadeSprite, preUpdate runs right before the physics step whereas update runs right after. For a lot of cases I'd guess it doesn't make a big difference, though. MeiaGaspea, B3L7 and Shalmak 2 1 Link to comment Share on other sites More sharing options...
Shalmak Posted November 5, 2018 Author Share Posted November 5, 2018 8 minutes ago, samme said: preUpdate comes a little earlier than update. For instance, with ArcadeSprite, preUpdate runs right before the physics step whereas update runs right after. For a lot of cases I'd guess it doesn't make a big difference, though. I think it could really help a developer to know more about these steps (like preupdate, physics, update, and anything else) - what each one does, their order etc. Is this documented anywhere? Link to comment Share on other sites More sharing options...
rex Posted November 6, 2018 Share Posted November 6, 2018 Here , and here is a simple note of phaser3's main loop. B3L7, samme and MeiaGaspea 3 Link to comment Share on other sites More sharing options...
Recommended Posts