在尝试创建不断追赶由用户控制的单个对象(称为“角色”)的“生成”对象(称为“ Rushers”)时遇到了一些麻烦。我的主要问题是Rushers的座标似乎与角色的座标完全相同。
我将代码分为几个类。这是角色第一个的代码:
public class Gamepanel {
private boolean right = false, left = false, up = false, down = false;
public Character mainChar;
private static int xCoor = 230;
private static int yCoor = 210;
这是主循环:
public void tick(){
mainChar = new Character(xCoor, yCoor, 30, 50);
mainChar.setxCoor(xCoor);
mainChar.setyCoor(yCoor);
if(right && mainChar.getxCoor() < 469) xCoor+=5;
if(left && mainChar.getxCoor() > 0) xCoor -= 5;
if(up && mainChar.getyCoor() > 0) yCoor -= 5;
if(down && mainChar.getyCoor() < 449) yCoor+=5;
}
GFX:
mainChar.draw(g);
控件是基本的keyPressed和keyReleased方法。为了简洁起见,我不会发布它们,因为它们似乎运行良好。
这是Character类的代码:
public class Character {
int xCoor = 230;
int yCoor = 210;
int width = 30;
int height = 50;
public Character(int xCoor, int yCoor, int width, int height) {
this.xCoor = xCoor;
this.yCoor = yCoor;
}
public void draw(Graphics g) {
g.setColor(Color.GREEN);
g.fillRect(xCoor, yCoor, width, height);
}
public int getxCoor() {
return xCoor;
}
public void setxCoor(int xCoor) {
this.xCoor = xCoor;
}
public int getyCoor() {
return yCoor;
}
public void setyCoor(int yCoor) {
this.yCoor = yCoor;
}
}
这些似乎都正常运行,没有大的问题。我遇到的最大问题是我的其他物件,Rushers。这是他们在游戏面板中的代码:
public class Gamepanel {
private Rusher rusher;
ArrayList<Rusher> rushers;
int spawnTimer = 0;
public Gamepanel() {
rushers = new ArrayList <Rusher>();
}
public void tick(){
spawnTimer++;
if(spawnTimer > 75) {
spawn();
spawnTimer = 0;
}
if(rushers.size() > 0) {
for(int i = 0; i < rushers.size(); i++) {
rushers.get(i).tick();
}
}
}
GFX:
if(rushers.size() > 0) {
for(int i = 0 ; i < rushers.size(); i++) {
rushers.get(i).draw(g);
}
}
Spawn方法(非常确定错误在这里):
public void spawn() {
int xSpawn[] = new int[4];
int ySpawn[] = new int[4];
ySpawn[0] = 250; ySpawn[1] = 499; ySpawn[2] = 250; ySpawn[3] = 0;
xSpawn[0] = 0; xSpawn[1] = 250; xSpawn[2] = 499; xSpawn[3] = 250;
int spawnCoor = randomRange(0, 3);
rusher = new Rusher(xSpawn[spawnCoor], ySpawn[spawnCoor], 10, 10);
rusher.setxCoor(xSpawn[spawnCoor]);
rusher.setyCoor(ySpawn[spawnCoor]);
rushers.add(rusher);
}
最后是Rusher类:
public class Rusher {
private static int xCoor;
private static int yCoor;
private int width = 20;
private int height = 20;
public Rusher(int xCoor, int yCoor, int width, int height) {
super(xCoor, yCoor, width, height);
Rusher.xCoor = xCoor;
Rusher.yCoor = yCoor;
}
public void tick() {
if(xCoor > Gamepanel.mainxCoor()) xCoor -= 2;
if(yCoor > Gamepanel.mainyCoor()) yCoor -= 2;
if(xCoor < Gamepanel.mainxCoor()) xCoor += 2;
if(yCoor < Gamepanel.mainyCoor()) yCoor += 2;
}
public void draw(Graphics g) {
g.setColor(Color.RED);
g.fillRect(xCoor, yCoor, width, height);
}
public int getxCoor() {
return xCoor;
}
public void setxCoor(int xCoor) {
Rusher.xCoor = xCoor;
}
public int getyCoor() {
return yCoor;
}
public void setyCoor(int yCoor) {
Rusher.yCoor = yCoor;
}
}
回顾一下,主要问题是Rushers的生成坐标与角色相同,尽管我将其运动速度设置为比角色慢3,但它们的移动速度也一样快。我试图使这一过程尽可能简短。如果需要更多代码,我的确有一些与这些对象有关的代码。
谢谢。
答案 0 :(得分:0)
我发现了。我将Rushers的坐标设置为静态,因此它们都共享相同的坐标。当我更改它并调整其他一些较小的内容时,效果会更好。