Tile Spacing supported?


(artanis) #1

My tile sheets all have a 1 pixel border around each tile. Ogmo has a Tile Spacing option that works just fine for this. When it comes to Flashpunk though, I’m not seeing an option. Is there a way to do tile spacing I just didn’t see?

I suppose I could redo my tilesheets.


(Ultima2876) #2

Hmm, I guess you could make a BitmapData object then draw all of your tiles to that, then ultimately create your Tilemap from the BitmapData.


(Jacob Albano) #3

I made a function for you. :slight_smile:

public static function pad(source:BitmapData, tileWidth:int, tileHeight:int, padding:int):BitmapData
{
    var width:int = source.width + (source.width / tileWidth) * (padding * 2);
    var height:int = source.height + (source.height / tileHeight) * (padding * 2);
    
    var target:BitmapData = new BitmapData(width, height, true, 0x0);
    var rect:Rectangle = new Rectangle(0, 0, tileWidth, tileHeight);
    var point:Point = new Point();
    
    var countX:int = source.width / tileWidth;
    var countY:int = source.height / tileHeight;
    
    for (var i:int = 0; i < countX; ++i)
    {
        for (var j:int = 0; j < countY; ++j)
        {
            rect.x = i * tileWidth;
            rect.y = j * tileHeight;
            
            point.x = padding * (i + 1) + padding * i + i * tileWidth;
            point.y = padding * (j + 1) + padding * j + j * tileHeight;
            
            target.copyPixels(source, rect, point);
        }
    }
    
    return target;
}

This is a fairly expensive operation and will eat up a lot of memory if you have a lot of copies, so you should ideally run it once and store the result.


(Ultima2876) #4

That’s the way! Nice one @jacobalbano :slight_smile: