一个不断弹起落下的小球的按钮事件问题,当我一直按继续按钮,小球的速度会不断增加,本来不想让它增加,这个怎么解决?附源代码:
package fourth;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class MIDletAction extends MIDlet {
private BallCanvas bc = new BallCanvas();
private Display dis;
public MIDletAction() {
}
protected void startApp() throws MIDletStateChangeException {
dis = Display.getDisplay(this);
dis.setCurrent(bc);
//bc.setTitle("移动的小球");
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
protected void pauseApp() {
}
class BallCanvas extends Canvas implements Runnable,CommandListener{
private int left = 50;
private int top = 50;
private int d = 100;
private int DIR = 1;
private Command cmdon = new Command("继续",Command.SCREEN,1);
private Command cmdpass = new Command("暂停",Command.STOP,1);
private Thread th;
private boolean RUN = true;
public BallCanvas(){
//this.addCommand(cmdon);
this.addCommand(cmdpass);
this.addCommand(cmdon);
this.setCommandListener(this);
th = new Thread(this);
th.start();
}
public void commandAction(Command c ,Displayable d){
if(c == cmdon){
RUN = true;
th = new Thread(this);
th.start();
}else if(c == cmdpass){
RUN = false;
th = null;
}
}
public void paint(Graphics g){
g.setColor(0,255,0);
g.fillRect(0,0,this.getWidth(),this.getHeight());
g.setColor(255,0,0);
g.fillArc(left, top,d,d,0,360);
}
public void run(){
while (RUN){
if(DIR == 1){
top+=3;
d--;
if(top >= this.getHeight()-d){
DIR = 2;
}
}
if(DIR == 2){
top-=3;
d++;
if(top <= 0){
DIR = 1;
}
}
repaint();
try{
Thread.currentThread().sleep(10);
}catch(Exception ex){}
}
}
}
}
package fourth;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class MIDletAction extends MIDlet {
private BallCanvas bc = new BallCanvas();
private Display dis;
public MIDletAction() {
}
protected void startApp() throws MIDletStateChangeException {
dis = Display.getDisplay(this);
dis.setCurrent(bc);
//bc.setTitle("移动的小球");
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
protected void pauseApp() {
}
class BallCanvas extends Canvas implements Runnable,CommandListener{
private int left = 50;
private int top = 50;
private int d = 100;
private int DIR = 1;
private Command cmdon = new Command("继续",Command.SCREEN,1);
private Command cmdpass = new Command("暂停",Command.STOP,1);
private Thread th;
private boolean RUN = true;
public BallCanvas(){
//this.addCommand(cmdon);
this.addCommand(cmdpass);
this.addCommand(cmdon);
this.setCommandListener(this);
th = new Thread(this);
th.start();
}
public void commandAction(Command c ,Displayable d){
if(c == cmdon){
RUN = true;
th = new Thread(this);
th.start();
}else if(c == cmdpass){
RUN = false;
th = null;
}
}
public void paint(Graphics g){
g.setColor(0,255,0);
g.fillRect(0,0,this.getWidth(),this.getHeight());
g.setColor(255,0,0);
g.fillArc(left, top,d,d,0,360);
}
public void run(){
while (RUN){
if(DIR == 1){
top+=3;
d--;
if(top >= this.getHeight()-d){
DIR = 2;
}
}
if(DIR == 2){
top-=3;
d++;
if(top <= 0){
DIR = 1;
}
}
repaint();
try{
Thread.currentThread().sleep(10);
}catch(Exception ex){}
}
}
}
}