2016-09-04 9 views
-1

ランダムと異なるパイプが(ゆるい鳥スタイルゲーム)見せること、私はAndroidのゲームシーンを作るに新たなんだと私の最初のゲームのために、私は成功したゆるい鳥クローンにLibGdx、コーヒー、そしてYouTubeに感謝をしました。それは、総クローンではありませんようにするには、しかし、私はパイプの種類を追加しようとしていると私は助けを必要と場所です。LibGDX:

4種類のパイプ(またはゲームにあるツリー)がランダムに表示されて予測不能になるようにしようとしていますが、これまでに1種類しか作成できませんでした現れる。ここでは2種類のタイプと自分のプレイステートに使用したコードを示します。

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

} 

HTree2クラスの実装:

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

} 

HPlayStateクラスの実装:

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

答えて

0

私はインターフェイスまたはタイプHTree WHのスーパークラスを使用することをお勧めします他のすべてのタイプはそこから拡張することができます。例えば。

/* 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ように、これら4つのタイプのいずれかを使用することができ、次のように

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 
    */ 
} 

その後、HTreeから延びるすべての4つの異なるクラスを作成しますので、単一Array<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 
    */ 
} 

注:私は、コードのいずれかをテストしていないので、そこに必要ないくつかの誤字や変更かもしれないが、それは正常に動作する必要があります。

+0

これを試してみましたが、私はそれがその後、自分自身を閉じることを進ん画面を触れるまでそれを後処理。何がうまくいかなかったのか分かりませんが、私はいくつか変更しましたがほとんどが名前を変更します。しかし、これを私の目的のために修正していきます。 – Riva

+0

いいえ、まだ何もありません。実際のプレイ状態になると、アプリを閉じます。 – Riva

+0

さて、どのようなエラーメッセージが表示されますか? –