Author Topic: Split a sprite  (Read 1277 times)

Rolpege

  • Guest
Split a sprite
« on: Wed, Jun 30, 2010 »
Here's how you can split a FlxSprite into more little FlxSprites:

Code: [Select]
/**
* Call this to split up a sprite into little sprites.
*
* @param Sprite The sprite you want to split.
* @param Width The width of the splitted sprites.
* @param Height The height of the splitted sprites.
*
* @return An Array full of the splitted sprites
*/
public static function splitSprite(Sprite:FlxSprite, Width:int, Height:int):Array
{
var numberOfHorizontal:int = int(Sprite.width / Width);
var numberOfVertical:int = int(Sprite.height / Height);

var r:Array = [];

for(var x:int = 0; x < numberOfHorizontal; x++)
{
for(var y:int = 0; y < numberOfVertical; y++)
{
var bmp:BitmapData = new BitmapData(Width, Height, true, 0x00000000);
bmp.copyPixels(Sprite.pixels, new Rectangle(x*Width, y*Height, Width, Height), new Point(0, 0), null, null, true);
var s:FlxSprite = new FlxSprite(x*Width+Sprite.x, y*Height+Sprite.y);
s.pixels = bmp;

r.push(s);
}
}

return r;
}

I'm sure it needs optimization and can be a lot improved, so if you find a better way to do it, please, post it! =D