簡體   English   中英

從兩個截然不同的類中獲取要比較的值

[英]Getting values to compare from 2 very different classes

我有這兩個課程,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類別:

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);
}
}

子彈班:

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。

您可以檢查所有子彈,並檢查它們是否與敵機位置相同,並在Mob中實現一種方法,如果您要重復使用比較,則可以對它們進行比較。 或者,您只需要自己在驅動程序類中進行比較,該類既可以在運行時進行控制,例如main(),又可以捕獲,同步和在屏幕上顯示事件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM