Monday, January 17, 2011

j2me 4-way scrolling game part 2:

Part 2:
         Tile layer class

Am really writing this stuff up as i go along so don't expect a detailed comprehensive coverage.

//source code//


    import java.io.IOException;
    import javax.microedition.lcdui.Image;
    import javax.microedition.lcdui.game.TiledLayer;
    /**
    *
    * @author kim
    */
    public class levelMaps {

    int[][] mapA =
            {                       
                            { 0, 1, 1, 1, 1, 1, 1, 1, 1, 0},
                            { 0, 4, 4, 4, 4, 4, 4, 4, 4, 0},
                            { 0, 7, 7, 7, 7, 7, 7, 7, 7, 0},
                            {10, 0, 0, 0, 0, 0, 0, 0, 0, 2},
                            {10, 0, 0, 0, 0, 0, 0, 0, 0, 3},
                            {10, 0, 0, 0, 0, 0, 0, 0, 0, 8},
                            {10, 0, 0, 0, 0, 0, 0, 0, 0, 11},
                            { 0, 6, 6, 6, 6, 6, 0, 0, 0, 10},
                            { 0, 9, 9, 9, 9, 9, 0, 0, 0, 10},
                            {12,12,12,12,10,10,10,10,10, 10},
            };

    int[][] mapB =
            {                         
                            {0,0,0,0,0,0,0,0,0,0},
                            {0,0,0,0,0,0,0,0,0,0},
                            {0,0,0,0,0,0,0,0,0,0},
                            {0,2,2,2,2,2,2,2,2,0},
                            {0,2,2,2,2,2,2,2,2,0},
                            {0,2,2,2,2,2,2,2,2,0},
                            {0,2,2,2,2,2,2,2,2,0},
                            {0,0,0,0,0,0,0,0,0,0},
                            {0,0,0,0,0,0,0,0,0,0},
                            {0,0,0,0,0,0,0,0,0,0},                           
            };

    private int rows;
    private int columns;
    private int tileWidth;
    private int tileHeight;
  
    private TiledLayer tiledLayer;
  
    boolean collisionB4Scroll;

    private int [][] selMap;

    public levelMaps(int sel) throws IOException
    {
        addMap(sel);
    }

         /*row ########
          * column#
          *            #
          *            #
          *
          */

    public void addMap(int stage) throws IOException
    {
        Image tileImg = null;

        switch(stage)
        {
            case 1:  tileImg = Image.createImage("/h1.png");
            selMap = mapA;

            rows = 10;
            columns = 10;
            tileWidth = 88;
            tileHeight = 44;
       
            break;

            case 2:  tileImg = Image.createImage("/bg.png");
            selMap = mapB;

            rows = 10;
            columns = 10;
            tileWidth = 88;
            tileHeight = 44;

            break;

            default: tileImg = Image.createImage("/h1.png");
            selMap = mapA;
        }

         tiledLayer = new TiledLayer(rows, columns, tileImg, tileWidth, tileHeight);

         for(int row = 0;row < rows; row++)
         {
              for(int column = 0;column < selMap.length;column ++)
              {
                tiledLayer.setCell(row, column, selMap[column][row]);
              }
          }
        }
          
    public TiledLayer getMap(){
        return tiledLayer;
    }

}




mapA[][] is a 10 by 10 multidimensional array that holds integers that represent the tile to be displayed from the image. the other is an array for the background. Note the only difference is the parameter taken by the addMap(int) method.
A switch determines the map array to be used which is added to a new instance of the TiledLayer. The tile Layer takes five parameters (refer to the Game API ) .
 The getMap() function returns the TileLayer and is called from the GameManager class. Also the tileImg = Image.createImage("/bg.png"), this parameter should reflect the filepath i.e if your images are in a folder names res then it should read as  "/res/bg.png".

Thats about all there is to it  so next i will cover the sprite class which entails the players and enemies.

No comments:

Post a Comment