Im using the built in NMEPReloader as my base class for my preloader in openfl.
What Im doing is playing an animation frame by frame
class AttractAnimation extends Sprite
{
var currentFrame:Int;
var previousFrame:Int;
var bitmapFrames:Array<Bitmap>;
var loadScreenTimer:Timer;
public function new()
{
super();
currentFrame = 0;
previousFrame = -1;
bitmapFrames = new Array<Bitmap>();
}
public function assignBitmapData(bitmapData : Array<BitmapData>){
for(bmap in bitmapData){
var frame = new Bitmap( null, flash.display.PixelSnapping.AUTO, true );
frame.bitmapData = bmap;
pushFramesAsBitmap(frame);
}
}
public function pushFramesAsBitmap(frameAsBitmap:Bitmap) {
bitmapFrames.push(frameAsBitmap);
frameAsBitmap.visible = false;
addChild(frameAsBitmap);
}
public function startAnimation(fps:Int) {
var milliseconds:Float = 1000 / fps;
loadScreenTimer = new Timer(milliseconds, 10);
loadScreenTimer.addEventListener(TimerEvent.TIMER, updateAnimation);
loadScreenTimer.start();
}
private function updateAnimation(e : TimerEvent):Void {
if (currentFrame > bitmapFrames.length -1) { currentFrame = 0; }
if (previousFrame != -1) {
bitmapFrames[previousFrame].visible = bitmapFrames[previousFrame].__combinedVisible = false;
}
bitmapFrames[currentFrame].visible = bitmapFrames[currentFrame].__combinedVisible = true;
previousFrame = currentFrame;
currentFrame++;
}
}
what im seeing when I call playAnimation is it stutters really bad, the next time I play the animation it plays smoothly.
I have tried setting the alpha to 0 playing the animation then setting it back to 1 and playing but it will stutter the second time in this case.
Same if I move it behind another display object play the animation then move it to the front of the draw order, it will stutter the second time.
IS there a way I can play this animation without it stuttering?
I was able to stop the stuttering by using a spritesheet instead of individual images.
I changed the name as we added a second animation. You can see I use Tilesheet now instead of a Bitmap array.
I have updated the below answer again to include the suggested onEnterFrame change. The spritesheet uses got rid of the stuttering but the animation is now much smoother with enterframe event.