paulmcf Posted April 27, 2016 Share Posted April 27, 2016 Hi, Is there a straightforward way to "shake" a sprite or button? By "shake" I mean that the object oscillates slightly (horizontally, vertically, or both) and briefly in response to a tap or some other event. Thanks, Paul Link to comment Share on other sites More sharing options...
Tom Atom Posted April 28, 2016 Share Posted April 28, 2016 Hi, you can create your own tweening function. For shaking you can use "wiggle" function: function wiggle(aProgress: number, aPeriod1: number, aPeriod2: number): number { var current1: number = aProgress * Math.PI * 2 * aPeriod1; var current2: number = aProgress * (Math.PI * 2 * aPeriod2 + Math.PI / 2); return Math.sin(current1) * Math.cos(current2); } and call it from your game like this: tweenX.to({ x: this.position.x + aRadius}, aTime, function (k) { return Easing.wiggle(k, aXPeriod1, aXPeriod2); }, true, 0, -1); ... change arguments aXPeriod1 and aXPeriod2 to make different shakes. aRadius gives number of pixels from current x position. To shake in both X and Y directions add another tween for Y with different periods. If set for x and y with different periods it in fact does nice sin/cos spline movement in range of aRadius (can be different for x and y tween). If period is high and time short, then it makes fast shake. Nice thing is, that this shake looks more natural than just random offsetting. If time is long then you can have for example randomly moving dust particles. See http://sbc.littlecolor.com/woodventure/ - branches are using it, sunrays are using it and dust particles are too using it... even hedgehog's muzzle is using it :-) sbonavida, drhayes, abhivaidya and 1 other 4 Link to comment Share on other sites More sharing options...
end3r Posted April 28, 2016 Share Posted April 28, 2016 I'd recommend checking those two topics on the forums, they may help answer your question. Link to comment Share on other sites More sharing options...
paulmcf Posted April 29, 2016 Author Share Posted April 29, 2016 Many thanks. I think some of the math techniques found in this Smashing Magazine tutorial (which I found through a link from one of the topics mentioned by end3r) will prove to be very useful. Link to comment Share on other sites More sharing options...
Recommended Posts