2014-02-24 59 views
1

我正在開發一個簡單的遊戲,使用Android的libgdx,我想將背景場景移動爲If Bird正在向前移動。我嘗試過在互聯網上提供的各種示例和代碼來移動或翻譯相機,但無濟於事。請幫助我,並建議一些代碼來完成此操作。在libgdx遊戲中移動背景屏幕(android)

這裏是我的代碼片段:

x+=0.1; 
      trunk_rec.x-=10; 
      trunk1_rec.x-=10; 
      bird_rec.y-=5; 
      if(trunk_rec.x<0) 
      { 
       trunk_rec.x=trunk1_rec.x=900; 
      } 

      camera.translate(1,0); 
      camera.update(); 
      game.batch.setProjectionMatrix(camera.combined); 

      game.batch.begin(); 
      game.batch.draw(bg,0, 0,800,500); 

      game.batch.draw(trunk,trunk_rec.x,trunk_rec.y); 
      game.batch.draw(trunk,trunk1_rec.x,trunk1_rec.y); 
      game.batch.draw(bird,bird_rec.x,bird_rec.y); 

      game.batch.end(); 

      if(trunk_rec.x==bird_rec.x) 
      { 
       score++; 
      } 

      if(Gdx.input.isTouched()) 
      { 
       bird_rec.y+=30; 
      } 

      if((trunk_rec.overlaps(bird_rec))|| trunk1_rec.overlaps(bird_rec) || bird_rec.y<10) 
      { 
       Gdx.app.exit(); 
       System.out.println("Score is "+score); 
      } 

我嘗試用camera.position移動相機爲好。

Thanx!

+0

更好的方法是移動鳥而不是背景。向我們展示背景/鳥的渲染代碼。 – noone

+0

@noone:但它是要求! –

+0

查看更新的問題 –

回答

0

您可以使用角色和動作

使用多個地面角色填充屏幕寬度。

if(isout){ 
isout=false; 
ground1.addAction(Actions.sequence(Actions.moveTo(-ground.getWidth(),ground.getY(),duration),isDone)); 
} 
Action isDone = new Action() { 

    @Override 
    public boolean act(float delta) { 
          isout=true; 
          ground1.setPosition(screenWidth,ground.getY()); 
     return true; 
    } 
};