Skip to content

Commit 82eb2a5

Browse files
cyn0x8Geokureli
andcommitted
tween framerate
Co-Authored-By: George Kurelic <Gkurelic@gmail.com>
1 parent f6a98e3 commit 82eb2a5

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

flixel/tweens/FlxTween.hx

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,7 @@ class FlxTween implements IFlxDestroyable
505505
public var active(default, set):Bool = false;
506506
public var duration:Float = 0;
507507
public var ease:EaseFunction;
508+
public var framerate:Float;
508509
public var onStart:TweenCallback;
509510
public var onUpdate:TweenCallback;
510511
public var onComplete:TweenCallback;
@@ -562,6 +563,7 @@ class FlxTween implements IFlxDestroyable
562563
onUpdate = Options.onUpdate;
563564
onComplete = Options.onComplete;
564565
ease = Options.ease;
566+
framerate = Options.framerate != null ? Options.framerate : 0;
565567
setDelays(Options.startDelay, Options.loopDelay);
566568
this.manager = manager != null ? manager : globalManager;
567569
}
@@ -619,13 +621,23 @@ class FlxTween implements IFlxDestroyable
619621

620622
function update(elapsed:Float):Void
621623
{
624+
var preTick:Float = _secondsSinceStart;
622625
_secondsSinceStart += elapsed;
626+
var postTick:Float = _secondsSinceStart;
627+
623628
var delay:Float = (executions > 0) ? loopDelay : startDelay;
624629
if (_secondsSinceStart < delay)
625630
{
626631
return;
627632
}
628-
scale = Math.max((_secondsSinceStart - delay), 0) / duration;
633+
634+
if (framerate > 0)
635+
{
636+
preTick = Math.fround(preTick * framerate) / framerate;
637+
postTick = Math.fround(postTick * framerate) / framerate;
638+
}
639+
640+
scale = Math.max((postTick - delay), 0) / duration;
629641
if (ease != null)
630642
{
631643
scale = ease(scale);
@@ -647,7 +659,7 @@ class FlxTween implements IFlxDestroyable
647659
}
648660
else
649661
{
650-
if (onUpdate != null)
662+
if (postTick > preTick && onUpdate != null)
651663
onUpdate(this);
652664
}
653665
}
@@ -919,6 +931,12 @@ typedef TweenOptions =
919931
*/
920932
@:optional var ease:EaseFunction;
921933

934+
/**
935+
* Optional set framerate for this tween to update at.
936+
* This also affects how often `onUpdate` is called.
937+
*/
938+
@:optional var framerate:Null<Float>;
939+
922940
/**
923941
* Optional start callback function.
924942
*/

flixel/tweens/misc/FlickerTween.hx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ typedef FlickerTweenOptions =
2323
*/
2424
@:optional var ease:EaseFunction;
2525

26+
/**
27+
* Optional set framerate for this tween to update at.
28+
* This also affects how often `onUpdate` is called.
29+
* Not to be confused with `period` for flickering.
30+
*/
31+
@:optional var framerate:Null<Float>;
32+
2633
/**
2734
* Optional start callback function.
2835
*/

tests/unit/src/flixel/tweens/FlxTweenTest.hx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import flixel.FlxBasic;
44
import flixel.FlxG;
55
import flixel.math.FlxPoint;
66
import flixel.tweens.FlxTween.TweenCallback;
7+
import flixel.tweens.FlxTween.TweenOptions;
78
import flixel.util.FlxTimer;
89
import massive.munit.Assert;
910

@@ -432,6 +433,35 @@ class FlxTweenTest extends FlxTest
432433
Assert.isTrue(tween3Updated);
433434
}
434435

436+
function assertTweenUpdates(duration:Float, ?fps:Float, ?options:TweenOptions, expected:Int)
437+
{
438+
var updates = 0;
439+
440+
if (options == null)
441+
options = {};
442+
443+
if (fps != null)
444+
options.framerate = fps;
445+
446+
options.onUpdate = function(_) updates++;
447+
options.onComplete = function(_) Assert.areEqual(expected, updates);
448+
FlxTween.tween({value: 0.0}, {value: 1}, 2.0, options);
449+
}
450+
451+
@Test
452+
function testTweenFramerate()
453+
{
454+
assertTweenUpdates(2.0, null, null, 60);
455+
assertTweenUpdates(2.0, 0, null, 60);
456+
assertTweenUpdates(2.0, 5, null, 10);
457+
assertTweenUpdates(2.0, 10, {startDelay: 0.5}, 20);
458+
assertTweenUpdates(2.0, 10.1, null, 21);
459+
assertTweenUpdates(2.0, 29.9, null, 60);
460+
assertTweenUpdates(2.0, 100, null, 60);
461+
462+
step(FlxG.updateFramerate * 3); // 3 full seconds
463+
}
464+
435465
function makeTween(duration:Float, onComplete:TweenCallback, ?onUpdate:TweenCallback):FlxTween
436466
{
437467
var foo = {f: 0};

0 commit comments

Comments
 (0)