?? subcanvas.java
字號:
TRIGGER_COUNT = 0;
}else{
TRIGGER_COUNT++;
}
}
image = null;
}
}
//對所有已經存在的敵人潛艇進行tick觸發
Image imageDestroyed = null;
for(int j = 0; j < enemyCollectionVector.size(); j++){
EnemySub enemySub = (EnemySub)enemyCollectionVector.elementAt(j);
int iCount = 0;
//當生命周期結束
if(!enemySub.isLifeState()){
imageDestroyed = SubMIDlet.createImage("/res/enemysub_die.png");
enemySub.setImage(imageDestroyed, imageDestroyed.getWidth(), imageDestroyed.getHeight());
if(enemySub.getVx() >= 0){
enemySub.setTransform(Sprite.TRANS_NONE);
}else{
enemySub.setTransform(Sprite.TRANS_MIRROR);
}
enemyCollectionVector.removeElementAt(j);
//消滅一艘敵人潛艇, 同時更新監聽數組
SpriteChanged spriteChanged = new SpriteChanged(enemySub, layerManager);
spriteChanged.start();
spriteChanged = null;
enemySub = null;
ENEMY_CURRENT--;
ENEMY_MAX--;
}else{
enemySub.tick();
}
}
imageDestroyed = null;
}
/**
* 畫布重畫事件,替代Canvas中的paint()事件, 根據不同的游戲狀態(gameState)畫出游戲畫面
* 本方法由thread每次重新啟動, 最后執行flushGraphics()重畫緩沖區
*/
public synchronized void paintCanvas(Graphics g){
if(gameState == GAME_INIT){
//游戲第一次啟動
//設置為全屏模式并清屏
this.setFullScreenMode(true);
g.setColor(255, 255, 255);
g.fillRect(0, 0, mainWidth, mainHeight);
if(!COMMAND_ADD_FLAG){
//添加響應命令及監聽器
this.addCommand(pauseCommand);
this.addCommand(exitCommand);
this.setCommandListener(this);
COMMAND_ADD_FLAG = true;
}
if(fishCollectionVector != null){
fishCollectionVector = null;
fishCollectionVector = new Vector();
}
if(this.layerManager != null){
this.layerManager = null;
this.layerManager = new LayerManager();
if(userViewWindow){
this.layerManager.setViewWindow(xViewWindow, yViewWindow, wViewWindow, hViewWindow);
}
}
if(mySub != null){
mySub = null;
mySub = new Sub(this, SubMIDlet.createImage("/res/sub.png"), getWidth() / 3, getHeight() / 3, layerManager);
}
//創建背景圖層
this.createSandBackground();
this.createSunkenBoat();
this.createFishCollection(0, FishCollection.NUM_FISH_TYPES);
this.createMysub();
this.createSeaBackground();
mySub.setPosition(getWidth() / 3, getHeight() / 3);
gameState = GAME_RUN;
}else if(gameState == GAME_RUN){
//游戲處于運行狀態
//提供游戲運行標識Flag,保證對用戶操作的響應和"敵人"的運行動作只有在運行的時候生效
//在性能過耗(可用內存不到當前內存總量的4/5時),進行垃圾回收GC
if(rt.freeMemory() < (rt.totalMemory() * 4 / 5)){
rt.gc();
}
}else if(gameState == GAME_SUSPEND){
//下一輪游戲
//更新數據
mySub.setHpLife(15);
mySub.setPosition(mainWidth / 3, mainHeight / 3);
if(PLAYER_LEVEL >= 4){
gameState = GAME_OVER;
}else{
PLAYER_LEVEL ++;
controller.EventHandler(Controller.EVENT_NEXTROUND);
//目前游戲只設計了四級關卡
ENEMY_MAX = PLAYER_LEVEL * 10;
ENEMY_CURRENT = 0;
TRIGGER_COUNT = 0;
TICK_COUNT = 0;
ENEMY_CURRENT_LIMIT = PLAYER_LEVEL * 2;
Layer layer = null;
//暫時刪除layerManager中所有文件,清空魚雷與敵人潛艇數據
//為更新圖層做準備
this.tinfishCollectionVector.removeAllElements();
this.enemyCollectionVector.removeAllElements();
this.fishCollectionVector.removeAllElements();
// for(int i = 0; i < layerManager.getSize(); i++){
// layer = layerManager.getLayerAt(i);
// layerManager.remove(layer);
// }
layer = null;
//更新海底圖層數據
this.SEABACK_DENSITY = (SEABACK_DENSITY + 1) % 4;
gameState = GAME_INIT;
this.unActive();
}
}else if(gameState == GAME_OVER){
//游戲結束
threadAlive = false;
controller.EventHandler(Controller.EVENT_MENU_GAMEOVER);
gameState = this.GAME_INIT;
}
//在緩沖區重畫
this.layerManager.paint(g, 0, (getHeight() - WORLD_HEIGHT) / 2);
this.flushGraphics();
}
public void commandAction(Command command, Displayable display) {
if(command == startCommand){
if(this.gameState == GAME_OVER){
gameState = GAME_INIT;
}else{
gameState = GAME_RUN;
}
this.removeCommand(this.startCommand);
this.addCommand(pauseCommand);
}else if(command == pauseCommand){
gameState = GAME_PAUSE;
this.removeCommand(this.pauseCommand);
this.addCommand(startCommand);
}else if(command == exitCommand){
gameState = GAME_OVER;
}
}
/**
* 創建海底沙地背景圖層
*/
protected void createSandBackground(){
Image bottomTitles = SubMIDlet.createImage("/res/bottom.png");
//將圖片bottomTitles切成指定大小(TILE_WIDTH, TILE_HEIGHT)
//創建一個指定維數(1, WIDTH_IN_TILES)的背景數組
TiledLayer layer = new TiledLayer(WIDTH_IN_TILES, 1,
bottomTitles, TILE_WIDTH, TILE_HEIGHT);
for(int column = 0; column < WIDTH_IN_TILES; column++){
//將海底圖層數組中的每個小格用原始圖片的第i塊來填充
int i = SubMIDlet.createRandom(NUM_DENSITY_LAYER_TILES) + 1;
layer.setCell(column, 0, i);
}
layer.setPosition(0, WORLD_HEIGHT - bottomTitles.getHeight());
layerManager.append(layer);
bottomTitles = null;
}
/**
* 創建海底水層背景圖片
*/
protected void createSeaBackground(){
if(this.SEABACK_DENSITY >= 4){
SEABACK_DENSITY = 0;
}
Image image = SubMIDlet.createImage("/res/densityLayer" + this.SEABACK_DENSITY + ".png");
layerSeaback = new TiledLayer(WIDTH_IN_TILES, HEIGHT_IN_TILES,
image, TILE_WIDTH, TILE_HEIGHT);
for(int row = 0; row < HEIGHT_IN_TILES; row++){
for(int column = 0; column < WIDTH_IN_TILES; column++){
layerSeaback.setCell(column, row, SubMIDlet.createRandom(NUM_DENSITY_LAYER_TILES) + 1);
}
}
layerSeaback.setPosition(0, 0);
layerManager.append(layerSeaback);
image = null;
}
/**
* 創建沉船圖層
*/
protected void createSunkenBoat(){
Image imageSunkenBoat = SubMIDlet.createImage("/res/sunkenBoat.png");
//出現的沉船數量
int numSunkenBoats = 1 + (WORLD_WIDTH / (3 * imageSunkenBoat.getWidth()));
Sprite sunkenBoat;
int bx = 0;
for(int i = 0; i < numSunkenBoats; i++){
sunkenBoat = new Sprite(imageSunkenBoat, imageSunkenBoat.getWidth(), imageSunkenBoat.getHeight());
sunkenBoat.setTransform(this.rotations[SubMIDlet.createRandom(this.rotations.length)]);
//隨機定義沉船位置
bx = (WORLD_WIDTH - imageSunkenBoat.getWidth()) / numSunkenBoats;
bx = (i * bx) + SubMIDlet.createRandom(bx);
sunkenBoat.setPosition(bx, WORLD_HEIGHT - imageSunkenBoat.getHeight());
//添加圖層
this.layerManager.append(sunkenBoat);
//mySub.addCollideable(sunkenBoat);
}
imageSunkenBoat = null;
}
/**
* 創建玩家潛艇
*/
protected void createMysub(){
this.layerManager.append(mySub);
}
/**創建魚群背景
* @param startId
* @param endId
*/
protected void createFishCollection(int startId, int endId){
for(int id = startId; id < endId; id++){
int w = WORLD_WIDTH / 4;
int h = WORLD_HEIGHT / 4;
int x = SubMIDlet.createRandom(WORLD_WIDTH - w);
int y = SubMIDlet.createRandom(WORLD_HEIGHT - h);
int vx = FishCollection.randomVelocity(TILE_WIDTH / 4);
int vy = FishCollection.randomVelocity(TILE_HEIGHT / 4);
//初始化魚類圖層, 同時把圖層添加到圖層管理器上
FishCollection fishCollection = new FishCollection(layerManager, id, x, y, w, h,
vx, vy, 0, 0, WORLD_WIDTH, WORLD_HEIGHT);
this.fishCollectionVector.addElement(fishCollection);
}
}
/**重新設置圖層顯示區域
* @param x 玩家潛艇位置x
* @param y 玩家潛艇位置y
* @param width 玩家潛艇寬
* @param height 玩家潛艇高
*/
public void adjustViewWindow(int xSub, int ySub, int width, int height) {
if (this.userViewWindow)
{
xViewWindow = xSub + (width / 2) - (wViewWindow / 2);
if (xViewWindow < 0)
{
xViewWindow = 0;
}
if (xViewWindow > (WORLD_WIDTH - wViewWindow))
{
xViewWindow = WORLD_WIDTH - wViewWindow;
}
yViewWindow = ySub + (height / 2) - (hViewWindow / 2);
if (yViewWindow < 0)
{
yViewWindow = 0;
}
if (yViewWindow > (WORLD_HEIGHT - hViewWindow))
{
yViewWindow = WORLD_HEIGHT - hViewWindow;
}
layerManager.setViewWindow(xViewWindow,
yViewWindow,
wViewWindow,
hViewWindow);
}
}
/** 獲取玩家潛艇
* @return 返回 mySub。
*/
public Sub getMySub() {
return mySub;
}
/**
* @return 返回 threadAlive。
*/
public boolean isThreadAlive() {
return threadAlive;
}
/**
* @param threadAlive 要設置的 threadAlive。
*/
public void setThreadAlive(boolean threadAlive) {
this.threadAlive = threadAlive;
}
public void active(){
this.showNotify();
}
public void unActive(){
this.hideNotify();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -