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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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