Your tiles don’t need to be sprites. Instead, your tiled background needs to have its own class. Have a look at how the MIDP 2.0 game API works, and write similar classes yourself.
The tile image PNG file could have all the different tile type images in e.g. a horizontal row. Then you build the real background image by drawing the ‘frames’ of that resource file image repeatedly onto a big blank image according to your tile grid (array ‘tiles’ below).
Since you’ve got the tile grid, you can also use it to find out which type of tile is at a certain grid position. If you have an array (‘isSolid’ below) telling which types of tile are solid, you can use that in an ‘isSolid’ method.
Warning: the code below is an adaptation from memory of part of a (working) demo game I wrote, but I just typed it in now and I haven’t even tried to compile it - no guarantees! I’m afraid YABB does wierd things with its indentation :(.
`class Background {
static final int TILE_WIDTH = 8;
static final int TILE_HEIGHT = 8;
static final int TILE_COLUMNS = 7; // see grid below
static final int TILE_ROWS = 5; // see grid below
static private int[][] tiles = new int[][]
{ { 0, 0, 0, 0, 0, 0, 0 }, // 0 is empty space
{ 1, 1, 0, 1, 1, 2, 1 }, // 1 is solid rock, 2 is a ladder
{ 0, 0, 0, 0, 0, 2, 0 },
{ 0, 0, 1, 0, 0, 2, 0 },
{ 1, 1, 1, 1, 0, 1, 1 } };
static private boolean isSolid[] = new boolean[]
{ false, true, true }; // space, rock, ladder
private Image backgroundImg;
// tilesImg is 24x8 pixels, with the images for space,
// then rock, then ladder in a horizontal row
Background(Image tilesImg) {
backgroundImg = Image.createImage(
TILE_WIDTH * TILE_COLUMNS,
TILE_HEIGHT * TILE_ROWS);
Graphics bg = backgroundImg.getGraphics();
for (int tx = 0; tx < TILE_COLUMNS; ++tx) {
for (int ty = 0; ty < TILE_ROWS; ++ty) {
bg.setClip(tx * TILE_WIDTH, ty * TILE_HEIGHT,
TILE_WIDTH, TILE_HEIGHT);
// draw tilesImg offset left so correct frame hits clip window
bg.drawImage(tilesImg,
(tx - tiles[ty][tx]) * TILE_WIDTH,
ty * TILE_HEIGHT,
Graphics.TOP | Graphics.LEFT);
}
}
}
void paint(Graphics g, int x, int y) {
g.drawImage(backgroundImg, x, y, Graphics.TOP | Graphics.LEFT);
}
// tx & ty are tile index, not pixel locations, i.e. 0..6 and 0..4
boolean isSolid(int tx, int ty) {
return isSolid[tile[ty][tx]];
}
}`
I leave you the fun of working out which tiles to check beneath your sprite.