j2me中,Sprite TieldLayer地图贴图的制作方法

来源:互联网 发布:数据库存储过程面试题 编辑:程序博客网 时间:2024/04/30 13:33
 

public class PersonMoveMain extends MIDlet {
 private Display display;
 private PersonMapCanvas pmc=new PersonMapCanvas(true);
 
 public PersonMoveMain(){
  display=Display.getDisplay(this);
 }
 protected void startApp() throws MIDletStateChangeException {
  display.setCurrent(pmc);
 }

 protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
 
 }

 protected void pauseApp() {
 }
 class PersonMapCanvas extends GameCanvas implements Runnable{
  //声明绘制地图的图片对象
  private Image map;
  //声明一个精灵对象
  private Sprite sperson;
  private LayerManager lm;
  //声明绘制草地和墙的layer
  private TiledLayer tlRoad;
  private TiledLayer tlWall;
  //声明一组精灵图片对象
  private Image pesondownImage;
  //声明一个绘图工具对象
  private Graphics g;
  //声明精灵的初始坐标x,y
  private int current_x=0;
  private int current_y=0;
  //设置屏幕刷新
  private boolean Running=true;
  // 设置背景层的行数和列数
  private int rowNum = 12;
  private int columnNum = 12;
  //控制LayerManager的x、y坐标VW_X
  private int VM_X=0;
  private int VM_Y=0;
  //定义地图分布情况
  int[][] cells = new int[][]{
    {1,1,2,2,2,2,2,2,2,1,2,2},
    {1,1,1,1,1,1,1,1,1,1,2,1},
    {1,1,1,2,2,2,2,2,1,2,1,1},
    {1,1,1,2,2,2,1,2,1,2,1,1},
    {1,1,1,2,2,1,2,1,1,2,1,1},
    {1,1,1,2,1,2,1,2,1,2,1,1},
    {1,1,1,2,1,2,2,1,1,2,1,1},
    {1,1,1,2,1,2,2,1,1,2,1,1},
    {1,1,1,2,1,2,1,2,1,2,1,1},
    {1,1,1,2,2,1,2,2,1,2,1,1},
    {1,1,1,2,1,2,2,2,1,2,1,1},
    {1,1,1,1,2,2,2,2,1,2,1,1}
   };
  protected PersonMapCanvas(boolean suppressKeyEvents) {
   super(true);
   //获得绘图工具对象
   g=this.getGraphics();
   //获得layer管理类对象
   lm=new LayerManager();
   //创建一个地图image对象和精灵image对象
   try {
    map = Image.createImage("/map.png");
    pesondownImage=Image.createImage("/person_down.png");
   } catch (IOException e) {
    e.printStackTrace();
   }
   //实例化地图对象,草地和墙以及精灵对象
   tlRoad = new TiledLayer(rowNum,columnNum,map,map.getWidth()/2,map.getHeight());
   tlWall = new TiledLayer(rowNum,columnNum,map,map.getWidth()/2,map.getHeight()); 
   sperson=new Sprite(pesondownImage,pesondownImage.getWidth()/4,pesondownImage.getHeight());   
   sperson.setPosition(current_x, current_y);
   //通过循环填充地图
   for(int i=0;i<12;i++){
    for(int j=0;j<12;j++){
     if(cells[j][i]==1){
      tlRoad.setCell(j, i, cells[i][j]);
     }
     else if(cells[j][i]==2){
      tlWall.setCell(j, i, cells[i][j]);
     }
    }
   }
   lm.append(sperson);
   lm.append(tlRoad);
   lm.append(tlWall);
   new Thread(this).start();
  }
  public void run() {
   while(Running){
    System.out.println("AAAAAAAAAAAAAA"+sperson.getX()+","+sperson.getX());
   
    g.setColor(255, 255, 255);
    g.fillRect(0, 0, this.getWidth(), this.getHeight());
    
    lm.setViewWindow(VM_X,VM_Y,this.getWidth(),this.getHeight());
    lm.paint(g, 0, 0);
    this.flushGraphics();
    switch (this.getKeyStates()) {
    case DOWN_PRESSED:
     System.out.println("下");
     //判断向下走的范围不要超过地图
     if(sperson.getY()>=0&&sperson.getY()<=map.getHeight()*12-sperson.getHeight()){
      sperson.nextFrame();//下一帧
      sperson.move(0, 1);
     }
     lm.paint(g, this.getWidth(), this.getHeight());
     //判断当精灵走到屏幕的0.6倍后、向上滚动地图
     if(sperson.getY()>this.getHeight()*0.6&&VM_Y<map.getHeight()*12-this.getHeight()){
      System.out.println("DDDD"+VM_Y);
      VM_Y++;
     }
     this.flushGraphics();
     break;
    case UP_PRESSED :
     System.out.println("上");
     sperson.nextFrame();//下一帧
     sperson.move(0, -1);
     lm.paint(g, this.getWidth(), this.getHeight());
     this.flushGraphics();
     break;
    case LEFT_PRESSED :
     System.out.println("左");
     sperson.nextFrame();//下一帧
     sperson.move(-1, 0);
     lm.paint(g, this.getWidth(), this.getHeight());
    
     this.flushGraphics();
     break;
    case RIGHT_PRESSED:
     System.out.println("右");
     sperson.nextFrame();//下一帧
     sperson.move(1, 0);
     lm.paint(g, this.getWidth(), this.getHeight());
     if(sperson.getX()>this.getWidth()*0.6&&VM_X<map.getHeight()*12-this.getWidth()){
      System.out.println("DDDD"+VM_X);
      VM_X++;
     }
     this.flushGraphics();
     break;
    }
    try {
     Thread.currentThread().sleep(100);
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
    
   }
  }
  
  
 }
}

运行效果:

 

原创粉丝点击