Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

update for tween system

  • Loading branch information...
commit 382915d068a89446e62c30c9a3f2aef10c4c5ba5 1 parent 503a957
@Beeblerox Beeblerox authored
View
10 src/org/flixel/FlxBasic.hx
@@ -66,7 +66,6 @@ class FlxBasic
alive = true;
ignoreDrawDebug = false;
- //autoClear = false;
autoClear = true;
}
@@ -79,7 +78,7 @@ class FlxBasic
{
if (autoClear && hasTween)
{
- clearTweens();
+ clearTweens(true);
_tween = null;
}
}
@@ -187,7 +186,7 @@ class FlxBasic
return t;
}
- public function removeTween(t:FlxTween):FlxTween
+ public function removeTween(t:FlxTween, ?destroy:Bool = false):FlxTween
{
var ft:FriendTween = t;
if (ft._parent != this)
@@ -211,11 +210,12 @@ class FlxBasic
}
ft._next = ft._prev = null;
ft._parent = null;
+ if (destroy) t.destroy();
t.active = false;
return t;
}
- public function clearTweens():Void
+ public function clearTweens(?destroy:Bool = false):Void
{
var t:FlxTween;
var ft:FriendTween = _tween;
@@ -223,7 +223,7 @@ class FlxBasic
while (ft != null)
{
fn = ft._next;
- removeTween(cast(ft, FlxTween));
+ removeTween(cast(ft, FlxTween), destroy);
ft = fn;
}
}
View
2  src/org/flixel/FlxG.hx
@@ -1751,7 +1751,7 @@ class FlxG
*/
public static function tween(object:Dynamic, values:Dynamic, duration:Float, ?options:Dynamic = null):MultiVarTween
{
- var type:TweenType = TweenType.OneShot,
+ var type:Int = FlxTween.ONESHOT,
complete:CompleteCallback = null,
ease:EaseFunction = null,
tweener:FlxBasic = FlxG.tweener;
View
99 src/org/flixel/tweens/FlxTween.hx
@@ -3,13 +3,6 @@ package org.flixel.tweens;
import org.flixel.tweens.util.Ease;
import org.flixel.FlxBasic;
-enum TweenType
-{
- Persist;
- Looping;
- OneShot;
-}
-
typedef CompleteCallback = Void->Void;
/**
@@ -26,6 +19,31 @@ typedef FriendTween = {
class FlxTween
{
+ /**
+ * Persistent Tween type, will stop when it finishes.
+ */
+ public static inline var PERSIST:Int = 1;
+
+ /**
+ * Looping Tween type, will restart immediately when it finishes.
+ */
+ public static inline var LOOPING:Int = 2;
+
+ /**
+ * "To and from" Tween type, will play tween hither and thither
+ */
+ public static inline var TOANDFRO:Int = 4;
+
+ /**
+ * Oneshot Tween type, will stop and remove itself from its core container when it finishes.
+ */
+ public static inline var ONESHOT:Int = 8;
+
+ /**
+ * Backward Tween type, will play tween in reverse direction
+ */
+ public static inline var BACKWARD:Int = 16;
+
public var active:Bool;
public var complete:CompleteCallback;
@@ -36,17 +54,30 @@ class FlxTween
* @param complete Optional callback for when the Tween completes.
* @param ease Optional easer function to apply to the Tweened value.
*/
- public function new(duration:Float, ?type:TweenType, ?complete:CompleteCallback, ?ease:EaseFunction)
+ public function new(duration:Float, ?type:Int = 0, ?complete:CompleteCallback, ?ease:EaseFunction)
{
_target = duration;
- if (type == null)
+ if (type == 0)
+ {
+ type = FlxTween.PERSIST;
+ }
+ else if (type == FlxTween.BACKWARD)
{
- type = TweenType.Persist;
+ type = FlxTween.PERSIST | FlxTween.BACKWARD;
}
_type = type;
this.complete = complete;
_ease = ease;
_t = 0;
+
+ _backward = (_type & BACKWARD) > 0;
+ }
+
+ public function destroy():Void
+ {
+ complete = null;
+ _parent = null;
+ _ease = null;
}
/**
@@ -56,13 +87,24 @@ class FlxTween
{
_time += FlxG.elapsed;
_t = _time / _target;
- if (_ease != null && _t > 0 && _t < 1)
+ if (_ease != null)
{
_t = _ease(_t);
}
+ if (_backward)
+ {
+ _t = 1 - _t;
+ }
if (_time >= _target)
{
- _t = 1;
+ if (!_backward)
+ {
+ _t = 1;
+ }
+ else
+ {
+ _t = 0;
+ }
_finish = true;
}
}
@@ -80,24 +122,43 @@ class FlxTween
}
active = true;
}
+
+ /**
+ * Immediately stops the Tween and removes it from its Tweener without calling the complete callback.
+ */
+ public function cancel():Void
+ {
+ active = false;
+ if (_parent != null)
+ {
+ _parent.removeTween(this);
+ }
+ }
/** @private Called when the Tween completes. */
private function finish():Void
{
- switch (_type)
+ switch ((_type & ~ FlxTween.BACKWARD))
{
- case Persist:
+ case FlxTween.PERSIST:
_time = _target;
active = false;
- case Looping:
+ case FlxTween.LOOPING:
+ _time %= _target;
+ _t = _time / _target;
+ if (_ease != null && _t > 0 && _t < 1) _t = _ease(_t);
+ start();
+ case FlxTween.TOANDFRO:
_time %= _target;
_t = _time / _target;
if (_ease != null && _t > 0 && _t < 1) _t = _ease(_t);
+ if (_backward) _t = 1 - _t;
+ _backward = !_backward;
start();
- case OneShot:
+ case FlxTween.ONESHOT:
_time = _target;
active = false;
- _parent.removeTween(this);
+ _parent.removeTween(this, true);
}
_finish = false;
if (complete != null) complete();
@@ -110,7 +171,7 @@ class FlxTween
public var scale(getScale, null):Float;
private function getScale():Float { return _t; }
- private var _type:TweenType;
+ private var _type:Int;
private var _ease:EaseFunction;
private var _t:Float;
@@ -121,4 +182,6 @@ class FlxTween
private var _parent:FlxBasic;
private var _prev:FriendTween;
private var _next:FriendTween;
+
+ private var _backward:Bool;
}
View
2  src/org/flixel/tweens/misc/Alarm.hx
@@ -13,7 +13,7 @@ class Alarm extends FlxTween
* @param complete Optional completion callback.
* @param type Tween type.
*/
- public function new(duration:Float, ?complete:CompleteCallback, type:TweenType)
+ public function new(duration:Float, ?complete:CompleteCallback, ?type:Int = 0)
{
super(duration, type, complete, null);
}
View
2  src/org/flixel/tweens/misc/AngleTween.hx
@@ -19,7 +19,7 @@ class AngleTween extends FlxTween
* @param complete Optional completion callback.
* @param type Tween type.
*/
- public function new(?complete:CompleteCallback, ?type:TweenType)
+ public function new(?complete:CompleteCallback, ?type:Int = 0)
{
angle = 0;
super(0, type, complete);
View
2  src/org/flixel/tweens/misc/ColorTween.hx
@@ -24,7 +24,7 @@ class ColorTween extends FlxTween
* @param complete Optional completion callback.
* @param type Tween type.
*/
- public function new(?complete:CompleteCallback, type:TweenType)
+ public function new(?complete:CompleteCallback, ?type:Int = 0)
{
alpha = 1;
super(0, type, complete);
View
2  src/org/flixel/tweens/misc/MultiVarTween.hx
@@ -14,7 +14,7 @@ class MultiVarTween extends FlxTween
* @param complete Optional completion callback.
* @param type Tween type.
*/
- public function new(?complete:CompleteCallback, ?type:TweenType)
+ public function new(?complete:CompleteCallback, ?type:Int = 0)
{
_vars = new Array<String>();
_start = new Array<Float>();
View
2  src/org/flixel/tweens/misc/NumTween.hx
@@ -18,7 +18,7 @@ class NumTween extends FlxTween
* @param complete Optional completion callback.
* @param type Tween type.
*/
- public function new(?complete:CompleteCallback, ?type:TweenType)
+ public function new(?complete:CompleteCallback, ?type:Int = 0)
{
value = 0;
super(0, type, complete);
View
2  src/org/flixel/tweens/misc/VarTween.hx
@@ -13,7 +13,7 @@ class VarTween extends FlxTween
* @param complete Optional completion callback.
* @param type Tween type.
*/
- public function new(?complete:CompleteCallback, ?type:TweenType)
+ public function new(?complete:CompleteCallback, ?type:Int = 0)
{
super(0, type, complete);
}
View
2  src/org/flixel/tweens/motion/CircularMotion.hx
@@ -14,7 +14,7 @@ class CircularMotion extends Motion
* @param complete Optional completion callback.
* @param type Tween type.
*/
- public function new(?complete:CompleteCallback, ?type:TweenType)
+ public function new(?complete:CompleteCallback, ?type:Int = 0)
{
_centerX = _centerY = 0;
_radius = angle = 0;
View
2  src/org/flixel/tweens/motion/CubicMotion.hx
@@ -14,7 +14,7 @@ class CubicMotion extends Motion
* @param complete Optional completion callback.
* @param type Tween type.
*/
- public function new(?complete:CompleteCallback, type:TweenType)
+ public function new(?complete:CompleteCallback, ?type:Int = 0)
{
_fromX = _fromY = _toX = _toY = 0;
_aX = _aY = _bX = _bY = 0;
View
2  src/org/flixel/tweens/motion/LinearMotion.hx
@@ -14,7 +14,7 @@ class LinearMotion extends Motion
* @param complete Optional completion callback.
* @param type Tween type.
*/
- public function new(?complete:CompleteCallback, ?type:TweenType)
+ public function new(?complete:CompleteCallback, ?type:Int = 0)
{
_fromX = _fromY = _moveX = _moveY = 0;
_distance = -1;
View
2  src/org/flixel/tweens/motion/LinearPath.hx
@@ -14,7 +14,7 @@ class LinearPath extends Motion
* @param complete Optional completion callback.
* @param type Tween type.
*/
- public function new(?complete:CompleteCallback, ?type:TweenType)
+ public function new(?complete:CompleteCallback, ?type:Int = 0)
{
_points = new Array<Point>();
_pointD = new Array<Float>();
View
2  src/org/flixel/tweens/motion/Motion.hx
@@ -25,7 +25,7 @@ class Motion extends FlxTween
* @param type Tween type.
* @param ease Optional easer function.
*/
- public function new(duration:Float, ?complete:CompleteCallback, ?type:TweenType, ?ease:EaseFunction = null)
+ public function new(duration:Float, ?complete:CompleteCallback, ?type:Int = 0, ?ease:EaseFunction = null)
{
x = y = 0;
super(duration, type, complete, ease);
View
2  src/org/flixel/tweens/motion/QuadMotion.hx
@@ -18,7 +18,7 @@ class QuadMotion extends Motion
* @param complete Optional completion callback.
* @param type Tween type.
*/
- public function new(?complete:CompleteCallback, type:TweenType)
+ public function new(?complete:CompleteCallback, ?type:Int = 0)
{
_distance = -1;
_fromX = _fromY = _toX = _toY = 0;
View
2  src/org/flixel/tweens/motion/QuadPath.hx
@@ -15,7 +15,7 @@ class QuadPath extends Motion
* @param complete Optional completion callback.
* @param type Tween type.
*/
- public function new(?complete:CompleteCallback, type:TweenType)
+ public function new(?complete:CompleteCallback, ?type:Int = 0)
{
_points = new Array<Point>();
_curve = new Array<Point>();
View
2  src/org/flixel/tweens/sound/Fader.hx
@@ -14,7 +14,7 @@ class Fader extends FlxTween
* @param complete Optional completion callback.
* @param type Tween type.
*/
- public function new(?complete:CompleteCallback, ?type:TweenType)
+ public function new(?complete:CompleteCallback, ?type:Int = 0)
{
super(0, type, complete);
}
View
12 src/org/flixel/tweens/sound/SfxFader.hx
@@ -1,5 +1,6 @@
package org.flixel.tweens.sound;
+import org.flixel.FlxG;
import org.flixel.FlxSound;
import org.flixel.tweens.FlxTween;
import org.flixel.tweens.util.Ease;
@@ -15,7 +16,7 @@ class SfxFader extends FlxTween
* @param complete Optional completion callback.
* @param type Tween type.
*/
- public function new(sfx:FlxSound, ?complete:CompleteCallback, type:TweenType)
+ public function new(sfx:FlxSound, ?complete:CompleteCallback, ?type:Int = 0)
{
super(0, type, finish);
_complete = complete;
@@ -44,12 +45,11 @@ class SfxFader extends FlxTween
/**
* Fades out the Sfx, while also playing and fading in a replacement Sfx.
* @param play The Sfx to play and fade in.
- * @param loop If the new Sfx should loop.
* @param duration Duration of the crossfade.
* @param volume The volume to fade in the new Sfx to.
* @param ease Optional easer function.
*/
- public function crossFade(play:FlxSound, loop:Bool, duration:Float, ?volume:Float = 1, ?ease:EaseFunction = null):Void
+ public function crossFade(play:FlxSound, duration:Float, ?volume:Float = 1, ?ease:EaseFunction = null):Void
{
_crossSfx = play;
_crossRange = volume;
@@ -57,11 +57,7 @@ class SfxFader extends FlxTween
_range = -_start;
_target = duration;
_ease = ease;
- if (loop)
- {
- _crossSfx.loop(0);
- }
- else _crossSfx.play(0);
+ _crossSfx.play(true);
start();
}

0 comments on commit 382915d

Please sign in to comment.
Something went wrong with that request. Please try again.