Jump to content

Wobble a sprite


dhj
 Share

Recommended Posts

Hi,

 

Thanks for reading.

 

I am writing a game where a sprite is drawn onto the screen at a random location, I would like to add a little random movement to the sprite, again I want the movement to be random.

 

I am thinking like a slight wobble (and the size of the wobble could be randomised) or a slight gravitational pull but;

 

I don't want it to move off the screen

I wouldn't mind stopping the sprite from overlapping another

 

Any pointers? The game is actually getting quite complex, but it's these small touches that make good, great!

Link to comment
Share on other sites

Hi, you can try using the Juicy plugin for phaser which has a 'jelly' effect as well as other nice effects: http://www.html5gamedevs.com/topic/6114-plugin-juicy-simple-effects-plugin-based-on-the-juice-it-or-lose-it-presentation/

 

Here's the function from Juicy:

jelly : function(object, strength, delay, initialScale) {  strength = strength || 0.2;  delay = delay || 0;  initialScale = initialScale ||  new Phaser.Point(1, 1);    this.game.add.tween(object.scale).to({x: initialScale.x + (initialScale.x * strength)}, 50, Phaser.Easing.Quadratic.InOut, true, delay)  .to({x: initialScale.x}, 600, Phaser.Easing.Elastic.Out, true);  this.game.add.tween(object.scale).to({y: initialScale.y + (initialScale.y * strength)}, 50, Phaser.Easing.Quadratic.InOut, true, delay + 50)  .to({y: initialScale.y}, 600, Phaser.Easing.Elastic.Out, true);};
Link to comment
Share on other sites

You can easily generate a randomised 'wobble' movement by incrementing a counter variable by a randomly chosen step and passing this to either Math.cos for horizontal motion or Math.sin for vertical (or both). This gives you a number between one and minus one, which you then multiply by your chosen wobble radius and apply as an offset to the sprites start position.

 

Here's an article that looks like it covers some of the relevant material

 

http://www.smashingmagazine.com/2011/10/04/quick-look-math-animations-javascript/

Link to comment
Share on other sites

Yeah some of the coding articles on Smashing Magazine are often pretty good I've found. The first time I really 'got it' about the way Math.sin and Math.cos worked though was when reading a book by Keith Peters, 'Foundation Actionscript, Making Things Move'. It was a real eye opener for me! There is also a javascript edition of the book, it's well worth reading if you get the chance.

 

http://www.apress.com/9781430236658

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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