当前位置:编程学习 > JAVA >>

GameCanvas中如何实现notifyDestroyed();的功能

RT 
  notifyDestroyed() 是 MIlet中的功能  如果要在GameCanvas类中实现这个功能  。
  应该怎么改  ? 代码如下:
   
/*  ********  GameCanvas类  ************       */

import java.util.Random;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.Sprite;

public class CarRaceCanvas extends GameCanvas implements CommandListener,Runnable {

private Graphics gra;

 

/************************用户汽车和5种对手汽车*************************/
private Sprite myCar;
private Sprite[] enemy = new Sprite[5];
/************************命令按钮***********************************/
private Command cmdStart = new Command("开始游戏", Command.SCREEN, 1);
private Command cmdStop = new Command("暂停游戏", Command.SCREEN, 1);
private Command cmdExit = new Command("退出游戏", Command.EXIT, 1);
/************************游戏线程***********************************/
private Thread game;
private boolean loop = true;

private int road;
private Random rnd = new Random();
private int score = 100;

private Font font = null;

public CarRaceCanvas() {
super(true);
this.prepareResource();
this.addCommand(cmdStart);
this.addCommand(cmdExit);
this.setCommandListener(this);

gra = this.getGraphics();
font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE);
}

public void prepareResource() {
/************************载入图片***********************************/
try {
Image myImage = Image.createImage("/mycar.gif");
myCar = new Sprite(myImage);
for (int i = 0; i < enemy.length; i++) {
Image enImg = Image.createImage("/enemy.gif");
enemy[i] = new Sprite(enImg);
}

} catch (Exception ex) {
ex.printStackTrace();
}
for (int i = 0; i < enemy.length; i++) {
setEnemy(i);
}
//将用户汽车画在界面正中央
myCar.setPosition((this.getWidth()-myCar.getWidth())/2, 
(this.getHeight()-myCar.getHeight())/2);
}

//设定第en个对手汽车的随机位置
public void setEnemy(int en) {
int x, y;
while (true) {
x = rnd.nextInt((int) this.getWidth() - enemy[en].getWidth());            
//取纵坐标为界面高度-10倍范围内的随机数
y = -rnd.nextInt(this.getHeight()*10);
for (int j = 0; j < enemy.length; j++) {
//不能让汽车和别的对手汽车重叠
if (j != en && enemy [j].collidesWith(enemy [en], true)){
continue;
}
}
enemy [en].setPosition(x, y);
break;
}
}

public void commandAction(Command cmd, Displayable dis) {
if (cmd == cmdStart) {
loop = true;
game = new Thread(this);
game.start();
this.removeCommand(cmdStart);
this.addCommand(cmdStop);
} else if (cmd == cmdStop) {
loop = false;
game = null;
this.removeCommand(cmdStop);
this.addCommand(cmdStart);
}else if(cmd==cmdExit){
loop=false;
game=null;
this.removeCommand(cmdExit);
  //destroyApp(true);                     
 //   notifyDestroyed();           /* 错误所在。notifyDestroyed()只能在MIDlet 中使用。要是在这个类中怎么将功能实现    */
}
}

public void run() {
while (loop) {
drawScreen();
int state = this.getKeyStates();
switch(state){
case GameCanvas.LEFT_PRESSED:
myCar.move(-10, 0);
break;
case GameCanvas.RIGHT_PRESSED:
myCar.move(10, 0);
break;
case GameCanvas.UP_PRESSED:
myCar.move(0, -10);
break;
case GameCanvas.DOWN_PRESSED:
myCar.move(0, 10);
break;
}
try {
Thread.sleep(50);
} catch (Exception e) {
}
}
}

public void drawScreen() {
//将界面背景用灰色清空
gra.setColor(120, 120, 120);
gra.fillRect(0, 0, this.getWidth(), this.getHeight());
//画道路
drawRoad();
//画出当前分数
gra.setColor(255,0,0);
gra.setFont(font);
gra.drawString("当前分数:"+score, this.getWidth()/2, 0, Graphics.TOP|Graphics.HCENTER);
//对手车辆下移
for (int i = 0; i < enemy.length; i++) {
enemy[i].move(0, 15);
enemy[i].paint(gra);
if (enemy[i].getY() > this.getHeight()){
setEnemy(i);
}
//判断是否碰撞
check(enemy[i]);
}
myCar.paint(gra);
this.flushGraphics();
}

public void drawRoad() {
road += 80;
//画道路中间的白线
gra.setColor(255, 255, 255);
gra.fillRect((int) this.getWidth()/2-10, road, 20, 150);
if (road >= this.getHeight()) {
road = -150;
}
}

public void check(Sprite en) {
if (myCar.collidesWith(en, true)) {
score--;
//如果用户汽车左侧撞到对手汽车
if (myCar.getX() > en.getX()) {
en.move(-20, 0);
myCar.move(20, 0);

//如果用户汽车右侧撞到对手汽车
else {
en.move(20, 0);
myCar.move(-20, 0);
}
}
if(score==0){
loop = false;
game = null;
//将界面背景用灰色清空
gra.setColor(120, 120, 120);
gra.fillRect(0, 0, this.getWidth(), this.getHeight());
gra.setColor(255,0,0);
gra.setFont(font);
gra.drawString("您输了", this.getWidth()/2, 0, Graphics.TOP|Graphics.HCENTER);
    this.removeCommand(cmdStart);
    this.removeCommand(cmdStop);
}
}
}


/********  MIDlet类    **********/

import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class CarRaceMIDlet extends MIDlet {
private CarRaceCanvas canvas = new CarRaceCanvas();
private Display dis;
protected void startApp() throws MIDletStateChangeException {
dis = Display.getDisplay(this);
dis.setCurrent(canvas);

}
protected void pauseApp() {}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {}


}
  应该如何改?   谢谢 J2ME  notifyDestroyed()    GameCanvas
补充:Java ,  J2ME
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,