Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package zpartan.generic.views;
- import zpartan.generic.views.SimpleView;
- import flash.display.Sprite;
- import flash.display.MovieClip;
- import flash.geom.Rectangle;
- import flash.geom.Point;
- import flash.events.Event;
- import flash.display.Bitmap;
- import flash.display.BitmapData;
- // hsl signal classes
- import hsl.haxe.Signaler;
- import hsl.haxe.direct.DirectSignaler;
- class CacheTimelineClipView2
- {
- private var _scope: Sprite;
- private var _nom: String;
- private var _view: SimpleView;
- private var _mc: MovieClip;
- private var _mcPreCache: MovieClip;
- private var _listCacheClips: List<Sprite>;
- private var _currCacheClip: Sprite;
- private var _cacheItter: Iterator<Sprite>;
- private var _firstTime: Bool;
- private var _toggle: Bool;
- private var _frame: Int;
- public var cachedSignaler( default, null): Signaler<Void>;
- public function new( scope_: Sprite, nom_: String )
- {
- _scope = scope_;
- _nom = nom_;
- cachedSignaler = new DirectSignaler( this, false );
- init();
- }
- private function init()
- {
- _toggle = false;
- _firstTime = true;
- _view = new SimpleView( _scope );
- _mc = _view.addMovieClip( _nom );
- _mc.gotoAndStop( _mc.totalFrames );
- _mc.visible = false;
- _listCacheClips = new List();
- _mcPreCache = new MovieClip();
- _mcPreCache.x = _mc.x;
- _mcPreCache.y = _mc.y + 20;
- //_mcPreCache.alpha = 0.2;
- _frame = 1;
- }
- public function startCaching()
- {
- //_bgPreCache.cacheAsBitmap = true;
- //_mc.play();
- _mc.gotoAndStop(1);
- _mc.addEventListener( Event.ENTER_FRAME, cacheFrames );
- }
- private function cacheFrames( e: Event )
- {
- var cacheSprite: Sprite;
- trace( _mc.currentFrame );
- if( _mc.currentFrame == 1 && _firstTime == false )
- {
- _mc.removeEventListener( Event.ENTER_FRAME, cacheFrames );
- _currCacheClip = _listCacheClips.first();
- switchBgToCache();
- cachedSignaler.dispatch();
- }
- else
- {
- _firstTime = false;
- cacheSprite = new Sprite();
- if( _mc.currentFrame == 1 )
- {
- _scope.addChildAt( _mcPreCache, 1 );
- }
- else
- {
- cacheSprite.visible = false;
- }
- cacheSprite.addChild( new Bitmap( copyToBitmap( _mc ) ) );
- _listCacheClips.add( cacheSprite );
- _mcPreCache.addChild( cacheSprite );
- _frame++;
- trace( _frame );
- if( _frame > _mc.totalFrames ){ _frame = 1; }
- _mc.gotoAndStop( _frame );
- }
- }
- private function switchBgToCache()
- {
- _scope.removeChild( _mc );
- _mc = null;
- _currCacheClip.visible = false;
- _cacheItter = _listCacheClips.iterator();
- _currCacheClip = _cacheItter.next();
- _currCacheClip.visible = true;
- _mcPreCache.addEventListener( Event.ENTER_FRAME, loopCacheFrames );
- }
- private function loopCacheFrames( e: Event )
- {
- if( _toggle == true )
- {
- _currCacheClip.visible = false;
- if( _cacheItter.hasNext() == false )
- {
- _cacheItter = _listCacheClips.iterator();
- }
- _currCacheClip = _cacheItter.next();
- _currCacheClip.visible = true;
- }
- _toggle = !_toggle;
- }
- public function copyToBitmap( mc: MovieClip ): BitmapData
- {
- mc.cacheAsBitmap = true;
- var wide: Int = Std.int( mc.width );
- var hi: Int = Std.int( mc.height );
- var point: Point = new Point( 0, 0);
- var rect: Rectangle = new Rectangle( 0 , 0, wide, hi);
- var abitmap: BitmapData = new BitmapData( wide, hi, false, 0x000000 );
- abitmap.draw( mc );
- abitmap.copyPixels( abitmap, rect, point, abitmap, point, false );
- return abitmap;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement