dhj Posted August 12, 2014 Share Posted August 12, 2014 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 screenI 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 More sharing options...
Hsaka Posted August 12, 2014 Share Posted August 12, 2014 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 More sharing options...
alex_h Posted August 13, 2014 Share Posted August 13, 2014 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/ lewster32 1 Link to comment Share on other sites More sharing options...
lewster32 Posted August 13, 2014 Share Posted August 13, 2014 @alex_h that article is BRILLIANT. Link to comment Share on other sites More sharing options...
alex_h Posted August 13, 2014 Share Posted August 13, 2014 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 lewster32 1 Link to comment Share on other sites More sharing options...
Recommended Posts