Advertisement
Guest User

Untitled

a guest
Jun 29th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package zpartan.generic.views;
  2.  
  3. import zpartan.generic.views.SimpleView;
  4. import flash.display.Sprite;
  5. import flash.display.MovieClip;
  6. import flash.geom.Rectangle;
  7. import flash.geom.Point;
  8. import flash.events.Event;
  9. import flash.display.Bitmap;
  10. import flash.display.BitmapData;
  11.  
  12.  
  13. // hsl signal classes
  14. import hsl.haxe.Signaler;
  15. import hsl.haxe.direct.DirectSignaler;
  16.  
  17.  
  18.  
  19. class CacheTimelineClipView2
  20. {
  21.    
  22.    
  23.     private var _scope:             Sprite;
  24.     private var _nom:               String;
  25.     private var _view:              SimpleView;
  26.     private var _mc:                MovieClip;
  27.     private var _mcPreCache:        MovieClip;
  28.     private var _listCacheClips:    List<Sprite>;
  29.     private var _currCacheClip:     Sprite;
  30.     private var _cacheItter:        Iterator<Sprite>;
  31.     private var _firstTime:         Bool;
  32.     private var _toggle:            Bool;
  33.     private var _frame:             Int;
  34.    
  35.     public var cachedSignaler(      default, null):     Signaler<Void>;
  36.    
  37.     public function new( scope_: Sprite, nom_: String )
  38.     {
  39.        
  40.         _scope              = scope_;
  41.         _nom                = nom_;
  42.         cachedSignaler      = new DirectSignaler( this, false );
  43.        
  44.         init();
  45.        
  46.     }
  47.    
  48.    
  49.     private function init()
  50.     {
  51.        
  52.         _toggle                     = false;
  53.         _firstTime                  = true;
  54.         _view                       = new SimpleView( _scope );
  55.         _mc                         = _view.addMovieClip( _nom );
  56.         _mc.gotoAndStop( _mc.totalFrames );
  57.         _mc.visible                 = false;
  58.        
  59.         _listCacheClips             = new List();
  60.         _mcPreCache                 = new MovieClip();
  61.         _mcPreCache.x               = _mc.x;
  62.         _mcPreCache.y               = _mc.y + 20;
  63.         //_mcPreCache.alpha           = 0.2;
  64.         _frame                      = 1;
  65.     }
  66.    
  67.    
  68.     public function startCaching()
  69.     {
  70.        
  71.         //_bgPreCache.cacheAsBitmap   = true;
  72.         //_mc.play();
  73.         _mc.gotoAndStop(1);
  74.         _mc.addEventListener( Event.ENTER_FRAME, cacheFrames );
  75.        
  76.     }
  77.    
  78.    
  79.     private function cacheFrames( e: Event )
  80.     {
  81.        
  82.         var cacheSprite: Sprite;
  83.         trace( _mc.currentFrame );
  84.         if( _mc.currentFrame == 1 && _firstTime == false )
  85.         {
  86.            
  87.             _mc.removeEventListener( Event.ENTER_FRAME, cacheFrames );
  88.             _currCacheClip = _listCacheClips.first();
  89.             switchBgToCache();
  90.             cachedSignaler.dispatch();
  91.            
  92.         }
  93.         else
  94.         {
  95.            
  96.             _firstTime                  = false;
  97.             cacheSprite                 = new Sprite();
  98.             if( _mc.currentFrame == 1 )
  99.             {
  100.                
  101.                 _scope.addChildAt( _mcPreCache, 1 );
  102.                
  103.             }
  104.             else
  105.             {
  106.                
  107.                 cacheSprite.visible     = false;
  108.                
  109.             }
  110.            
  111.             cacheSprite.addChild( new Bitmap( copyToBitmap( _mc ) ) );
  112.             _listCacheClips.add( cacheSprite );
  113.             _mcPreCache.addChild( cacheSprite );
  114.            
  115.             _frame++;
  116.             trace( _frame );
  117.             if( _frame > _mc.totalFrames ){ _frame = 1; }
  118.             _mc.gotoAndStop( _frame );
  119.            
  120.            
  121.         }
  122.        
  123.     }
  124.    
  125.    
  126.     private function switchBgToCache()
  127.     {
  128.        
  129.         _scope.removeChild( _mc );
  130.        
  131.         _mc                     = null;
  132.         _currCacheClip.visible  = false;
  133.         _cacheItter             = _listCacheClips.iterator();
  134.         _currCacheClip          = _cacheItter.next();
  135.         _currCacheClip.visible  = true;
  136.        
  137.         _mcPreCache.addEventListener( Event.ENTER_FRAME, loopCacheFrames );
  138.        
  139.     }
  140.    
  141.    
  142.     private function loopCacheFrames( e: Event )
  143.     {
  144.         if( _toggle == true )
  145.         {
  146.             _currCacheClip.visible  = false;
  147.        
  148.             if( _cacheItter.hasNext() == false )
  149.             {
  150.                 _cacheItter         = _listCacheClips.iterator();
  151.             }
  152.        
  153.             _currCacheClip          = _cacheItter.next();
  154.             _currCacheClip.visible  = true;
  155.            
  156.         }
  157.         _toggle = !_toggle;
  158.        
  159.     }
  160.    
  161.    
  162.     public function copyToBitmap( mc: MovieClip ): BitmapData
  163.     {
  164.        
  165.         mc.cacheAsBitmap                = true;
  166.        
  167.         var wide:       Int             = Std.int( mc.width );
  168.         var hi:         Int             = Std.int( mc.height );
  169.         var point:      Point           = new Point( 0, 0);
  170.         var rect:       Rectangle       = new Rectangle( 0 , 0, wide, hi);
  171.         var abitmap:    BitmapData      = new BitmapData( wide, hi, false, 0x000000 );
  172.         abitmap.draw( mc );
  173.         abitmap.copyPixels( abitmap, rect, point, abitmap, point, false );
  174.        
  175.         return abitmap;
  176.        
  177.     }
  178.    
  179.    
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement