Jump to content

Optimal way run an asynchronous loop with a delay?


WayardEmcee
 Share

Recommended Posts

So basically I have an object called MoveManager. It gets instantiated and assigned a sprite and it moves that sprite based on blah blah blah parameters independently of all other MoveManagers. Now, I want to sprite to move at a certain rate say +32 x every 500ms. What would be the optimal solution (I presume using timer for shooting out events) to move the sprite in the aforementioned way. I'm still kind of struggling to comprehend javascript's ambiguity since I'm used to more strict languages.

Link to comment
Share on other sites

Use a tween.

this.game.add.tween(sprite).to({ x: destination }, Phaser.Easing.Linear.None, 1000, true);

This will only run for 1 second and move property x to destination. Also increase the x property every frame linearly.

 

So if you want fine grained control, use timer.

this.game.time.events.loop(500, moveSpriteBy32, this);// this.game.time.events.add will trigger once// this.game.time.events.loop will trigger forever// this.game.time.events.repeat will trigger any number of times
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...