Jump to content

"Shaking" a sprite


Recommended Posts


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.



Link to comment
Share on other sites

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 :-)

Link to comment
Share on other sites


  • Recently Browsing   0 members

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