J2ME关于一个不断弹起落下的小球的按钮事件问题

一个不断弹起落下的小球的按钮事件问题,当我一直按继续按钮,小球的速度会不断增加,本来不想让它增加,这个怎么解决?附源代码:
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){}
}
}

}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值