public class MyCache extends Sprite
{
private var _clip: Sprite = null ;
public function MyCache(clip: Sprite, framesCount: int )
{
_clip = clip;
_framesCount = framesCount;
_bitmap = new Bitmap( null , PixelSnapping.AUTO, true );
addChild(_bitmap);
mouseEnabled = false ;
}
private var _bitmap: Bitmap = null ;
private var _frames: Array = new Array();
private var _framesCount: int = 0;
private var _currentFrame: int = 0;
private var _onEnd: Function = null ;
private var _loop: Boolean = true ;
public function play(loop: Boolean = true , onEnd: Function = null ): void
{
_currentFrame = 0;
_loop = loop;
_onEnd = onEnd;
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
public function stop(): void
{
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
if (_onEnd != null )
_onEnd( this );
}
private function createCache(clip: Sprite): Object
{
var rect: Rectangle = clip.getRect(clip);
var bmpData: BitmapData = new BitmapData(rect.width, rect.height, true , 0x00000000);
var m: Matrix = new Matrix();
m.translate(-rect.x, -rect.y);
m.scale(clip.scaleX, clip.scaleY);
bmpData.draw(clip, m);
return { "frame" : bmpData, "x" : rect.x, "y" : rect.y };
}
private function cachNextFrame(): void
{
if (_frames.length < _framesCount)
{
_clip.dispatchEvent( new Event(Event.ENTER_FRAME));
_frames.push(createCache(_clip));
}
}
private function onEnterFrame(e: Event): void
{
cachNextFrame();
if (_currentFrame < _frames.length)
{
var bmpData: Object = _frames[_currentFrame];
_bitmap.bitmapData = bmpData.frame;
_bitmap.x = bmpData.x;
_bitmap.y = bmpData.y;
}
else
{
if (_loop)
_currentFrame = 0;
else
stop();
}
_currentFrame++;
}
}
* This source code was highlighted with Source Code Highlighter .
public class MyCache extends Sprite
{
private var _clip: Sprite = null ;
public function MyCache(clip: Sprite, framesCount: int )
{
_clip = clip;
_framesCount = framesCount;
_bitmap = new Bitmap( null , PixelSnapping.AUTO, true );
addChild(_bitmap);
mouseEnabled = false ;
}
private var _bitmap: Bitmap = null ;
private var _frames: Array = new Array();
private var _framesCount: int = 0;
private var _currentFrame: int = 0;
private var _onEnd: Function = null ;
private var _loop: Boolean = true ;
public function play(loop: Boolean = true , onEnd: Function = null ): void
{
_currentFrame = 0;
_loop = loop;
_onEnd = onEnd;
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
public function stop(): void
{
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
if (_onEnd != null )
_onEnd( this );
}
private function createCache(clip: Sprite): Object
{
var rect: Rectangle = clip.getRect(clip);
var bmpData: BitmapData = new BitmapData(rect.width, rect.height, true , 0x00000000);
var m: Matrix = new Matrix();
m.translate(-rect.x, -rect.y);
m.scale(clip.scaleX, clip.scaleY);
bmpData.draw(clip, m);
return { "frame" : bmpData, "x" : rect.x, "y" : rect.y };
}
private function cachNextFrame(): void
{
if (_frames.length < _framesCount)
{
_clip.dispatchEvent( new Event(Event.ENTER_FRAME));
_frames.push(createCache(_clip));
}
}
private function onEnterFrame(e: Event): void
{
cachNextFrame();
if (_currentFrame < _frames.length)
{
var bmpData: Object = _frames[_currentFrame];
_bitmap.bitmapData = bmpData.frame;
_bitmap.x = bmpData.x;
_bitmap.y = bmpData.y;
}
else
{
if (_loop)
_currentFrame = 0;
else
stop();
}
_currentFrame++;
}
}
* This source code was highlighted with Source Code Highlighter .
public class MyCachePool
{
protected function createEffect(): MyCache
{
return null ;
}
private var _pool: Array = new Array();
public function show(owner: Sprite, px: int , py: int , loop: Boolean = true ): void
{
var effect: MyCache = getEffect();
owner.addChild(effect);
effect.x = px;
effect.y = py;
effect.play(loop, onEnd);
}
private function getEffect(): MyCache
{
if (_pool.length > 0)
{
var idx: int = Math.round((Math.random() * (_pool.length - 1)));
var rez: MyCache = _pool[idx];
_pool.splice(idx, 1);
return rez;
}
return createEffect();
}
private function onEnd(effect: MyCache): void
{
_pool.push(effect);
}
}
* This source code was highlighted with Source Code Highlighter .
public class MyCachePool
{
protected function createEffect(): MyCache
{
return null ;
}
private var _pool: Array = new Array();
public function show(owner: Sprite, px: int , py: int , loop: Boolean = true ): void
{
var effect: MyCache = getEffect();
owner.addChild(effect);
effect.x = px;
effect.y = py;
effect.play(loop, onEnd);
}
private function getEffect(): MyCache
{
if (_pool.length > 0)
{
var idx: int = Math.round((Math.random() * (_pool.length - 1)));
var rez: MyCache = _pool[idx];
_pool.splice(idx, 1);
return rez;
}
return createEffect();
}
private function onEnd(effect: MyCache): void
{
_pool.push(effect);
}
}
* This source code was highlighted with Source Code Highlighter .
public class BoomCachPool extends MyCachePool
{
override protected function createEffect():MyCache
{
return new MyCache( new gBoom(), 20);
}
}
* This source code was highlighted with Source Code Highlighter .
public class BoomCachPool extends MyCachePool
{
override protected function createEffect():MyCache
{
return new MyCache( new gBoom(), 20);
}
}
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/113319/
All Articles