[client] add AnimationManager
This commit is contained in:
@@ -1,47 +1,62 @@
|
||||
package ru.m.animate;
|
||||
|
||||
import flash.display.PixelSnapping;
|
||||
import haxe.Timer;
|
||||
import flash.display.Bitmap;
|
||||
import flash.display.BitmapData;
|
||||
|
||||
import flash.display.PixelSnapping;
|
||||
import haxe.Timer;
|
||||
|
||||
typedef Frame = {
|
||||
var image:BitmapData;
|
||||
var length:Int;
|
||||
}
|
||||
|
||||
class Animate extends Bitmap {
|
||||
class AnimateManager {
|
||||
public var playing(default, default):Bool;
|
||||
|
||||
private static var timer:Timer;
|
||||
private static var instances:Array<Animate> = [];
|
||||
private var timer:Timer;
|
||||
private var animations:Array<Animate>;
|
||||
|
||||
private static function init():Void {
|
||||
if (timer == null) {
|
||||
timer = new Timer(30);
|
||||
timer.run = updateAll;
|
||||
}
|
||||
public function new() {
|
||||
animations = [];
|
||||
timer = new Timer(30);
|
||||
timer.run = update;
|
||||
}
|
||||
|
||||
private static function updateAll():Void {
|
||||
for (instance in instances) {
|
||||
if (instance.playing) {
|
||||
instance.update();
|
||||
public function update():Void {
|
||||
if (playing) {
|
||||
for (animation in animations) {
|
||||
if (animation.playing) {
|
||||
animation.update();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public var playing(default, set):Bool;
|
||||
public function add(animate:Animate):Void {
|
||||
animations.push(animate);
|
||||
}
|
||||
|
||||
public function remove(animate:Animate):Void {
|
||||
if (animations.indexOf(animate) > -1) {
|
||||
animations.remove(animate);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Animate extends Bitmap {
|
||||
|
||||
public var playing(default, default):Bool;
|
||||
public var frames(default, set):Array<Frame>;
|
||||
|
||||
private var sequence:Array<BitmapData>;
|
||||
private var index:Int;
|
||||
|
||||
@:provide private static var manager:AnimateManager;
|
||||
|
||||
public function new(?frames:Array<Frame>) {
|
||||
super(null, PixelSnapping.AUTO, true);
|
||||
this.frames = frames == null ? [] : frames;
|
||||
init();
|
||||
instances.push(this);
|
||||
manager.add(this);
|
||||
}
|
||||
|
||||
public function set_frames(value:Array<Frame>):Array<Frame> {
|
||||
@@ -59,14 +74,7 @@ class Animate extends Bitmap {
|
||||
return frames;
|
||||
}
|
||||
|
||||
public function set_playing(value:Bool):Bool {
|
||||
if (playing != value) {
|
||||
playing = value;
|
||||
}
|
||||
return playing;
|
||||
}
|
||||
|
||||
private function update():Void {
|
||||
public function update():Void {
|
||||
if (++index >= sequence.length) {
|
||||
index = 0;
|
||||
}
|
||||
@@ -77,8 +85,6 @@ class Animate extends Bitmap {
|
||||
}
|
||||
|
||||
public function dispose():Void {
|
||||
if (instances.indexOf(this) > -1) {
|
||||
instances.remove(this);
|
||||
}
|
||||
manager.remove(this);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user