Skip to content

Instantly share code, notes, and snippets.

@jgranick
Created October 2, 2012 21:46
Show Gist options
  • Save jgranick/3823511 to your computer and use it in GitHub Desktop.
Save jgranick/3823511 to your computer and use it in GitHub Desktop.
How to use nme.display.Tilesheet (NME recipe)
import nme.display.Sprite;
import nme.display.Tilesheet;
import nme.geom.Rectangle;
import nme.Assets;
public class TilesheetExample extends Sprite {
public function new () {
super ();
var tilesheet = new Tilesheet (Assets.getBitmapData ("example.png"));
tilesheet.addTileRect (new Rectangle (0, 0, 100, 100));
tilesheet.addTileRect (new Rectangle (100, 0, 100, 100));
tilesheet.drawTiles (graphics, [ 0, 0, 0, 100, 0, 1 ]);
}
}
@jgranick
Copy link
Author

jgranick commented Oct 2, 2012

When you call "addTileRect", it generates a new tile ID. To draw the first rectangle from the source bitmap, use a tile ID of 0. To draw the second rectangle, use a tile ID of 1, and so on.

The array that drawTiles expects begins with the X, Y and tile ID that you wish to draw. In the example above, it will draw twice, once at (0, 0) and once at (100, 0).

The third parameter of "drawTiles" is a boolean value for whether the render should be smoothed, and the fourth accepts optional flags.

You can set flags for Tilesheet.TILE_SCALE, Tilesheet.TILE_ROTATION, Tilesheet.RGB and Tilesheet.TILE_ALPHA to extend the values in the draw array. Depending on which flags you use, the array expands from [ x, y, tileID ... ] to [ x, y, tile ID, scale, rotation, red, green, blue, alpha ... ]

@sergey-miryanov
Copy link

I can't find Tilesheet file at source tree (under nme.display), so I have a question - does addTileRect return ID of new created tile?

@thomasuster
Copy link

I can't find Tilesheet under nme.display either, only browser.display and native.display...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment