Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- * GMovieClipExtended
- *
- * Extends GMovieClip to add reverse playing and repeat counter.
- * Considerations:
- * - reverseAndPlay takes priority over repeatable.
- * - repeatCount represents 1 entire timeline, so use n*2 in reverseAndPlay.
- *
- * Example:
- *
- * var mc: GMovieClipExtended = GNodeFactory.createNodeWithComponent(GMovieClipExtended) as GMovieClipExtended ;
- * mc.setTextureAtlas(someAtlas);
- * Genome2D.getInstance().root.addChild(explosion_mc.node);
- * mc.frames = ['frame1', 'frame2', 'frame3'] ;
- * mc.reverseAndPlay = true ;
- * mc.repeatCount = 2 ; // 1 in-out animation
- *
- */
- package {
- import com.genome2d.components.renderables.GMovieClip;
- import com.genome2d.core.GNode;
- import com.genome2d.g2d;
- use namespace g2d ;
- public class GMovieClipExtended extends GMovieClip{
- public var reverseAndPlay:Boolean = false ;
- public var repeatCount:int = 1 ;
- private var _playDirection: int = 1 ;
- private var _repeatCounter:int = 0 ;
- public function GMovieClipExtended(pNode:GNode) {
- super(pNode);
- }
- override public function update(p_deltaTime:Number, p_parentTransformUpdate:Boolean, p_parentColorUpdate:Boolean):void {
- if(!cTexture) return ;
- if(_bPlaying){
- _nAccumulatedTime += p_deltaTime ;
- if( _nAccumulatedTime >= _nSpeed ){
- // new frame.
- _iCurrentFrame += _nAccumulatedTime/_nSpeed * _playDirection ;
- var lastFrame: Boolean = false ;
- if( reverseAndPlay ){
- if( _iCurrentFrame < _iFrameIdsLength-1 && _iCurrentFrame > 0 ){
- _iCurrentFrame %= _iFrameIdsLength ;
- } else {
- lastFrame = true ;
- }
- } else {
- if( _iCurrentFrame < _iFrameIdsLength ){
- _iCurrentFrame %= _iFrameIdsLength ;
- } else {
- lastFrame = true ;
- }
- }
- if( lastFrame ){
- if ( repeatCount > 0 && ++_repeatCounter >= repeatCount ) {
- _repeatCounter = 0 ;
- _iCurrentFrame = _playDirection > 0 ? _iFrameIdsLength-1 : 0 ;
- _playDirection = 1 ;
- _bPlaying = false ;
- } else {
- if( reverseAndPlay ) {
- _playDirection *= -1;
- } else if( repeatable ){
- _iCurrentFrame = 0 ;
- }
- }
- }
- cTexture = _cTextureAtlas.getTexture(_aFrameIds[_iCurrentFrame]);
- }
- _nAccumulatedTime %= _nSpeed ;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment