Java LibGDX:使随机和不同的管道出现(Flappy Bird风格的游戏)

Java LibGDX:使随机和不同的管道出现(Flappy Bird风格的游戏),java,android,random,libgdx,Java,Android,Random,Libgdx,我是Android游戏制作领域的新手,在我的第一个游戏中,我成功地制作了一个Flappy Bird克隆,多亏了LibGdx、coffee和Youtube。然而,为了使它不是一个完全的克隆,我正在尝试添加不同类型的管道,这就是我需要帮助的地方 我试图使4种不同类型的管道(或树,就像我的游戏中的一样)随机出现,使其不可预测,但到目前为止,我只设法使1种类型出现。以下是我用于其中两种类型和播放状态的代码 HTree类实现: public class HTree { public static fina

我是Android游戏制作领域的新手,在我的第一个游戏中,我成功地制作了一个Flappy Bird克隆,多亏了LibGdx、coffee和Youtube。然而,为了使它不是一个完全的克隆,我正在尝试添加不同类型的管道,这就是我需要帮助的地方

我试图使4种不同类型的管道(或树,就像我的游戏中的一样)随机出现,使其不可预测,但到目前为止,我只设法使1种类型出现。以下是我用于其中两种类型和播放状态的代码

HTree
类实现:

public class HTree {
public static final int TREE_WIDTH = 52;
private static final int FLUCTUACTION = 130;
private static final int TREE_GAP = 100;
private static final int LOWEST_OPENING = 120;
private Texture toptree,bottomtree;
private Vector2 postoptree,posbottree;
private Rectangle boundsTop,boundsBot;
private Random rand;
public HTree(float x){
    toptree = new Texture("uppertree.png");

    bottomtree = new Texture("bottomtree.png");

    rand = new Random();

    postoptree = new Vector2(x, rand.nextInt(FLUCTUACTION) + TREE_GAP + LOWEST_OPENING);

    posbottree = new Vector2(x, postoptree.y - TREE_GAP - bottomtree.getHeight());

    boundsTop = new Rectangle(getPostoptree().x,getPostoptree().y, toptree.getWidth(), toptree.getHeight());

    boundsBot = new Rectangle(getPosbottree().x,getPosbottree().y, bottomtree.getWidth(), bottomtree.getHeight());


}

public Texture getToptree() {
    return toptree;

}

public Texture getBottomtree() {
    return bottomtree;

}

public Vector2 getPostoptree() {
    return postoptree;

}

public Vector2 getPosbottree() {
    return posbottree;
}

public void reposition(float x){
    postoptree.set(x, rand.nextInt(FLUCTUACTION) + TREE_GAP + LOWEST_OPENING);
    posbottree.set(x, postoptree.y - TREE_GAP - bottomtree.getHeight());
    boundsTop.setPosition(postoptree.x,postoptree.y);
    boundsBot.setPosition(posbottree.x,posbottree.y);
}

public boolean collides(Rectangle player){
    return player.overlaps(boundsTop) || player.overlaps(boundsBot);
}

public void dispose(){
    toptree.dispose();
    bottomtree.dispose();
}

}
public class HTree2 {
public static final int TREE_WIDTH = 52;
private static final int FLUCTUACTION = 130;
private static final int TREE_GAP = 100;
private static final int LOWEST_OPENING = 120;
private Texture toptree2,bottomtree2;
private Vector2 postoptree2,posbottree2;
private Rectangle boundsTop2,boundsBot2;
private Random rand;
public HTree2(float x){
    toptree2 = new Texture("uppertree2.png");

    bottomtree2 = new Texture("bottomtree2.png");

    rand = new Random();

    postoptree2 = new Vector2(x, rand.nextInt(FLUCTUACTION) + TREE_GAP + LOWEST_OPENING);

    posbottree2 = new Vector2(x, postoptree2.y - TREE_GAP - bottomtree2.getHeight());

    boundsTop2 = new Rectangle(getPostoptree().x,getPostoptree().y, toptree2.getWidth(), toptree2.getHeight());

    boundsBot2 = new Rectangle(getPosbottree().x,getPosbottree().y, bottomtree2.getWidth(), bottomtree2.getHeight());


}

public Texture getToptree() {
    return toptree2;

}

public Texture getBottomtree() {
    return bottomtree2;

}

public Vector2 getPostoptree() {
    return postoptree2;

}

public Vector2 getPosbottree() {
    return posbottree2;
}

public void reposition2(float x){
    postoptree2.set(x, rand.nextInt(FLUCTUACTION) + TREE_GAP + LOWEST_OPENING);
    posbottree2.set(x, postoptree2.y - TREE_GAP - bottomtree2.getHeight());
    boundsTop2.setPosition(postoptree2.x,postoptree2.y);
    boundsBot2.setPosition(posbottree2.x,posbottree2.y);
}

public boolean collides2(Rectangle player){
    return player.overlaps(boundsTop2) || player.overlaps(boundsBot2);
}

public void dispose2(){
    toptree2.dispose();
    bottomtree2.dispose();
}

}
public class HPlayState extends HState {
private static final int TREE_SPACING = 125;
private static final int TREE_COUNT = 4;
private static final int GROUND_Y_OFFSET = -50;


private HBird bird;
private Texture bg;
private Texture ground;
private Vector2 groundPos1,groundPos2;
private Array<HTree> trees;
private Array<HTree2> trees2;



public HPlayState(HGameStateManager gsm) {
    super(gsm);
    bird = new HBird(50,300);
    cam.setToOrtho(false, HBonGame.WIDTH/2,HBonGame.HEIGHT/2);
    bg = new Texture("background3.jpg");
    ground = new Texture("ground.png");
    groundPos1 = new Vector2(cam.position.x-cam.viewportWidth/2,GROUND_Y_OFFSET);
    groundPos2 = new Vector2((cam.position.x-cam.viewportWidth/2) + ground.getWidth(),GROUND_Y_OFFSET);
    trees = new Array<HTree>();
    trees2 = new Array<HTree2>();

    for(int i=1; i <= TREE_COUNT; i++){
        trees.add(new HTree(i * (TREE_SPACING + HTree.TREE_WIDTH)));
    }
    for (int i=1; i >= TREE_COUNT; i++){
        trees2.add(new HTree2(i * (TREE_SPACING + HTree2.TREE_WIDTH)));
    }

}

@Override
protected void handleInput() {
    if (Gdx.input.justTouched())
        bird.jump();

}

@Override
public void update(float dt) {
    handleInput();
    updateGround();
    bird.update(dt);
    cam.position.x = bird.getPosition().x + 80;
    for (int i=0; i < trees.size; i++ ){
        HTree tree = trees.get(i);

        if (cam.position.x - (cam.viewportWidth / 2) > tree.getPostoptree().x + tree.getToptree().getWidth()){
            tree.reposition(tree.getPostoptree().x + ((HTree.TREE_WIDTH + TREE_SPACING) * TREE_COUNT));
        }

        if(tree.collides(bird.getBounds())){
            gsm.set(new HGameOverState(gsm));
        }
    }
    for (int i=0; i < trees2.size; i++ ){
        HTree2 tree2 = trees2.get(i);

        if (cam.position.x - (cam.viewportWidth / 2) > tree2.getPostoptree().x + tree2.getToptree().getWidth()){
            tree2.reposition2(tree2.getPostoptree().x + ((HTree2.TREE_WIDTH + TREE_SPACING) * TREE_COUNT));
        }

        if(tree2.collides2(bird.getBounds())){
            gsm.set(new HGameOverState(gsm));
        }
    }
    if (bird.getPosition().y <= ground.getHeight()+GROUND_Y_OFFSET){
        gsm.set(new HGameOverState(gsm));
    }
    cam.update();

}

@Override
public void render(SpriteBatch sb) {
    sb.setProjectionMatrix(cam.combined);
    sb.begin();
    sb.draw(bg,cam.position.x - (cam.viewportWidth/2), 0);
    sb.draw(bird.getTexture(),bird.getPosition().x,bird.getPosition().y);
    for (HTree tree : trees) {
        sb.draw(tree.getToptree(), tree.getPostoptree().x, tree.getPostoptree().y);
        sb.draw(tree.getBottomtree(), tree.getPosbottree().x, tree.getPosbottree().y);
    }
    for (HTree2 tree2 : trees2) {
        sb.draw(tree2.getToptree(), tree2.getPostoptree().x, tree2.getPostoptree().y);
        sb.draw(tree2.getBottomtree(), tree2.getPosbottree().x, tree2.getPosbottree().y);
    }
    sb.draw(ground, groundPos1.x,groundPos1.y);
    sb.draw(ground, groundPos2.x,groundPos2.y);
    sb.end();
}

@Override
public void dispose() {
    bg.dispose();
    bird.dispose();
    ground.dispose();
    for (HTree tree : trees){
        tree.dispose();

    }
    for (HTree2 tree2 : trees2){
        tree2.dispose2();

    }


}

private void updateGround() {
    if (cam.position.x - (cam.viewportWidth / 2) > groundPos1.x + ground.getWidth())
        groundPos1.add(ground.getWidth() * 2, 0);
    if (cam.position.x - (cam.viewportWidth / 2) > groundPos2.x + ground.getWidth())
        groundPos2.add(ground.getWidth() * 2, 0);



}
@Override
public void resize(int width, int height) {
    bird = new HBird(50,300);
}
}
HTree2
类实现:

public class HTree {
public static final int TREE_WIDTH = 52;
private static final int FLUCTUACTION = 130;
private static final int TREE_GAP = 100;
private static final int LOWEST_OPENING = 120;
private Texture toptree,bottomtree;
private Vector2 postoptree,posbottree;
private Rectangle boundsTop,boundsBot;
private Random rand;
public HTree(float x){
    toptree = new Texture("uppertree.png");

    bottomtree = new Texture("bottomtree.png");

    rand = new Random();

    postoptree = new Vector2(x, rand.nextInt(FLUCTUACTION) + TREE_GAP + LOWEST_OPENING);

    posbottree = new Vector2(x, postoptree.y - TREE_GAP - bottomtree.getHeight());

    boundsTop = new Rectangle(getPostoptree().x,getPostoptree().y, toptree.getWidth(), toptree.getHeight());

    boundsBot = new Rectangle(getPosbottree().x,getPosbottree().y, bottomtree.getWidth(), bottomtree.getHeight());


}

public Texture getToptree() {
    return toptree;

}

public Texture getBottomtree() {
    return bottomtree;

}

public Vector2 getPostoptree() {
    return postoptree;

}

public Vector2 getPosbottree() {
    return posbottree;
}

public void reposition(float x){
    postoptree.set(x, rand.nextInt(FLUCTUACTION) + TREE_GAP + LOWEST_OPENING);
    posbottree.set(x, postoptree.y - TREE_GAP - bottomtree.getHeight());
    boundsTop.setPosition(postoptree.x,postoptree.y);
    boundsBot.setPosition(posbottree.x,posbottree.y);
}

public boolean collides(Rectangle player){
    return player.overlaps(boundsTop) || player.overlaps(boundsBot);
}

public void dispose(){
    toptree.dispose();
    bottomtree.dispose();
}

}
public class HTree2 {
public static final int TREE_WIDTH = 52;
private static final int FLUCTUACTION = 130;
private static final int TREE_GAP = 100;
private static final int LOWEST_OPENING = 120;
private Texture toptree2,bottomtree2;
private Vector2 postoptree2,posbottree2;
private Rectangle boundsTop2,boundsBot2;
private Random rand;
public HTree2(float x){
    toptree2 = new Texture("uppertree2.png");

    bottomtree2 = new Texture("bottomtree2.png");

    rand = new Random();

    postoptree2 = new Vector2(x, rand.nextInt(FLUCTUACTION) + TREE_GAP + LOWEST_OPENING);

    posbottree2 = new Vector2(x, postoptree2.y - TREE_GAP - bottomtree2.getHeight());

    boundsTop2 = new Rectangle(getPostoptree().x,getPostoptree().y, toptree2.getWidth(), toptree2.getHeight());

    boundsBot2 = new Rectangle(getPosbottree().x,getPosbottree().y, bottomtree2.getWidth(), bottomtree2.getHeight());


}

public Texture getToptree() {
    return toptree2;

}

public Texture getBottomtree() {
    return bottomtree2;

}

public Vector2 getPostoptree() {
    return postoptree2;

}

public Vector2 getPosbottree() {
    return posbottree2;
}

public void reposition2(float x){
    postoptree2.set(x, rand.nextInt(FLUCTUACTION) + TREE_GAP + LOWEST_OPENING);
    posbottree2.set(x, postoptree2.y - TREE_GAP - bottomtree2.getHeight());
    boundsTop2.setPosition(postoptree2.x,postoptree2.y);
    boundsBot2.setPosition(posbottree2.x,posbottree2.y);
}

public boolean collides2(Rectangle player){
    return player.overlaps(boundsTop2) || player.overlaps(boundsBot2);
}

public void dispose2(){
    toptree2.dispose();
    bottomtree2.dispose();
}

}
public class HPlayState extends HState {
private static final int TREE_SPACING = 125;
private static final int TREE_COUNT = 4;
private static final int GROUND_Y_OFFSET = -50;


private HBird bird;
private Texture bg;
private Texture ground;
private Vector2 groundPos1,groundPos2;
private Array<HTree> trees;
private Array<HTree2> trees2;



public HPlayState(HGameStateManager gsm) {
    super(gsm);
    bird = new HBird(50,300);
    cam.setToOrtho(false, HBonGame.WIDTH/2,HBonGame.HEIGHT/2);
    bg = new Texture("background3.jpg");
    ground = new Texture("ground.png");
    groundPos1 = new Vector2(cam.position.x-cam.viewportWidth/2,GROUND_Y_OFFSET);
    groundPos2 = new Vector2((cam.position.x-cam.viewportWidth/2) + ground.getWidth(),GROUND_Y_OFFSET);
    trees = new Array<HTree>();
    trees2 = new Array<HTree2>();

    for(int i=1; i <= TREE_COUNT; i++){
        trees.add(new HTree(i * (TREE_SPACING + HTree.TREE_WIDTH)));
    }
    for (int i=1; i >= TREE_COUNT; i++){
        trees2.add(new HTree2(i * (TREE_SPACING + HTree2.TREE_WIDTH)));
    }

}

@Override
protected void handleInput() {
    if (Gdx.input.justTouched())
        bird.jump();

}

@Override
public void update(float dt) {
    handleInput();
    updateGround();
    bird.update(dt);
    cam.position.x = bird.getPosition().x + 80;
    for (int i=0; i < trees.size; i++ ){
        HTree tree = trees.get(i);

        if (cam.position.x - (cam.viewportWidth / 2) > tree.getPostoptree().x + tree.getToptree().getWidth()){
            tree.reposition(tree.getPostoptree().x + ((HTree.TREE_WIDTH + TREE_SPACING) * TREE_COUNT));
        }

        if(tree.collides(bird.getBounds())){
            gsm.set(new HGameOverState(gsm));
        }
    }
    for (int i=0; i < trees2.size; i++ ){
        HTree2 tree2 = trees2.get(i);

        if (cam.position.x - (cam.viewportWidth / 2) > tree2.getPostoptree().x + tree2.getToptree().getWidth()){
            tree2.reposition2(tree2.getPostoptree().x + ((HTree2.TREE_WIDTH + TREE_SPACING) * TREE_COUNT));
        }

        if(tree2.collides2(bird.getBounds())){
            gsm.set(new HGameOverState(gsm));
        }
    }
    if (bird.getPosition().y <= ground.getHeight()+GROUND_Y_OFFSET){
        gsm.set(new HGameOverState(gsm));
    }
    cam.update();

}

@Override
public void render(SpriteBatch sb) {
    sb.setProjectionMatrix(cam.combined);
    sb.begin();
    sb.draw(bg,cam.position.x - (cam.viewportWidth/2), 0);
    sb.draw(bird.getTexture(),bird.getPosition().x,bird.getPosition().y);
    for (HTree tree : trees) {
        sb.draw(tree.getToptree(), tree.getPostoptree().x, tree.getPostoptree().y);
        sb.draw(tree.getBottomtree(), tree.getPosbottree().x, tree.getPosbottree().y);
    }
    for (HTree2 tree2 : trees2) {
        sb.draw(tree2.getToptree(), tree2.getPostoptree().x, tree2.getPostoptree().y);
        sb.draw(tree2.getBottomtree(), tree2.getPosbottree().x, tree2.getPosbottree().y);
    }
    sb.draw(ground, groundPos1.x,groundPos1.y);
    sb.draw(ground, groundPos2.x,groundPos2.y);
    sb.end();
}

@Override
public void dispose() {
    bg.dispose();
    bird.dispose();
    ground.dispose();
    for (HTree tree : trees){
        tree.dispose();

    }
    for (HTree2 tree2 : trees2){
        tree2.dispose2();

    }


}

private void updateGround() {
    if (cam.position.x - (cam.viewportWidth / 2) > groundPos1.x + ground.getWidth())
        groundPos1.add(ground.getWidth() * 2, 0);
    if (cam.position.x - (cam.viewportWidth / 2) > groundPos2.x + ground.getWidth())
        groundPos2.add(ground.getWidth() * 2, 0);



}
@Override
public void resize(int width, int height) {
    bird = new HBird(50,300);
}
}
HPlayState
类实现:

public class HTree {
public static final int TREE_WIDTH = 52;
private static final int FLUCTUACTION = 130;
private static final int TREE_GAP = 100;
private static final int LOWEST_OPENING = 120;
private Texture toptree,bottomtree;
private Vector2 postoptree,posbottree;
private Rectangle boundsTop,boundsBot;
private Random rand;
public HTree(float x){
    toptree = new Texture("uppertree.png");

    bottomtree = new Texture("bottomtree.png");

    rand = new Random();

    postoptree = new Vector2(x, rand.nextInt(FLUCTUACTION) + TREE_GAP + LOWEST_OPENING);

    posbottree = new Vector2(x, postoptree.y - TREE_GAP - bottomtree.getHeight());

    boundsTop = new Rectangle(getPostoptree().x,getPostoptree().y, toptree.getWidth(), toptree.getHeight());

    boundsBot = new Rectangle(getPosbottree().x,getPosbottree().y, bottomtree.getWidth(), bottomtree.getHeight());


}

public Texture getToptree() {
    return toptree;

}

public Texture getBottomtree() {
    return bottomtree;

}

public Vector2 getPostoptree() {
    return postoptree;

}

public Vector2 getPosbottree() {
    return posbottree;
}

public void reposition(float x){
    postoptree.set(x, rand.nextInt(FLUCTUACTION) + TREE_GAP + LOWEST_OPENING);
    posbottree.set(x, postoptree.y - TREE_GAP - bottomtree.getHeight());
    boundsTop.setPosition(postoptree.x,postoptree.y);
    boundsBot.setPosition(posbottree.x,posbottree.y);
}

public boolean collides(Rectangle player){
    return player.overlaps(boundsTop) || player.overlaps(boundsBot);
}

public void dispose(){
    toptree.dispose();
    bottomtree.dispose();
}

}
public class HTree2 {
public static final int TREE_WIDTH = 52;
private static final int FLUCTUACTION = 130;
private static final int TREE_GAP = 100;
private static final int LOWEST_OPENING = 120;
private Texture toptree2,bottomtree2;
private Vector2 postoptree2,posbottree2;
private Rectangle boundsTop2,boundsBot2;
private Random rand;
public HTree2(float x){
    toptree2 = new Texture("uppertree2.png");

    bottomtree2 = new Texture("bottomtree2.png");

    rand = new Random();

    postoptree2 = new Vector2(x, rand.nextInt(FLUCTUACTION) + TREE_GAP + LOWEST_OPENING);

    posbottree2 = new Vector2(x, postoptree2.y - TREE_GAP - bottomtree2.getHeight());

    boundsTop2 = new Rectangle(getPostoptree().x,getPostoptree().y, toptree2.getWidth(), toptree2.getHeight());

    boundsBot2 = new Rectangle(getPosbottree().x,getPosbottree().y, bottomtree2.getWidth(), bottomtree2.getHeight());


}

public Texture getToptree() {
    return toptree2;

}

public Texture getBottomtree() {
    return bottomtree2;

}

public Vector2 getPostoptree() {
    return postoptree2;

}

public Vector2 getPosbottree() {
    return posbottree2;
}

public void reposition2(float x){
    postoptree2.set(x, rand.nextInt(FLUCTUACTION) + TREE_GAP + LOWEST_OPENING);
    posbottree2.set(x, postoptree2.y - TREE_GAP - bottomtree2.getHeight());
    boundsTop2.setPosition(postoptree2.x,postoptree2.y);
    boundsBot2.setPosition(posbottree2.x,posbottree2.y);
}

public boolean collides2(Rectangle player){
    return player.overlaps(boundsTop2) || player.overlaps(boundsBot2);
}

public void dispose2(){
    toptree2.dispose();
    bottomtree2.dispose();
}

}
public class HPlayState extends HState {
private static final int TREE_SPACING = 125;
private static final int TREE_COUNT = 4;
private static final int GROUND_Y_OFFSET = -50;


private HBird bird;
private Texture bg;
private Texture ground;
private Vector2 groundPos1,groundPos2;
private Array<HTree> trees;
private Array<HTree2> trees2;



public HPlayState(HGameStateManager gsm) {
    super(gsm);
    bird = new HBird(50,300);
    cam.setToOrtho(false, HBonGame.WIDTH/2,HBonGame.HEIGHT/2);
    bg = new Texture("background3.jpg");
    ground = new Texture("ground.png");
    groundPos1 = new Vector2(cam.position.x-cam.viewportWidth/2,GROUND_Y_OFFSET);
    groundPos2 = new Vector2((cam.position.x-cam.viewportWidth/2) + ground.getWidth(),GROUND_Y_OFFSET);
    trees = new Array<HTree>();
    trees2 = new Array<HTree2>();

    for(int i=1; i <= TREE_COUNT; i++){
        trees.add(new HTree(i * (TREE_SPACING + HTree.TREE_WIDTH)));
    }
    for (int i=1; i >= TREE_COUNT; i++){
        trees2.add(new HTree2(i * (TREE_SPACING + HTree2.TREE_WIDTH)));
    }

}

@Override
protected void handleInput() {
    if (Gdx.input.justTouched())
        bird.jump();

}

@Override
public void update(float dt) {
    handleInput();
    updateGround();
    bird.update(dt);
    cam.position.x = bird.getPosition().x + 80;
    for (int i=0; i < trees.size; i++ ){
        HTree tree = trees.get(i);

        if (cam.position.x - (cam.viewportWidth / 2) > tree.getPostoptree().x + tree.getToptree().getWidth()){
            tree.reposition(tree.getPostoptree().x + ((HTree.TREE_WIDTH + TREE_SPACING) * TREE_COUNT));
        }

        if(tree.collides(bird.getBounds())){
            gsm.set(new HGameOverState(gsm));
        }
    }
    for (int i=0; i < trees2.size; i++ ){
        HTree2 tree2 = trees2.get(i);

        if (cam.position.x - (cam.viewportWidth / 2) > tree2.getPostoptree().x + tree2.getToptree().getWidth()){
            tree2.reposition2(tree2.getPostoptree().x + ((HTree2.TREE_WIDTH + TREE_SPACING) * TREE_COUNT));
        }

        if(tree2.collides2(bird.getBounds())){
            gsm.set(new HGameOverState(gsm));
        }
    }
    if (bird.getPosition().y <= ground.getHeight()+GROUND_Y_OFFSET){
        gsm.set(new HGameOverState(gsm));
    }
    cam.update();

}

@Override
public void render(SpriteBatch sb) {
    sb.setProjectionMatrix(cam.combined);
    sb.begin();
    sb.draw(bg,cam.position.x - (cam.viewportWidth/2), 0);
    sb.draw(bird.getTexture(),bird.getPosition().x,bird.getPosition().y);
    for (HTree tree : trees) {
        sb.draw(tree.getToptree(), tree.getPostoptree().x, tree.getPostoptree().y);
        sb.draw(tree.getBottomtree(), tree.getPosbottree().x, tree.getPosbottree().y);
    }
    for (HTree2 tree2 : trees2) {
        sb.draw(tree2.getToptree(), tree2.getPostoptree().x, tree2.getPostoptree().y);
        sb.draw(tree2.getBottomtree(), tree2.getPosbottree().x, tree2.getPosbottree().y);
    }
    sb.draw(ground, groundPos1.x,groundPos1.y);
    sb.draw(ground, groundPos2.x,groundPos2.y);
    sb.end();
}

@Override
public void dispose() {
    bg.dispose();
    bird.dispose();
    ground.dispose();
    for (HTree tree : trees){
        tree.dispose();

    }
    for (HTree2 tree2 : trees2){
        tree2.dispose2();

    }


}

private void updateGround() {
    if (cam.position.x - (cam.viewportWidth / 2) > groundPos1.x + ground.getWidth())
        groundPos1.add(ground.getWidth() * 2, 0);
    if (cam.position.x - (cam.viewportWidth / 2) > groundPos2.x + ground.getWidth())
        groundPos2.add(ground.getWidth() * 2, 0);



}
@Override
public void resize(int width, int height) {
    bird = new HBird(50,300);
}
}
公共类HPlayState扩展了HState{
私有静态最终整数树_间距=125;
私有静态最终整数树计数=4;
专用静态最终int接地偏移量=-50;
私人海鸟;
私人纹理背景;
私人纹理地面;
专用矢量2 groundPos1,groundPos2;
私有数组树;
私有数组树2;
公共HPlayState(HGameStateManager gsm){
超级(gsm);
鸟=新的HBird(50300);
凸轮设置为(假,HBonGame.宽度/2,HBonGame.高度/2);
bg=新纹理(“background3.jpg”);
地面=新纹理(“ground.png”);
groundPos1=新矢量2(凸轮位置x凸轮视口宽度/2,地面Y偏移);
groundPos2=新矢量2((cam.position.x-cam.viewportWidth/2)+ground.getWidth(),ground\u Y\u OFFSET);
trees=新数组();
trees2=新数组();
对于(int i=1;i=TREE\u COUNT;i++){
树2.添加(新HTree2(i*(树间距+HTree2.树宽度));
}
}
@凌驾
受保护的无效handleInput(){
if(Gdx.input.justTouched())
鸟。跳();
}
@凌驾
公共无效更新(浮动dt){
handleInput();
updateGround();
更新(dt);
cam.position.x=bird.getPosition().x+80;
对于(int i=0;itree.getPostoptree().x+tree.getToptree().getWidth()){
tree.reposition(tree.getPostoptree().x+((HTree.tree_宽度+树_间距)*树_计数));
}
if(tree.collides(bird.getBounds())){
gsm.set(新HGameOverState(gsm));
}
}
for(int i=0;itree2.getPostoptree().x+tree2.getToptree().getWidth()){
tree2.reposition2(tree2.getPostoptree().x+((HTree2.TREE_宽度+TREE_间距)*TREE_计数));
}
if(tree2.collides2(bird.getBounds())){
gsm.set(新HGameOverState(gsm));
}
}
if(bird.getPosition().y groundPos1.x+ground.getWidth())
groundPos1.add(ground.getWidth()*2,0);
如果(cam.position.x-(cam.viewportWidth/2)>groundPos2.x+ground.getWidth())
groundPos2.add(ground.getWidth()*2,0);
}
@凌驾
公共空心调整大小(整型宽度、整型高度){
鸟=新的HBird(50300);
}
}

我建议使用类型为
HTree
的接口或超类,所有其他类型都可以从中扩展。例如

public abstract class HTree {

    /*
    All your class variables as declared
    in your question
    */

    public HTree(float x, String topTreeFile, String bottomTreeFile) {

        /* Do all the setup stuff here */

        // Set the textures
        toptree = new Texture(topTreeFile);
        bottomtree = new Texture(bottomTreeFile);
    }

    /*
    All your normal getters and setters
    and other functions here
    */
}
然后您将创建4个不同的类,它们都从
HTree
扩展,如下所示:

/* Different types of tree each extend the main HTree class */

public class HTree1 extends HTree {

    public HTree1(float x) {
        super(x, "uppertree.png", "lowertree.png");

        /* Any other custom code for this tree type 
           can go here */
    }
}

public class HTree2 extends HTree {

    public HTree1(float x) {
        super(x, "uppertree2.png", "lowertree2.png");

        /* Any other custom code for this tree type 
           can go here */
    }
}

/* And so on for HTree3 and HTree4 ... */
最后,您可以将这四种不同类型中的任何一种用作
HTree
,因为它们都是从该类扩展而来的,因此可以将它们分组到单个
数组中,并可以在单个循环中一起呈现/更新

public class HPlayState extends HState {

    /* Other class variables here ... */

    /* Only one array for all different tree types */
    private Array<HTree> trees;

    public HPlayState(GameStateManager gsm) {
        /* Same setup code ... */

        trees = new Array<HTree>();
        Random random = new Random();

        for (int i = 1; i <= TREECOUNT; i++) {

            /* Randomly pick which tree type to choose */
            int treeType = rand.next(4); // 4 or however many different types of tree you have
            float x = i * (TREE_SPACING + HTree.TREE_WIDTH);

            if (treeType == 0)          trees.add(new HTree1(x));
            else if (treeType == 1)     trees.add(new HTree2(x));
            else if (treeType == 2)     trees.add(new HTree3(x));
            else                        trees.add(new HTree4(x));

        }
    }

    /*
    Rendering and updating code here
    */
}
公共类HPlayState扩展了HState{
/*其他类变量在这里*/
/*对于所有不同的树类型,只有一个数组*/
私有数组树;
公共HPlayState(GameStateManager gsm){
/*相同的设置代码*/
trees=新数组();
随机=新随机();

对于(int i=1;我尝试了这个,它一直工作到我触摸屏幕,然后它开始关闭自己。我不知道出了什么问题,虽然我做了一些更改,但大部分只是重命名。我将继续尝试并修复这个问题。不,仍然没有。当它达到实际的游戏状态时仍然关闭应用程序。没有。游戏工作正常直到我真正进入播放状态。然后它才关闭。控制台中一定有错误消息。例如NullPointerException