如何在 FitViewport 视图中绘制精灵。 libgdx
How to draw a sprite within the FitViewport view. libgdx
- 这里的基本问题是:如何始终将精灵保持在 Fitviewport 内?如何保持对视图的引用,以便获得正确的绘制位置坐标?
我正在尝试将敌人生成到游戏画面中。但这是由 FitViewport 处理的,敌人甚至玩家都可以在某些屏幕分辨率下移动到 FitViewport 之外。到目前为止,问题似乎出在 Y 轴上。
FitViewport 是这样制作的:
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);
然后相机位置在 resize() 方法中像这样更新:
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() 方法调用 Player 自己的 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 之外生成)我在实现所有这些视口的 Screen 的 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 没有给出正确的结果(它确实给了我同样的问题)。
在批处理和应用视口方面正确实现了 render() 方法:
MyGame.batch.setProjectionMatrix(gameCamera.combined);
gameViewport.apply();
MyGame.batch.begin();
for (int i = entities.size()-1; i>=0;i--){
entities.get(i).render();
}
你永远不应该在调整大小时改变你的玩家或敌人的位置,这就是视口的用途,首先删除所有这样做的代码,让你的视口按照你的预期工作你需要创建一个新实例当你调整大小时相机通过新的视口宽度和高度,我更喜欢让我的相机静态,这样我就可以从我想要的任何地方访问它的属性,你应该这样做:
public static OrthographicCamera update(int width,int height){
instance = new OrthographicCamera(width, height);
instance.setToOrtho(false);
return instance;
}
我的问题的答案是 post 我自己在我的另一个问题中提出的,这也是由于 FitViewports 的实现以及在将对象绘制到其中时使用 WorldWidth 和 WorldHeight 属性作为参考坐标引起的混乱游戏,并在考虑这些值的情况下正确设置相机位置。
答案就在这里,尽管它是文本而不是代码,主要是我已经在 post 中写的内容。
- 这里的基本问题是:如何始终将精灵保持在 Fitviewport 内?如何保持对视图的引用,以便获得正确的绘制位置坐标?
我正在尝试将敌人生成到游戏画面中。但这是由 FitViewport 处理的,敌人甚至玩家都可以在某些屏幕分辨率下移动到 FitViewport 之外。到目前为止,问题似乎出在 Y 轴上。
FitViewport 是这样制作的:
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);
然后相机位置在 resize() 方法中像这样更新:
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() 方法调用 Player 自己的 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 之外生成)我在实现所有这些视口的 Screen 的 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 没有给出正确的结果(它确实给了我同样的问题)。
在批处理和应用视口方面正确实现了 render() 方法:
MyGame.batch.setProjectionMatrix(gameCamera.combined);
gameViewport.apply();
MyGame.batch.begin();
for (int i = entities.size()-1; i>=0;i--){
entities.get(i).render();
}
你永远不应该在调整大小时改变你的玩家或敌人的位置,这就是视口的用途,首先删除所有这样做的代码,让你的视口按照你的预期工作你需要创建一个新实例当你调整大小时相机通过新的视口宽度和高度,我更喜欢让我的相机静态,这样我就可以从我想要的任何地方访问它的属性,你应该这样做:
public static OrthographicCamera update(int width,int height){
instance = new OrthographicCamera(width, height);
instance.setToOrtho(false);
return instance;
}
我的问题的答案是 post 我自己在我的另一个问题中提出的,这也是由于 FitViewports 的实现以及在将对象绘制到其中时使用 WorldWidth 和 WorldHeight 属性作为参考坐标引起的混乱游戏,并在考虑这些值的情况下正确设置相机位置。
答案就在这里,尽管它是文本而不是代码,主要是我已经在 post 中写的内容。