从2个非常不同的类中获取值进行比较

时间:2013-12-16 02:14:16

标签: java arraylist polymorphism parent-child iteration

我有这两个类,Bullet和EnemyJet。 Bullet类扩展了Player类,EnemyJet类扩展了EnemyControl类。他们都使用了一个arraylist,我正试图存储并获得他们的位置。

    public class Player extends Mob {
    public List<Bullet> bullets = new ArrayList<Bullet>();

    }

    public class EnemyControl extends Mob {
    public List<EnemyJet> enemyJet = new ArrayList<EnemyJet>();

    }

我是否需要创建一个新类来存储和测试这两个位置,或者使用它们扩展的Mob类?如何从列表中获取相同的值以在新类中进行比较?

例如,测试与子弹和敌人喷射器的碰撞。如果是这样,请移除喷射。

EnemyJet Class:

public class EnemyJet extends EnemyControl{

Random rdm = new Random();

private int destx, desty;
private Sprite sprite;
private int counter = 0;
private boolean dead = false;
private boolean setPos = false;

private void newPosition(){
    destx = rdm.nextInt(963);
    desty = rdm.nextInt(500);
    //System.out.println(destx + " " + desty);
}

private void move(){
    if(x > destx) x-=3;
    if(y > desty) y-=3;
    if(x < destx) x+=3;
    if(y < desty) y+=3;

//  if(x >= )
}

private void setPos(){
    if(!setPos){
        x = rdm.nextInt(500);
        y = rdm.nextInt(100);
        setPos = true;
    }
}

public void update(){
    setPos();
    move();

    if(counter > rdm.nextInt(20000)){
    newPosition();
    counter = 0;
    }
    counter++;
}

public void render(Screen screen){
    sprite = Sprite.enemyjet;
    screen.renderMob(x, y, sprite);
}
}

Bullet Class:

public class Bullet {
private Sprite sprite;
private boolean removed = false;
public int y = 0, x = 0;

public Bullet(int x, int y){
    this.x = x;
    this.y = y;
}

public boolean isRemoved(){
    return removed;
}

public void update(){
    y -= 15;
}

public void render(Screen screen){
    sprite = Sprite.jetbullet;
    screen.drawBullet(x, y, sprite);
}
}

玩家等级:

public class Player extends Mob {
public List<Bullet> bullets = new ArrayList<Bullet>();

private Keyboard input;
private Sprite sprite;

private int counter = 0;
private int fireRate = 4;

public Player(Keyboard input){
    this.input = input;
}

public void update(){
    int xa = 0, ya = 0;

    if(input.up) ya=-7;
    if(input.down) ya =+7;
    if(input.left) xa=-7;
    if(input.right) xa=+7;

    if(!input.up && !input.down && !input.left && !input.right)
        intensity = 0; else intensity = 8;

    move(xa, ya);

    if(input.shoot && counter > fireRate){
        bullet = new Bullet(x, y);
        bullets.add(bullet);
        counter = 0;
    }

    if(bullets.size() > 0){
        for(int i = 0; i < bullets.size(); i++){
            bullets.get(i).update();
            if(bullets.get(i).y < 0){
                bullets.remove(i);
            }
        }
    }   
    counter++;
}

public void render(Screen screen){
    sprite = Sprite.jet;

    if(bullets.size()>0)
    for(int i = 0; i < bullets.size(); i++){
        bullets.get(i).render(screen);
    }

    screen.renderMob(x, y, sprite);

    screen.setIntensity(intensity);
}

}

添加了所有类Player,EnemyJet和Bullet。

1 个答案:

答案 0 :(得分:1)

你可以检查所有子弹并检查它们是否与敌方喷气机具有相同的位置,并在Mob中实施一种比较它们的方法,如果你将重复使用比较。或者您只是在运行时控制两者的驱动程序类中进行比较,例如: main()或事件被捕获并同步并显示在屏幕上。