⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mycanvas.java

📁 手机游戏demo,用tile实现地图图层
💻 JAVA
字号:
import javax.microedition.lcdui.*;import javax.microedition.lcdui.game.*;import java.io.IOException;public class MyCanvas extends GameCanvas implements Runnable,CommandListener  {  private TiledLayerDemo midlet;//程序控制类  private volatile Thread animationThread=null;//线程  private TiledLayer mBackground;//地图    private LayerManager mLayerManager;  private int canvasHeight, canvasWidth;//屏幕高度/宽度  private static int tileHeight,tileWidth;//地图的高度/  private static int columns,rows;  private static int mapWidth,mapHeight;  private int MON = 1;//月亮moon  private int SHN = 2;//闪烁shine  private int STR = 3;//星星star  private int AIR = 4;//夜空air  private int ANI;//动画贴片animated    private int aniStarIndex;  private int aniCounter;  private boolean isRun;    private int curX,curY;//当前viewer坐标    private final int SLEEP=10;  private Command exit;  public MyCanvas(TiledLayerDemo midlet)  {    super(true);    this.midlet = midlet;    canvasHeight=getHeight();    canvasWidth=getWidth();    try{    mBackground = createBackground();//创建背景    }catch(IOException e){    System.out.println("create error");    }    curX = -(mapWidth - canvasWidth)/2;    curY = -(mapHeight - canvasHeight)/2;    mBackground.setPosition(curX,curY);      exit=new Command("Exit", Command.STOP,1);    this.addCommand(exit);    this.setCommandListener(this);//两个按键   }   synchronized void start()  {    isRun=true;    animationThread=new Thread(this);    animationThread.start();  }  public void run()  {    Graphics g = this.getGraphics();    try    {      while (isRun)      {      	tick();      	render(g);        Thread.sleep(SLEEP);      }    }    catch(InterruptedException ie)    {      System.out.println(ie.toString());    }  }  private void tick(){     aniCounter++;     if(aniCounter%6 == 0)     mBackground.setAnimatedTile(ANI,SHN);     else     mBackground.setAnimatedTile(ANI,STR);  }   private TiledLayer createBackground()throws IOException {    Image image = Image.createImage("/star.png");//创建背景图片    TiledLayer tiledLayer = new TiledLayer(10, 10, image, 32, 32);//注意:背景的参数的设置!    tileWidth = 32;    tileHeight = 32;     columns = 10;    rows = 10;    mapWidth =tileWidth*columns;    mapHeight = tileHeight*rows;    ANI =  tiledLayer.createAnimatedTile(STR);    int[] map = {//地图数组       AIR,AIR,AIR,AIR,AIR, AIR,AIR,AIR,AIR,AIR,       AIR,ANI,AIR,AIR,AIR, AIR,AIR,AIR,STR,AIR,       AIR,AIR,STR,AIR,AIR, ANI,AIR,AIR,STR,AIR,       AIR,AIR,AIR,MON,STR, AIR,AIR,STR,AIR,AIR,       AIR,AIR,STR,STR,AIR, AIR,AIR,AIR,AIR,AIR,       AIR,AIR,STR,AIR,ANI, AIR,AIR,AIR,ANI,AIR,       AIR,AIR,AIR,AIR,AIR, STR,AIR,AIR,AIR,AIR,       AIR,AIR,STR,AIR,AIR, AIR,AIR,AIR,AIR,AIR,       AIR,STR,AIR,AIR,AIR, STR,AIR,AIR,AIR,AIR,       AIR,AIR,AIR,AIR,AIR, AIR,AIR,AIR,AIR,AIR,    };//显示地图    for (int i = 0; i < map.length; i++) {      int column = i % columns;      int row = (i - column) / columns;      tiledLayer.setCell(column, row, map[i]);//通过一个循环来设置tiledLayer    }    //tiledLayer.setAnimatedTile(SHN,STR);       // tiledLayer.setCell(4,5, aniStarIndex);        return tiledLayer;//返回  }    private void render(Graphics g){    //mLayerManager.paint(g, 0, 0);    mBackground.paint(g);    flushGraphics();    }  synchronized void stop()  {    isRun=false;  }  public void commandAction(Command c, Displayable d)  {    //if any Exit key is pressed then exit    if(c==exit)    {      midlet.exitMidlet();    }  }  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -