Jump to content

Random Movement


rvizcaino
 Share

Recommended Posts

This is a difficult one to answer as there are many different ways for something to move randomly! A simple implementation would be to use a tween on a timer, something like this:

var powerup = this.game.add.sprite(0, 0, 'powerup');this.game.time.events.loop(2000, function() {  this.game.add.tween(powerup).to({x: this.game.world.randomX, y: this.game.world.randomY}, 1750, Phaser.Easing.Quadratic.InOut, true);}, this)

Be aware that this is a very simplified solution, and will cause errors if the object is removed. It'd be much better to extend the Sprite object and add this as a behaviour that only occurs when it's alive.

Link to comment
Share on other sites

Yes, the sprites will move in straight lines to random points on the screen. If you want a smoother way of doing this, you could probably randomly tween the body.velocity.x and body.velocity.y values maybe? Like I said, there are lots of ways to do this depending on the type of random movement you want.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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