Skip to content

Instantly share code, notes, and snippets.

@DleanJeans
Created July 16, 2017 02:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DleanJeans/682f3f0236d2b118f98ad7288853f4b3 to your computer and use it in GitHub Desktop.
Save DleanJeans/682f3f0236d2b118f98ad7288853f4b3 to your computer and use it in GitHub Desktop.
ScreenShake for HaxeFlixel
package systems.screen;
import flixel.FlxG;
import flixel.FlxObject;
import flixel.tweens.FlxEase;
import flixel.tweens.FlxTween;
import objects.Ball;
import objects.Wall;
class ScreenShake {
public var power:Float = 0.025;
private var _tween:FlxTween;
public function new() {}
public function ball(ball:Ball, object:FlxObject) {
shake(ball.velocity.x * power, -ball.velocity.y * power);
}
public function shake(powerX:Float, powerY:Float) {
if (_tween != null && !_tween.finished)
_tween.cancel();
_tween = tweenCameraScroll(FlxG.camera.scroll.x + powerX, FlxG.camera.scroll.y + powerY).then(tweenCameraScroll(0, 0));
}
private function tweenCameraScroll(toX:Float, toY:Float):FlxTween {
return FlxTween.tween(FlxG.camera.scroll, { x:toX, y:toY }, Settings.duration.screenShake);
}
private function runFor1Loop(tween:FlxTween) {
if (tween.executions == 2) {
tween.cancel();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment