GetNumTiles

From Spheriki

Jump to: navigation, search

Gives the number of tiles in the tileset being used by the current map.

Contents

Usage

number GetNumTiles();

Examples

The main purpose of this function is to get information about the currently loaded tileset. Other functions allow you to get tiles and set tiles on the map, and even get the image of a particular tile from the tileset. As such, it is possible to make your own map editor within the sphere engine. This would be useful for making tedious things easier and quicker, as well as making much more complicated structures in your map simpler to do. For example, using your own editor, you can make a tool to place down trees. While you can somewhat do this in the map editor, the layer you place the tiles on is limited to the current layer you have selected. With a custom editor, you can place the tiles over multiple layers so that trees will overlap each other correctly.

To use a custom map editor though, you would like to be able to select which tile to draw. To do that, you would also want some graphic display to show what tile you will be selecting. To do that, you would need a loop to go through all the tiles in the tileset and draw them, for which, you would need to use GetNumTiles() to know when to stop.

This example shows a simple loop for going through the tiles. Most likely, you will not want to draw every single tile since there would not be enough room on the screen to do so. As such, we limit the number of tiles we go through in this example to 20.


//this will be the first tile shown in the display
starting_tile = 0;

for (i=0;i<20 && starting_tile+i<GetNumTiles();i++)
{
  Tileset[starting_tile+i].blit(0,i*GetTileHeight());
}


Where the Tileset array is a preloaded array that holds the images from the tileset. This would be done via another loop using the GetNumTiles() function and the GetTileImage() function:


var Tileset = new Array();

for (i=0;i<GetNumTiles();i++)
{
  Tileset[i] = GetTileImage(i);
}


Notes

  • This function will only work while the map engine is running.


See also

Personal tools