J2ME 新手求教

颍川公子 技术经理  2010-09-24 08:16:02
请大家帮忙看看以下代码,如何实现再增加一个球使这两个球同时运动,一个使之沿着屏幕运动,另一个沿着斜线运动并会反弹。 请高手赐教

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;

public class NewClass extends Canvas implements Runnable {

int width;
int height;
int x;
int y;
int v1 = 5;
int v2 = 5;
int x1;
int y1;

public NewClass() {
//获得屏幕的宽高
width = this.getWidth();
height = this.getHeight();
new Thread(this).start();
}

protected void paint(Graphics g) {
//清理屏幕
g.setColor(0, 255, 0);
g.fillRect(0, 0, width, height);

g.setColor(255, 0, 0);
g.fillArc(x, y, 10, 10, 0, 360);

void update() {
x = x + v1;
y = y + v2;
if (x >= this.getWidth() - 10) {
v1 = -v1;
} else if (y >= this.getHeight() - 10) {
v2 = -v2;
} else if (x <= 0) {
v1 = -v1;
} else if (y <= 0) {
v2 = -v2;
}
}
void second() {

x = x + v1;
if (x >= this.getWidth() - 10) {
v1 = 0;
v2 = 5;
y = y + v2;
}else if (y >= this.getHeight() - 10) {
v2 = 0;
v1 = -5;
x = x+v1;
}else if(x == 0){
v1 = 0;
v2 = -5;
y+=v2;
}else if(y == 0){
v2 = 0;
v1 = 5;
x+=v1;
}
}

public void run() {
while (true) {
try {
update();
//second();
repaint();
Thread.sleep(100);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
...全文
75 点赞 收藏 7
写回复
7 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
颍川公子 2010-09-25
能给出完整的代码吗? 那个我试了 但是不行。
回复
麦田捕手 2010-09-25
手机资源很宝贵,一个线程足以完成你的需求了,在run中写两个updata()
回复
QQ153984069 2010-09-25
图片无法上传,我留下MSN:wangjunhui2010@hotmail.com,QQ:153984069
回复
QQ153984069 2010-09-25

import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
//两个图片相向运动,碰撞之后弹回
public class MIDlet2 extends MIDlet {
private MyGameCanvas mgc = new MyGameCanvas();
private Display dis;
protected void startApp() throws MIDletStateChangeException {
dis = Display.getDisplay(this);
dis.setCurrent(mgc);
}

protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub

}

protected void pauseApp() {
// TODO Auto-generated method stub

}

class MyGameCanvas extends GameCanvas implements Runnable{
private Image img1;
private Image img2;
private Sprite sp1;
private Sprite sp2;
private Graphics gra;
private int x1 = 0,y1 = 0;
private int x2 = 200, y2 = 200;
private boolean RUN = true;
private boolean DIR = true;
public MyGameCanvas(){
super(true);
try{
img1 = Image.createImage("/img1.png");
img2 = Image.createImage("/img2.png");
sp1 = new Sprite(img1);
sp2 = new Sprite(img2);
gra = this.getGraphics();
}catch(Exception ex){
ex.printStackTrace();
}
sp1.setPosition(x1, y1);
sp2.setPosition(x2, y2);
new Thread(this).start();
}
public void run(){
while(RUN){
if(DIR){
sp1.move(1, 1); //右下移动
sp2.move(-1, -1); //左上移动
}
else{
sp2.move(1, 1);
sp1.move(-1, -1);
}
gra.setColor(255,255,255);
gra.fillRect(0,0,this.getWidth(),this.getHeight());
sp1.paint(gra);
sp2.paint(gra);
this.flushGraphics();
if(sp1.collidesWith(sp2, true)){//碰撞检测
//RUN = false;
DIR = false;
}
try{
Thread.currentThread().sleep(10);
}catch(Exception ex){}
}

}
}


}
回复
SimonYeung 2010-09-25
[Quote=引用 3 楼 tiewantn 的回复:]
手机资源很宝贵,一个线程足以完成你的需求了,在run中写两个updata()
[/Quote]
这个也行
回复
开两个线程分别控制两个RUN()方法,不就得了
回复
开两个线程啦
回复
相关推荐
发帖
J2ME
创建于2007-09-28

1.3w+

社区成员

Java J2ME
申请成为版主
帖子事件
创建了帖子
2010-09-24 08:16
社区公告
暂无公告