?? simplelayerdemo.java
字號:
//simpleLayerDemo.java file
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.game.*;
import java.util.*;
public class simpleLayerDemo extends MIDlet implements CommandListener
{
private Command exitCommand,actCommand;
private LayerCanvas canvas;
public simpleLayerDemo()
{//創建命令,畫布和定時器
exitCommand =new Command("Exit",Command.EXIT,1);
actCommand =new Command("Action",Command.SCREEN,1);
canvas =new LayerCanvas();
canvas.addCommand(actCommand);
canvas.addCommand(exitCommand);
canvas.setCommandListener(this);
}
protected void startApp( )
{//顯示畫布并開始游戲執行
Display.getDisplay(this).setCurrent(canvas);
}
protected void pauseApp( )
{
}
protected void destroyApp( boolean p1 )
{
}
public void commandAction(Command c,Displayable d)
{
if (c ==exitCommand)
{
destroyApp(false);
notifyDestroyed();
}
else if (c ==actCommand)
canvas.Action();
}
public class LayerCanvas extends GameCanvas
{
private Graphics g;
private TiledLayer backLayer0;//圖層
private Image backImage0;
private int aniCounter =0;//計數器
private int aniStarIndex =0;//保存動畫分塊的序號
//記錄圖層backLayer0的每個單元對應的圖像塊序號
private byte[ ][ ] backMap=
{{5,-1, 5,-1,-1},//序號-1為動態分塊
{5, 5, 5, 5,-1},
{4, 5, 5, 5, 5},
{4, 4, 1, 5, 5},//序號1為跳動的小球圖像
{4, 4, 4, 4, 4}};
public LayerCanvas()
{
super(true);
g = getGraphics();//得到作圖對象
try
{//裝入圖像
backImage0 = Image.createImage("/sky.png");
} catch (Exception e) {}
//創建圖層,分塊大小為:30×30,單元格數量為:5×5
backLayer0 = new TiledLayer(5, 5, backImage0, 30, 30);
//創建動畫分塊,aniStarIndex 應該返回 -1
aniStarIndex = backLayer0.createAnimatedTile(3);
for(int y=0;y<5;y++)
{//逐個設置每個單元格的圖像,標記為0的單元格表示不處理
for(int x=0;x<5;x++)
backLayer0.setCell(x, y, backMap[y][x]);
}
}
public void BallAction()
{//直接修改單元格內的圖像
backLayer0.setCell(2, 3, aniCounter%2+1);
}
public void StarAction()
{//修改動態分塊中的圖像來達到動畫效果
if(aniCounter%2 ==0)
backLayer0.setAnimatedTile(aniStarIndex, 3);
else
backLayer0.setAnimatedTile(aniStarIndex, 6);
}
//執行游戲功能
public void Action()
{
aniCounter ++;//增加計時器
StarAction();//顯示星星的動畫效果
BallAction();//顯示小球的動畫效果
//將圖層繪制的脫機屏幕上
backLayer0.paint(g);
//刷新屏幕
flushGraphics();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -