Author Topic: FlxTilemapExt - tile to FlxSprite  (Read 8072 times)

Chelnok

  • Contributor
  • ****
  • Posts: 257
  • Karma: +0/-0
    • View Profile
FlxTilemapExt - tile to FlxSprite
« on: Fri, Oct 28, 2011 »
Small extension that let you grab particular tile from tilemap to FlxSprite.

How to use:

Code: [Select]
var myTileSprite:FlxSprite = map.tileToFlxSprite(15,16)
add(myTileSprite)
myTileSprite.velocity.x = 15 //for testing

Code: [Select]
package
{
import flash.geom.Point;
import flash.geom.Rectangle;

import org.flixel.FlxSprite;
import org.flixel.FlxTilemap;


/**
* This is an extension that adds one function to FlxTilemap class: tileToFlxSprite().
* You can also copy the function and paste it to FlxTilemap. No need to change anything.

* @author Chelnok
*/
public class FlxTilemapExt extends FlxTilemap
{
public function FlxTilemapExt()
{
super();
}


/**
* Chance a particular tile to FlxSprite. Or just copy the graphic if you dont want any chances to mapdata itself.
*
* @param X The X coordinate of the tile (in tiles, not pixels).
* @param Y The Y coordinate of the tile (in tiles, not pixels).
* @param NewTile New tile to the mapdata. Use -1 if you dont want any changes. Default = 0 (empty)
*
* @return FlxSprite.
*/
public function tileToFlxSprite(X:uint,Y:uint,NewTile:int=0):FlxSprite
{
var rowIndex:int = X + (Y * widthInTiles);
var rect:Rectangle = _rects[rowIndex] as Rectangle;
var pt:Point = new Point(0, 0);
var tileSprite:FlxSprite = new FlxSprite();

tileSprite.makeGraphic(_tileWidth,_tileHeight,0x00000000,true)

tileSprite.x = X * _tileWidth;
tileSprite.y = Y * _tileHeight;

if (rect != null)
tileSprite.pixels.copyPixels(_tiles, rect, pt);

tileSprite.dirty = true

if (NewTile >= 0)
setTile(X,Y,NewTile);

return tileSprite;
}
}
}