新手求指点,坦克大战的问题

Kamoe 2013-04-04 10:58:14
//这个是Tank类里的一个方法
public Bullet fire() {
int x = this.x + MyTank.WIDTH/2 - Bullet.WIDTH/2;
int y = this.y + MyTank.HEIGH/2 - Bullet.HEIGH/2;
Bullet bullet = new Bullet(x, y, barrelDir, tc);
tc.bullets.add(bullet);//运行的时候出现空指针错误,说是这里,好像是没添加进去,用DeBug查的时候也是在这里就跳进了一个很多很长的代码里,不知道为什么会出现这种错误,求解,如果代码不详细我还会再贴,谢谢了
return bullet;
}

我在TankClient类里有

List<Bullet> bullets = new ArrayList<Bullet>();
....
public void paint(Graphics g) {
myTank.draw(g);

for(int i=0; i<bullets.size(); i++) {
bullets.get(i).draw(g);
}
}
...全文
228 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
tc是什么?初始化了吗?
a_a\\\ 2013-04-04
  • 打赏
  • 举报
回复
tc应该没有new吧!空指针的异常,一般都是因为某些对象没有实例化。建议你认真检查一下对象有没有实例化或赋值
a_a\\\ 2013-04-04
  • 打赏
  • 举报
回复
引用 9 楼 inuyashazh 的回复:
package tankwar; import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import java.awt.Toolkit; import java.awt.event.KeyEvent; public class MyTank { int ……
你没有new, TankClient tc = new TankClient();
a_a\\\ 2013-04-04
  • 打赏
  • 举报
回复

package tankwar;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;



public class MyTank {

int x;
int y;
public static final int WIDTH = 30;
public static final int HEITHT = 30;
int X_SPEED = 5;
int Y_SPEED = 5;

public MyTank(int x, int y, Direction dir) {
this.x = x;
this.y = y;
this.dir = dir;
}

private boolean isLeft = false;
private boolean isUp = false;
private boolean isRight = false;
private boolean isDown = false;
enum Direction {
L, LU, U, RU, R, RD, D, LD, STOP
};

public TankClient tc = null;        //这里写null了,请检查

public Direction dir = Direction.L;
Direction barrelDir = dir;

public void draw(Graphics g) {
Color c = g.getColor();
g.setColor(Color.green);
g.fillOval(this.x, this.y, WIDTH, HEITHT);
g.setColor(c);
barrelDraw(g);
move();
}

public void barrelDraw(Graphics g) {
Color c = g.getColor();
g.setColor(Color.BLACK);
switch (barrelDir) {
case L:
g.drawLine(this.x + WIDTH/2, this.y + HEITHT/2, this.x, this.y + WIDTH/2);
break;
case LU:
g.drawLine(this.x + WIDTH/2, this.y + HEITHT/2, this.x, this.y);
break;
case U:
g.drawLine(this.x + WIDTH/2, this.y + HEITHT/2, this.x + WIDTH/2, this.y);
break;
case RU:
g.drawLine(this.x + WIDTH/2, this.y + HEITHT/2, this.x + WIDTH, this.y);
break;
case R:
g.drawLine(this.x + WIDTH/2, this.y + HEITHT/2, this.x + WIDTH, this.y + HEITHT/2);
break;
case RD:
g.drawLine(this.x + WIDTH/2, this.y + HEITHT/2, this.x + WIDTH, this.y + HEITHT);
break;
case D:
g.drawLine(this.x + WIDTH/2, this.y + HEITHT/2, this.x + WIDTH/2, this.y + HEITHT);
break;
case LD:
g.drawLine(this.x + WIDTH/2, this.y + HEITHT/2, this.x, this.y + HEITHT);
break;
case STOP:
break;
}
}

public void move() {
switch (dir) {
case L:
x = x - X_SPEED;
break;
case LU:
x = x - X_SPEED;
y = y - Y_SPEED;
break;
case U:
y = y - Y_SPEED;
break;
case RU:
x = x + X_SPEED;
y = y - Y_SPEED;
break;
case R:
x = x + X_SPEED;
break;
case RD:
x = x + X_SPEED;
y = y + Y_SPEED;
break;
case D:
y = y + Y_SPEED;
break;
case LD:
x = x - X_SPEED;
y = y + Y_SPEED;
break;
case STOP:
break;
}

if(this.dir != Direction.STOP) {
this.barrelDir = this.dir;
}

if(isLeft == true && isUp == false && isRight == false && isDown == false) dir = Direction.L;
if(isLeft == false && isUp == true && isRight == false && isDown == false) dir = Direction.U;
if(isLeft == false && isUp == false && isRight == true && isDown == false) dir = Direction.R;
if(isLeft == false && isUp == false && isRight == false && isDown == true) dir = Direction.D;

if(isLeft == true && isUp == true && isRight == false && isDown == false) dir = Direction.LU;
if(isLeft == false && isUp == true && isRight == true && isDown == false) dir = Direction.RU;
if(isLeft == false && isUp == false && isRight == true && isDown == true) dir = Direction.RD;
if(isLeft == true && isUp == false && isRight == false && isDown == true) dir = Direction.LD;
if(isLeft == false && isUp == false && isRight == false && isDown == false) dir = Direction.STOP;
}

public Bullet fire() {
int x = this.x + MyTank.WIDTH/2 - Bullet.WIDTH/2;
int y = this.y + MyTank.HEITHT/2 - Bullet.HEIGHT/2;
Bullet bullet = new Bullet(x, y, barrelDir);
tc.bullets.add(bullet);
return bullet;
}

public void KeyPressed(KeyEvent e) {
int key = e.getKeyCode();
switch (key) {
case KeyEvent.VK_LEFT:
isLeft = true;
break;
case KeyEvent.VK_UP:
isUp = true;
break;
case KeyEvent.VK_RIGHT:
isRight = true;
break;
case KeyEvent.VK_DOWN:
isDown = true;
break;
}

}

public void KeyReleased(KeyEvent e) {
int key = e.getKeyCode();
switch (key) {
case KeyEvent.VK_LEFT:
isLeft = false;
break;
case KeyEvent.VK_UP:
isUp = false;
break;
case KeyEvent.VK_RIGHT:
isRight = false;
break;
case KeyEvent.VK_DOWN:
isDown = false;
break;
case KeyEvent.VK_SPACE:
fire();
}
}

}


Kamoe 2013-04-04
  • 打赏
  • 举报
回复
package tankwar; import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import java.awt.Toolkit; import java.awt.event.KeyEvent; public class MyTank { int x; int y; public static final int WIDTH = 30; public static final int HEITHT = 30; int X_SPEED = 5; int Y_SPEED = 5; public MyTank(int x, int y, Direction dir) { this.x = x; this.y = y; this.dir = dir; } private boolean isLeft = false; private boolean isUp = false; private boolean isRight = false; private boolean isDown = false; enum Direction { L, LU, U, RU, R, RD, D, LD, STOP }; public TankClient tc = null; public Direction dir = Direction.L; Direction barrelDir = dir; public void draw(Graphics g) { Color c = g.getColor(); g.setColor(Color.green); g.fillOval(this.x, this.y, WIDTH, HEITHT); g.setColor(c); barrelDraw(g); move(); } public void barrelDraw(Graphics g) { Color c = g.getColor(); g.setColor(Color.BLACK); switch (barrelDir) { case L: g.drawLine(this.x + WIDTH/2, this.y + HEITHT/2, this.x, this.y + WIDTH/2); break; case LU: g.drawLine(this.x + WIDTH/2, this.y + HEITHT/2, this.x, this.y); break; case U: g.drawLine(this.x + WIDTH/2, this.y + HEITHT/2, this.x + WIDTH/2, this.y); break; case RU: g.drawLine(this.x + WIDTH/2, this.y + HEITHT/2, this.x + WIDTH, this.y); break; case R: g.drawLine(this.x + WIDTH/2, this.y + HEITHT/2, this.x + WIDTH, this.y + HEITHT/2); break; case RD: g.drawLine(this.x + WIDTH/2, this.y + HEITHT/2, this.x + WIDTH, this.y + HEITHT); break; case D: g.drawLine(this.x + WIDTH/2, this.y + HEITHT/2, this.x + WIDTH/2, this.y + HEITHT); break; case LD: g.drawLine(this.x + WIDTH/2, this.y + HEITHT/2, this.x, this.y + HEITHT); break; case STOP: break; } } public void move() { switch (dir) { case L: x = x - X_SPEED; break; case LU: x = x - X_SPEED; y = y - Y_SPEED; break; case U: y = y - Y_SPEED; break; case RU: x = x + X_SPEED; y = y - Y_SPEED; break; case R: x = x + X_SPEED; break; case RD: x = x + X_SPEED; y = y + Y_SPEED; break; case D: y = y + Y_SPEED; break; case LD: x = x - X_SPEED; y = y + Y_SPEED; break; case STOP: break; } if(this.dir != Direction.STOP) { this.barrelDir = this.dir; } if(isLeft == true && isUp == false && isRight == false && isDown == false) dir = Direction.L; if(isLeft == false && isUp == true && isRight == false && isDown == false) dir = Direction.U; if(isLeft == false && isUp == false && isRight == true && isDown == false) dir = Direction.R; if(isLeft == false && isUp == false && isRight == false && isDown == true) dir = Direction.D; if(isLeft == true && isUp == true && isRight == false && isDown == false) dir = Direction.LU; if(isLeft == false && isUp == true && isRight == true && isDown == false) dir = Direction.RU; if(isLeft == false && isUp == false && isRight == true && isDown == true) dir = Direction.RD; if(isLeft == true && isUp == false && isRight == false && isDown == true) dir = Direction.LD; if(isLeft == false && isUp == false && isRight == false && isDown == false) dir = Direction.STOP; } public Bullet fire() { int x = this.x + MyTank.WIDTH/2 - Bullet.WIDTH/2; int y = this.y + MyTank.HEITHT/2 - Bullet.HEIGHT/2; Bullet bullet = new Bullet(x, y, barrelDir); tc.bullets.add(bullet); return bullet; } public void KeyPressed(KeyEvent e) { int key = e.getKeyCode(); switch (key) { case KeyEvent.VK_LEFT: isLeft = true; break; case KeyEvent.VK_UP: isUp = true; break; case KeyEvent.VK_RIGHT: isRight = true; break; case KeyEvent.VK_DOWN: isDown = true; break; } } public void KeyReleased(KeyEvent e) { int key = e.getKeyCode(); switch (key) { case KeyEvent.VK_LEFT: isLeft = false; break; case KeyEvent.VK_UP: isUp = false; break; case KeyEvent.VK_RIGHT: isRight = false; break; case KeyEvent.VK_DOWN: isDown = false; break; case KeyEvent.VK_SPACE: fire(); } } } 这是tank类里的全部代码
Kamoe 2013-04-04
  • 打赏
  • 举报
回复
引用 5 楼 cfk942315386 的回复:
tc应该没有实例化吧
写了,实例化后也不行,还是空指针
Kamoe 2013-04-04
  • 打赏
  • 举报
回复
引用 6 楼 lye2000000_super 的回复:
没new肯定会报空指针异常了。。。你这样只是定义了个变量。但是没有个变量赋值。那就是null。tc = new TankClient();这样之后才能使用。不然就是使用null
这些也试过了,还是空指针异常,不加tc也是报nullpoint,感觉好像不是tc的事情
  • 打赏
  • 举报
回复
没new肯定会报空指针异常了。。。你这样只是定义了个变量。但是没有个变量赋值。那就是null。tc = new TankClient();这样之后才能使用。不然就是使用null
cfk逗号 2013-04-04
  • 打赏
  • 举报
回复
tc应该没有实例化吧
Kamoe 2013-04-04
  • 打赏
  • 举报
回复
引用 1 楼 jie873440996 的回复:
tc应该没有new吧!空指针的异常,一般都是因为某些对象没有实例化。建议你认真检查一下对象有没有实例化或赋值
恩,但是不+tc也是有空指针异常
Kamoe 2013-04-04
  • 打赏
  • 举报
回复
引用 2 楼 lye2000000_super 的回复:
tc是什么?初始化了吗?
tc 是TankClient的对象,我每个类都引入了一个TanClient的对象,我是这么初始化的 TankCliet tc; 他没有带参数的构造函数

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧