2016-03-08 94 views
1
  • 基本的问题在这里是:如何始终保持Fitviewport内你的精灵?如何保持对视图的引用以便在哪里绘制适当的坐标?

我试图产卵敌人进入游戏画面。但是这是由FitViewport处理的,敌人甚至玩家可以在某些屏幕分辨率下移出FitViewport。到目前为止,问题似乎在Y轴上。如何在FitViewport视图中绘制精灵。 libgdx

的FitViewport由这样的:

gameViewport.update(width,height); //not used when using the virtual viewport in the render method. 
gameCamera.position.set(player.position.x + 200,player.position.y, 0); 

然后update()方法调用玩家自己:

gameCamera = new OrthographicCamera(); 
gameCamera.setToOrtho(false); 
gameViewport = new FitViewport(MyGame.WORLD_WIDTH,MyGame.WORLD_HEIGHT,gameCamera); 
gameViewport.setScreenBounds(0,0,MyGame.WORLD_WIDTH,MyGame.WORLD_HEIGHT); 

然后摄像机位置得到这样在调整大小()方法更新包含以下几行的update()方法:

//POSITION UPDATE 
if (this.position.x<0) this.position.x=0; 
if (this.position.x>Gdx.graphics.getWidth() - width) this.position.x= Gdx.graphics.getWidth() - width; 
if (this.position.y<0) this.position.y = 0; 
if (this.position.y>PlayScreen.gameViewport.getScreenHeight() - height) this.position.y = PlayScreen.gameViewport.getScreenHeight()- height; 

关于X轴的注意事项我是仍然使用Gdx.graphics维度,因为我还没有使用PlayScreen.gameViewport.getScreenHeight()(为此目的已将gameViewport设置为静态)。

而且对敌方重生(此相关的问题是,它们产卵屏幕y外侧在我所看到的而言)我有屏幕的update()方法实现所有这些视这里面代码:

//Alien Spawn 
    if (System.currentTimeMillis() - lastZSpawn >= SpawnTimer){ 
     count++; 
     lastZSpawn= System.currentTimeMillis(); 
     for (int i=0;i<count;i++){ 
      int x = Gdx.graphics.getWidth(); 
      int y = random.nextInt((int)gameViewport.getScreenHeight() - Alien.height); 
      if (entities.size()<6){ 
       entities.add(new Alien(new Vector2(x,y),1, alienImages,(float)((0)))); 
      } 
     } 

    } 

而且使用gameViewport.getScreenHeight()这里的原因Gdx.graphics wasnt给出正确的结果(它给了我同样的问题,真的)。

渲染()方法在批处理方面的正确实施和应用视:

MyGame.batch.setProjectionMatrix(gameCamera.combined); 
gameViewport.apply(); 
MyGame.batch.begin(); 

for (int i = entities.size()-1; i>=0;i--){ 
    entities.get(i).render(); 
} 

回答

0

回答我的问题是由自己发表在这也得到了一个混乱FitViewports实施驱动,并作为参考坐标绘制对象到游戏时使用WorldWidth和WorldHeight性能我的另一个问题,也是正确设置照相机的位置也考虑到这些值。 答案在这里即使它的文本,而不是代码,它主要是我已经写在这个非常后。 FitViewport doesnt scale properly Libgdx

0

你应该永远不会改变您的播放器或调整时,敌人的位置,这就是为什么视口是,删除所有先执行的代码,使视口按预期工作,在调整大小时需要创建一个新的摄像机实例,以传递新的视口宽度和高度,我更愿意让摄像机变为静态,这样我就可以使用它的atribbutes来自世界各地的,我想,你应该做这样的事情:

public static OrthographicCamera update(int width,int height){ 
     instance = new OrthographicCamera(width, height); 
     instance.setToOrtho(false); 
     return instance; 
    } 
+0

我没有那个重新定位调整大小()方法中我的实体的任何代码。但生病尝试你的相机暗示。 – rufoDev

+0

好吧,我累了,但在我的设备女巫是一个非常高分辨率的设备,相机得到中心,而不是定位相对于播放器。一切都缩小了,就像没有FitViewport一样。我正在使用FitViewport来随意扩大游戏的精灵并保持宽高比 – rufoDev

+0

我是否应该重新设置视口以便我可以将它传递给新的相机实例? – rufoDev