J2ME 游戏开发之层---背景静态层

* TiledLayer(columns, rows, image, tileWidth, tileHeight)

* columns: layer包含多少列

* rows : layer包含多少行

* image : 待填充到layer的图片,一般由一些列的子图组成

* tileWidth :每个字图的宽

* tileHeight: 每个字图的高

 

* layer.setCell(col, row, tileIndex)

* col : 图片填充的列

* row :填充的行

* tileIndex: 地图索引 :如果为0的话,则绘制透明色

* setPosition(x, y): 设置层的位置

*getX(),getY() : 获得层的left和top位置

 

 

package com.slinw.TiledLayout;



import javax.microedition.lcdui.Graphics;

import javax.microedition.lcdui.Image;

import javax.microedition.lcdui.game.TiledLayer;

import com.slinw.gamecanvas.RootGameCanvas;

import com.slinw.unit.Images;







/**

 * TiledLayer(columns, rows, image, tileWidth, tileHeight)

 *         columns: layer包含多少列

 *         rows   : layer包含多少行

 *         image  : 待填充到layer的图片,一般由一些列的子图组成

 *         tileWidth :每个字图的宽

 *         tileHeight: 每个字图的高

 * 

 * layer.setCell(col, row, tileIndex)

 *         col : 图片填充的列

 *         row :填充的行

 *         tileIndex: 地图索引  :如果为0的话,则绘制透明色

 * 

 * @author Administrator

 *

 */

public class StaticTiledLayout extends RootGameCanvas{



    private int LEA = 1;        //草地 lea

    private int FLD = 2;       //田地  field

    private int HSE = 3;       //house

    private int TRE = 4;       //tree

    private int WOD = 5;      //wood  树丛

    private int LAK = 6;     //leak

    

    private Image image;

    private TiledLayer bglLayer;

    

    private int curX = 100;

    private int curY = 100;

    private int moveX = 5;

    private int moveY = 5;

    

    public StaticTiledLayout(){

        

        initData();

        

    }

    

    private void initData(){

        image = Images.getImage("/background.png");

        

        bglLayer = createTiledLayer();

    }

    

    private TiledLayer createTiledLayer(){

        TiledLayer layer;

        int columns = 5;

        int rows = 5;

        int tileHeight = 36;

        int tileWidth = 78;

        

        //初始化层的行和列以及填充行和列的图片

        layer = new TiledLayer(columns, rows, image, tileWidth, tileHeight);

        

        //定义层显示的CELL格

        int[] cells = {

                LEA,FLD,TRE,FLD,FLD,

                LEA,LEA,LEA,WOD,LEA,

                HSE,LEA,HSE,WOD,FLD,

                WOD,LAK,HSE,LEA,WOD,

                HSE,LEA,LEA,TRE,LEA,

        };

        

        //填充行、列对应的图片

        for (int i = 0; i < cells.length; i++) {

            

            int col = i % columns;        

            int row = (i - col)/columns;        

            layer.setCell(col, row, cells[i]);        

        }

        

        return layer;

    }

    

    public void drawScreen(Graphics g) {

        // TODO Auto-generated method stub

        g.setColor(255,255,255);

        g.fillRect(0, 0, width, height);

        

        bglLayer.setPosition(curX, curY);

        bglLayer.paint(g);

        

        this.flushGraphics();

    }



    public void keyLeftPressedEvent() {

        // TODO Auto-generated method stub

        curX = curX - moveX;

        System.out.println("left**************");

    }



    public void keyRightPressedEvent() {

        // TODO Auto-generated method stub

        curX = curX + moveX;



    }



    public void keyUpPressedEvent() {

        // TODO Auto-generated method stub

        curY = curY - moveY;

    }



    public void keyDownPressedEvent() {

        // TODO Auto-generated method stub

        curY = curY + moveY;

    }



}

你可能感兴趣的:(j2me)