- 基本的问题在这里是:如何始终保持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();
}
我没有那个重新定位调整大小()方法中我的实体的任何代码。但生病尝试你的相机暗示。 – rufoDev
好吧,我累了,但在我的设备女巫是一个非常高分辨率的设备,相机得到中心,而不是定位相对于播放器。一切都缩小了,就像没有FitViewport一样。我正在使用FitViewport来随意扩大游戏的精灵并保持宽高比 – rufoDev
我是否应该重新设置视口以便我可以将它传递给新的相机实例? – rufoDev