2017-02-09 75 views
1

我正在开发一个libgdx游戏。我想要做的是创建一个屏幕,其中一些正方形从顶部到底部“下降”。所以:
1.我必须设置一个背景图像。
2.我必须随机产生一些正方形。
3.我必须将屏幕从底部移动到顶部。Libgdx自动滚动相机

为了得到我使用雪碧的背景,很多精灵的广场。然后用相机移动屏幕。

在render方法,我有:

@Override 
    public void render(float delta) { 
     Gdx.gl.glClearColor(0, 0, 0, 0); 
     Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 
     Gdx.input.setInputProcessor(stage); 

     batch.begin(); 

     backgroundSprite.draw(batch); 

     for(Square square : squareList) { 
      //Gdx.app.log("[Match]", square.toString()); 
      square.updatePosition(); 
      square.setSize(20, 20); 
      square.draw(batch); 
     } 
     batch.end(); 
     batch.setProjectionMatrix(fixedCamera.view); 

     fixedCamera.position.y +=1; 
     System.out.println("Camera position: x:" +fixedCamera.position.x+" y:"+fixedCamera.position.y); 
     fixedCamera.update(); 
     stage.act(Gdx.graphics.getDeltaTime()); 
     stage.draw(); 
    } 

这是在构造函数中的代码。这就是我在类

final static int WIDTH = Gdx.app.getGraphics().getWidth(); 
final static int HEIGHT = Gdx.app.getGraphics().getHeight(); 
skin = new Skin(Gdx.files.internal("skin/flat-earth-ui.json")); 
     fixedCamera = new OrthographicCamera(WIDTH, HEIGHT); 
     stage = new Stage(new ScreenViewport()); 
     batch = new SpriteBatch(); 
     dynBatch = new SpriteBatch(); 
     backgroundSprite = new Sprite(new Texture(Gdx.files.internal(scenario.getTexturePath()))); 
     backgroundSprite.setPosition(0,0); 
     backgroundSprite.setSize(WIDTH, HEIGHT); 
     fixedCamera.position.set(0,0, 0); 
     fixedCamera.update(); 

的项目显示正确,但相机抖动时,所有的精灵消失...... 我怎样才能解决呢?有没有办法更有效地处理?

+0

Gdx.input.setInputProcessor(stage);在create()或show()方法中使用 – Aryan

+0

您在游戏中使用舞台吗? fixedCamera与舞台相机不同? – Aryan

+0

您的fixedCamera的视口是什么? – Aryan

回答

0

您的需求可以通过以下步骤来实现:

  1. 形成具有屏幕尺寸,并保持它的底部雪碧。
  2. 在随机水平位置的屏幕顶部生成正方形,并更改所有正方形(精灵)的y位置
  3. 无需移动相机。保持在屏幕中间。