Created
May 18, 2017 15:33
-
-
Save JoeCreates/b3dc48a20a93dd1d69f8537d8dbf84f5 to your computer and use it in GitHub Desktop.
Lycan CinematicText example
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 text; | |
import config.ColorPresets; | |
import flixel.text.FlxText.FlxTextBorderStyle; | |
import flixel.tweens.FlxEase; | |
import flixel.tweens.FlxTween; | |
import lycan.ui.CinematicText; | |
class MyCinematicText extends CinematicText { | |
private var tweens:Array<FlxTween>; | |
public function new(x:Float, y:Float, text:String, size:Int = 24, spacing:Float = 0, font:String, autoHide:Bool = false) { | |
super(x, y, text, size, spacing, font, autoHide, function(char:String, size:Int, font:String):CinematicLetter { | |
var letter = new CinematicLetter(char, size, font); | |
letter.pixelPerfectRender = false; | |
letter.setBorderStyle(FlxTextBorderStyle.SHADOW, ColorPresets.BLACK, 2, 1); | |
return letter; | |
}); | |
tweens = new Array<FlxTween>(); | |
} | |
public static inline function customEaseElasticOut(t:Float):Float { | |
var amp:Float = 2; | |
var period:Float = 0.4; | |
return amp * Math.pow(2, -10 * t) * Math.sin((t - (period / (2 * Math.PI) * Math.asin(1 / amp))) * (2 * Math.PI) / period) + 1; | |
} | |
public override function showLetter(letter:CinematicLetter, index:Int):Void { | |
var targetScaleX:Float = letter.scale.x; | |
var targetScaleY:Float = letter.scale.y; | |
letter.scale.set(0.1, 0.1); | |
letter.alpha = 0; | |
var delay:Float = 0.1 * index; | |
var letterTween = FlxTween.tween(letter, { alpha: 1 }, 0.2, {startDelay: delay}); | |
var scaleTween = FlxTween.tween(letter.scale, { x: targetScaleX, y: targetScaleY }, 0.5, { startDelay: delay, ease: MyCinematicText.customEaseElasticOut, onComplete: function(tween:FlxTween) { | |
if (autoHide) { | |
hideLetter(letter, 0); | |
} | |
if (index >= letters.length - 1) { | |
finishedShowing = true; | |
} | |
}}); | |
tweens.push(letterTween); | |
tweens.push(scaleTween); | |
} | |
public override function hideLetter(letter:CinematicLetter, index:Int):Void { | |
var displayTime:Float = 0.85; // Time each character is shown before vanishing | |
var delay:Float = displayTime + 0.1 * index; | |
var letterTween = FlxTween.tween(letter, { alpha: 0 }, 0.5, {startDelay: delay, ease: FlxEase.expoIn}); | |
var scaleTween = FlxTween.tween(letter.scale, { x: 0.1, y: 0.1 }, 0.5, { startDelay: delay, ease: FlxEase.elasticIn, onComplete: function(tween:FlxTween) { | |
if (index >= letters.length - 1) { | |
finishedHiding = true; | |
} | |
}}); | |
tweens.push(letterTween); | |
tweens.push(scaleTween); | |
} | |
public override function destroy():Void { | |
for (tween in tweens) { | |
tween.cancel(); | |
} | |
tweens = []; | |
super.destroy(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment