[render] added animates

This commit is contained in:
2018-02-01 00:05:14 +03:00
parent f2f860fc9d
commit 6f338584eb
6 changed files with 191 additions and 29 deletions

View File

@@ -0,0 +1,62 @@
package ru.m.animate;
import flash.display.PixelSnapping;
import haxe.Timer;
import flash.display.Bitmap;
import flash.display.BitmapData;
class Animate extends Bitmap {
private static var timer:Timer;
private static var instances:Array<Animate> = [];
private static function init():Void {
if (timer == null) {
timer = new Timer(30);
timer.run = updateAll;
}
}
private static function updateAll():Void {
for (instance in instances) {
if (instance.playing) {
instance.update();
}
}
}
public var playing(default, set):Bool;
private var frames:Array<BitmapData>;
private var index:Int;
public function new(frames:Array<BitmapData>) {
super(frames[0], PixelSnapping.AUTO, true);
this.frames = frames;
init();
instances.push(this);
}
public function set_playing(value:Bool):Bool {
if (playing != value) {
playing = value;
}
return playing;
}
private function update():Void {
if (++index >= frames.length) {
index = 0;
}
var nextBitmapData = frames[index];
x -= (nextBitmapData.width - bitmapData.width) / 2;
y -= (nextBitmapData.height - bitmapData.height) / 2;
bitmapData = nextBitmapData;
}
public function dispose():Void {
if (instances.indexOf(this) > -1) {
instances.remove(this);
}
}
}