繁体   English   中英

如何在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()方法调用播放器自己的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设置为static)。

同样在敌人生成时(这里相关的问题是,根据我看到的内容,它们在屏幕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;
    }

我的问题的答案由我自己发布在另一个我的问题中,该问题也是由FitViewports的实现混乱以及在将对象绘制到游戏中时使用WorldWidth和WorldHeight属性作为参考坐标以及正确设置摄影机位置引起的还要考虑这些价值观。 答案就在这里,即使它是文本而不是代码,主要是我在这篇文章中已经写的内容。 FitViewport无法正确缩放Libgdx

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM