91 lines
2.2 KiB
Haxe
91 lines
2.2 KiB
Haxe
package ru.m.animate;
|
|
|
|
import flash.display.Bitmap;
|
|
import flash.display.BitmapData;
|
|
import flash.display.PixelSnapping;
|
|
import haxe.Timer;
|
|
|
|
typedef Frame = {
|
|
var image:BitmapData;
|
|
var length:Int;
|
|
}
|
|
|
|
class AnimateManager {
|
|
public var playing(default, default):Bool;
|
|
|
|
private var timer:Timer;
|
|
private var animations:Array<Animate>;
|
|
|
|
public function new() {
|
|
animations = [];
|
|
timer = new Timer(30);
|
|
timer.run = update;
|
|
}
|
|
|
|
public function update():Void {
|
|
if (playing) {
|
|
for (animation in animations) {
|
|
if (animation.playing) {
|
|
animation.update();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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;
|
|
manager.add(this);
|
|
}
|
|
|
|
public function set_frames(value:Array<Frame>):Array<Frame> {
|
|
sequence = [];
|
|
index = 0;
|
|
if (value != null) {
|
|
frames = value;
|
|
for (frame in frames) {
|
|
sequence = sequence.concat([for (i in 0...frame.length) frame.image]);
|
|
}
|
|
bitmapData = sequence[0];
|
|
} else {
|
|
bitmapData = null;
|
|
}
|
|
return frames;
|
|
}
|
|
|
|
public function update():Void {
|
|
if (++index >= sequence.length) {
|
|
index = 0;
|
|
}
|
|
var nextBitmapData = sequence[index];
|
|
x -= (nextBitmapData.width - bitmapData.width) / 2;
|
|
y -= (nextBitmapData.height - bitmapData.height) / 2;
|
|
bitmapData = nextBitmapData;
|
|
}
|
|
|
|
public function dispose():Void {
|
|
manager.remove(this);
|
|
}
|
|
}
|