So I was putting together a game where I wanted an animated tilemap in flixel, it did not seem to have this functionality natively, so I created a class for it and thought I'd share. It would be pretty simple to extend this to add animation features for randomly animating tilemaps or specify tile sequences for different positions.
[code lang="actionscript"]
///////////////////////////////////////////////////
// FlxAnimTilemap.as
// CREATED: Wed Mar 12 21:19:36 EDT 2008
// BY: cjgammon - cj.gammon@gmail.com
//
// CLASS RESPONSIBILITES:
// tilemap of sprites that animate in a synchronized fashion
//
//////////////////////////////////////////////////package org.flixel
{import org.flixel.FlxAnimTilemap;
public class FlxAnimTilemap extends FlxTilemap
{
private var _animating:Boolean = true;
private var _animSpeedState:uint = 0;
private var _animState:uint = 1; //the first sprite of the animation sequence
private var spriteTotalTiles:uint = 5; //the last sprite of the animation
private var animIntervals:uint = 5; //speed of animation// CONSTRUCTOR
public function FlxAnimTilemap($spriteTotalTiles:uint = 5, $animInterval:uint = 10):void
{
spriteTotalTiles = $spriteTotalTiles;
animIntervals = $animInterval;
super();
}// PUBLIC METHODS
override public function update():void
{
if (_animating)
{
_animSpeedState++
if (_animSpeedState%animIntervals==0)
{
animate();
}
}
}public function play():void
{
_animating = true;
}public function stop():void
{
_animating = false;
}public function animate():void
{
_animState++;
for (var i:int = 0; i < totalTiles; i++)
{
var tile:uint = getTileByIndex(i);
if (tile!=0)
{
setTileByIndex(i, _animState);
}
}
if (_animState==spriteTotalTiles)
{
_animState=1;
}
}
}
}[/code]