rvizcaino Posted November 11, 2014 Share Posted November 11, 2014 Hi! I have a question, maybe a silly one. I want to apply a random movement to a sprite, I want it to move all over the screen, it's for a power up for a space shooter game. Thanks! Link to comment Share on other sites More sharing options...
lewster32 Posted November 12, 2014 Share Posted November 12, 2014 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 More sharing options...
rvizcaino Posted November 14, 2014 Author Share Posted November 14, 2014 Hi Lewster, thanks for answer! This would make item move in straight lines right? Link to comment Share on other sites More sharing options...
lewster32 Posted November 14, 2014 Share Posted November 14, 2014 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 More sharing options...
Recommended Posts