Jump to content
Search Community

Another bonus to the GreenSock Tween engine: a pausable Timer!

mrkos67 test
Moderator Tag

Recommended Posts

var $time:int = 90;
var twn:TweenMax = TweenMax.to( this, $time, { onUpdate:onUpdate_tl, onComplete:onComplete_tl } );

function onUpdate_tl( evt:TweenEvent = null ):void
{
 mcTimer.time = Math.ceil( $time - twn.currentTime );
}

function onComplete_tl( evt:TweenEvent = null ):void
{
 trace( "timer done!" );
}

btnPlay.addEventListener( MouseEvent.CLICK, onclick_play );
btnPause.addEventListener( MouseEvent.CLICK, onclick_pause );

function onclick_play( evt:MouseEvent ):void
{
 twn.play();
}
function onclick_pause( evt:MouseEvent ):void
{
 twn.pause();
}

 

Works like a champ!

 

Just thought I'd post this in case anybody might want to use it.

Link to comment
Share on other sites

just created a basic class for this:

 


package 
{
import com.greensock.*;
import com.greensock.events.*;
import flash.events.*;

//=======================================================================================================================================
// CLASS: SecondsTimer
//---------------------------------------------------------------------------------------------------------------------------------------
public class SecondsTimer extends EventDispatcher
{
//=====================================================================================================================================
// CONSTS
//-------------------------------------------------------------------------------------------------------------------------------------

//=====================================================================================================================================
// VARS
//-------------------------------------------------------------------------------------------------------------------------------------
protected var _timer :TweenMax;
protected var _$currentTime :int;
protected var _$currentCountdown :int;
protected var _$duration :int;

//=====================================================================================================================================
// GETTERS
//-------------------------------------------------------------------------------------------------------------------------------------
public function get duration () :int { return( _$duration ); }
public function get currentTime () :Number { return( _$currentTime ); }
public function get currentCountdown () :Number { return( _$currentCountdown ); }

//=====================================================================================================================================
// SETTERS
//-------------------------------------------------------------------------------------------------------------------------------------
public function set duration ( $n:int ) :void { _$duration = $n; }

//=====================================================================================================================================
// CONSTRUCTOR & INITS
//-------------------------------------------------------------------------------------------------------------------------------------
public function SecondsTimer( $duration:int = -1 )
{
// init vars
duration = $duration;
}

//=====================================================================================================================================
// PUBLIC ACCESS
//-------------------------------------------------------------------------------------------------------------------------------------
public function start():void
{
if( _$duration > 0 )
_timer = TweenMax.to( this, _$duration, { onUpdate:onUpdate_timer, onComplete:onComplete_timer } );
else
dispatchEvent( new ErrorEvent( ErrorEvent.ERROR, false, false, "Property \"duration\" has not been set." ) );
}
//-------------------------------------------------------------------------------------------------------------------------------------
public function play():void
{
if( _timer )
_timer.play();
else
start();
}
//-------------------------------------------------------------------------------------------------------------------------------------
public function pause():void
{
if( _timer )
_timer.pause();
else
dispatchEvent( new ErrorEvent( ErrorEvent.ERROR, false, false, "Timer is not currently running" ) );
}
//-------------------------------------------------------------------------------------------------------------------------------------
public function stop():void
{
if( _timer )
_timer.kill();
else
dispatchEvent( new ErrorEvent( ErrorEvent.ERROR, false, false, "Timer is not currently running" ) );
}

//=====================================================================================================================================
// HELPERS
//-------------------------------------------------------------------------------------------------------------------------------------
protected function onUpdate_timer( evt:TweenEvent = null ):void
{
var newCurrentTime:int = Math.floor( _timer.currentTime );
if( _$currentTime != newCurrentTime )
{
_$currentTime = newCurrentTime;
_$currentCountdown = _$duration - _$currentTime;
dispatchEvent( new TimerEvent( TimerEvent.TIMER ) );
}
}

//-------------------------------------------------------------------------------------------------------------------------------------
protected function onComplete_timer( evt:TweenEvent = null ):void
{
dispatchEvent( new TimerEvent( TimerEvent.TIMER_COMPLETE ) );
}

}
}

Link to comment
Share on other sites

Thanks for sharing! I'm curious, though, why you wouldn't just use a regular TweenLite instance or a delayedCall() (which is actually just an empty TweenLite with an onComplete) like this:

 

var timer:TweenLite = TweenLite.delayedCall(90, myFunction);
timer.pause();
timer.play();

 

You could get the time whenever you want using the time() method (in v12). Is that missing any functionality that you wanted?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...