Created
June 21, 2015 15:11
-
-
Save girasquid/5d3aa745544f61fe168a to your computer and use it in GitHub Desktop.
Simple Starling screenshake implementation - heavily cribbed from https://github.com/mikesoylu/fortia/blob/9347105789c2ac9e82109efab4ac44fa3511a7cf/com/mikesoylu/fortia/fScene.as#L89-L102
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package { | |
import starling.core.Starling; | |
import starling.animation.Transitions; | |
import starling.animation.Tween; | |
public class Utils { | |
// This function accepts an originalX and originalY so that you can shake things that | |
// are on screen but not necessarily originally located at 0, 0 (compared to the original | |
// implementation which only ever moved things back to 0, 0) | |
public static function screenShake(drawable:Shakeable, shakeDuration:Number, intensity:Number, originalX:Number, originalY:Number):void { | |
drawable.x = originalX + (Math.random() * intensity - intensity / 2); | |
drawable.y = originalY + (Math.random() * intensity - intensity / 2); | |
if(drawable.tween != null) { | |
Starling.juggler.remove(drawable.tween); | |
} | |
var t:Tween = new Tween(drawable, shakeDuration, Transitions.EASE_OUT); | |
t.moveTo(originalX, originalY); | |
drawable.tween = t; | |
Starling.juggler.add(drawable.tween); | |
} | |
} | |
} | |
import starling.animation.Tween; | |
import starling.display.Sprite; | |
class Shakeable extends Sprite { | |
public var tween:Tween; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment