j2me Sprite 和TieldLayer检测问题

 

class MyGameCanvas extends GameCanvas implements Runnable{
  private TiledLayer troad;
  private TiledLayer twall;
  private Sprite sperson;
  private Image personImage;
  private Image mapImage;
  private LayerManager lm;
  private Graphics g;
  protected MyGameCanvas(boolean suppressKeyEvents) {
   super(true);
   try {
    lm=new LayerManager();
    g=this.getGraphics();
    personImage=Image.createImage("/icon.png");
    mapImage=Image.createImage("/map.png");
    sperson=new Sprite(personImage);
    troad=new TiledLayer(4, 2, mapImage, mapImage.getWidth()/2, mapImage.getHeight());
    twall=new TiledLayer(4, 2, mapImage, mapImage.getWidth()/2, mapImage.getHeight());
    twall.setPosition(0, 80);

    troad.setCell(0, 0, 1);
    troad.setCell(1, 0, 1);
    troad.setCell(2, 0, 1);
    troad.setCell(3, 0, 1);
    
    troad.setCell(0, 1, 1);
    troad.setCell(1, 1, 1);
    troad.setCell(2, 1, 1);
    troad.setCell(3, 1, 1);
    
    twall.setCell(0, 0, 2);
    twall.setCell(1, 0, 2);
    twall.setCell(2, 0, 2);
    twall.setCell(3, 0, 2);
    
    twall.setCell(0, 1, 2);
    twall.setCell(1, 1, 2);
    twall.setCell(2, 1, 2);
    twall.setCell(3, 1, 2);

    lm.append(sperson);
    lm.append(troad);
    lm.append(twall);
    lm.setViewWindow(0, 0, this.getWidth(), this.getHeight());
    lm.paint(g, 0, 0);
    this.flushGraphics();
    new Thread(this).start();
   
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }

  public void run() {
   while(true){
    g.setColor(255, 255, 255);
    g.fillRect(0, 0, this.getWidth(), this.getHeight());
    lm.paint(g, 0, 0);
    this.flushGraphics();
    switch (this.getKeyStates()) {
    case DOWN_PRESSED:
     System.out.println("下");
     
     if (!sperson.collidesWith(twall, true))
       {
      sperson.move(0, 1);
      
       }else{
        System.out.println("撞墙了");
       }
     lm.paint(g, this.getWidth(), this.getHeight()); 
     this.flushGraphics();
     break;
    case UP_PRESSED :
     System.out.println("上");
     sperson.move(0, -1);
     if (sperson.collidesWith(twall, true))
       {
      System.out.println("撞墙了");
       }
     lm.paint(g, this.getWidth(), this.getHeight());
     this.flushGraphics();
     break;
    case LEFT_PRESSED :
     System.out.println("左");
     sperson.move(-1, 0);
     if (sperson.collidesWith(twall, true))
       {
      System.out.println("撞墙了");
       }
     lm.paint(g, this.getWidth(), this.getHeight());    
     this.flushGraphics();
     break;
    case RIGHT_PRESSED:
     System.out.println("右");
     sperson.move(1, 0);
     if (sperson.collidesWith(twall, true))
       {
      System.out.println("撞墙了");
       }
     lm.paint(g, this.getWidth(), this.getHeight()); 
     this.flushGraphics();
     break;
    }
    try {
     Thread.currentThread().sleep(100);
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
    
   }
  
  
   
  }

运行效果:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值