-
Notifications
You must be signed in to change notification settings - Fork 460
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add FlxTween.flicker #3086
Merged
Merged
add FlxTween.flicker #3086
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package flixel.tweens.misc; | ||
|
||
import flixel.FlxBasic; | ||
import flixel.tweens.FlxTween; | ||
|
||
/** | ||
* Special tween options for flicker tweens | ||
* @since 5.7.0 | ||
*/ | ||
typedef FlickerTweenOptions = TweenOptions & | ||
{ | ||
/** | ||
* Whether the object will show after the tween, defaults to `true` | ||
*/ | ||
?endVisibility:Bool, | ||
|
||
/** | ||
* The amount of time the object will show, compared to the total duration, The default is `0.5`, | ||
* meaning equal times visible and invisible. | ||
*/ | ||
?ratio:Float, | ||
|
||
/** | ||
* An optional custom flicker function, defaults to | ||
* `function (tween) { return (tween.time / tween.period) % 1 > tween.ratio; }` | ||
*/ | ||
?tweenFunction:(FlickerTween)->Bool | ||
}; | ||
|
||
/** | ||
* Flickers an object. See `FlxTween.flicker()` | ||
* @since 5.7.0 | ||
*/ | ||
class FlickerTween extends FlxTween | ||
{ | ||
/** The object being flickered */ | ||
public var basic(default, null):FlxBasic; | ||
|
||
/** Controls how the object flickers over time */ | ||
public var tweenFunction(default, null):(FlickerTween)->Bool; | ||
|
||
/** Whether the object will show after the tween, defaults to `true` */ | ||
public var endVisibility(default, null):Bool = true; | ||
|
||
/** How often, in seconds, the visibility cycles */ | ||
public var period(default, null):Float = 0.08; | ||
|
||
/** | ||
* The ratio of time the object will show, default is `0.5`, | ||
* meaning equal times visible and invisible. | ||
*/ | ||
public var ratio(default, null):Float = 0.5; | ||
|
||
function new(options:FlickerTweenOptions, ?manager:FlxTweenManager):Void | ||
{ | ||
tweenFunction = defaultTweenFunction; | ||
if (options != null) | ||
{ | ||
if (options.endVisibility != null) | ||
endVisibility = options.endVisibility; | ||
|
||
if (options.ratio != null) | ||
ratio = options.ratio; | ||
|
||
if (options.tweenFunction != null) | ||
tweenFunction = options.tweenFunction; | ||
} | ||
|
||
super(options, manager); | ||
} | ||
|
||
/** | ||
* Clean up references | ||
*/ | ||
override function destroy() | ||
{ | ||
super.destroy(); | ||
basic = null; | ||
} | ||
|
||
/** | ||
* Flickers the desired object | ||
* | ||
* @param basic The object to flicker | ||
* @param duration Duration of the tween, in seconds | ||
* @param period How often, in seconds, the visibility cycles | ||
*/ | ||
public function tween(basic:FlxBasic, duration:Float, period:Float):FlickerTween | ||
{ | ||
this.basic = basic; | ||
this.duration = duration; | ||
|
||
start(); | ||
return this; | ||
} | ||
|
||
override function update(elapsed:Float):Void | ||
{ | ||
super.update(elapsed); | ||
|
||
if (tweenFunction != null && _secondsSinceStart >= _delayToUse) | ||
{ | ||
final visible = tweenFunction(this); | ||
// do not call setter every frame | ||
if (basic.visible != visible) | ||
basic.visible = visible; | ||
} | ||
} | ||
|
||
override function onEnd() | ||
{ | ||
super.onEnd(); | ||
|
||
basic.visible = endVisibility; | ||
} | ||
|
||
override function isTweenOf(object:Dynamic, ?field:String):Bool | ||
{ | ||
return basic == object && (field == null || field == "visible" || field == "flicker"); | ||
} | ||
|
||
/** | ||
* The default tween function of flicker tweens | ||
* @param tween The tween handling the flickering | ||
*/ | ||
public static function defaultTweenFunction(tween:FlickerTween) | ||
{ | ||
return (tween.time / tween.period) % 1 > tween.ratio; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was curious how you approached handling the ratio. This is pleasantly simple 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. at first I had |
||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The rest of the functions here have explicit returns types on the signature. What is the flixel guideline for return types in function signatures?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there is no rule on this, I kinda prefer not having it, in more cases, especially here where
tweenFunction
is well defined