简体   繁体   English

Libgdx Viewport,舞台不起作用

[英]Libgdx Viewport with stage not working

2 months ago I stumbled upon the problem that my game that I was working on did not scale with other devices. 2个月前,我偶然发现了我正在处理的游戏无法与其他设备一起扩展的问题。 So I decided to implement viewports since people said that it was the best way and also an easy way. 所以我决定实施视口,因为人们说这是最好的方式,也是一种简单的方法。 But it's been around 2 months since I started and still don't have a working example. 但是自从我开始以来已经有两个月了,但仍然没有一个有效的例子。 I have read every single page I could find, read the wiki multiple times and ask several people but never got an answer that helped me getting it to work. 我已经阅读了我能找到的每一页,多次阅读wiki并询问了几个人,但从来没有得到过帮助我开始工作的答案。

So the problem is that I need to do 3 things, First I have a stage, this is were I would place all my actors on (buttons and stuff), then I need my cam in this case a OrthographicCamera, and then I need a viewport to scale everything. 所以问题是我需要做3件事,首先我有一个舞台,这是我将所有演员放在(按钮和东西)上,然后我需要我的凸轮在这种情况下是一个OrthographicCamera,然后我需要一个视口扩展一切。

This is my lastest test: http://pastebin.com/ZvZmeHLs 这是我最新的测试: http//pastebin.com/ZvZmeHLs

My create 我的创造

@Override
    public void create () {
            cam = new OrthographicCamera(800, 600);
            viewport = new FillViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), cam);
            viewport.update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);
            stage = new Stage(viewport);

            atlas = new TextureAtlas("LoadingScreen.pack");
            Logo = new Image(atlas.findRegion("VeslaLogo"));
            stage.addActor(Logo);
    }

and my resize 我的调整大小

@Override
    public void resize(int Width, int Height) {
            viewport.update(Width, Height, true);

            Logo.setSize(700, 500);
            Logo.setX(Width / 2 - Logo.getWidth() / 2);
            Logo.setY(Height / 2 - Logo.getHeight() / 2);
            Logo.setOrigin(Logo.getWidth() / 2, Logo.getHeight() / 2);
    }

I first thought that it was working, on a nexus one it looked good, had everything on screen but when I tested it on a s4 mini it was way to big. 我首先想到它正在工作,在一个看起来不错的关系上,屏幕上有所有内容,但是当我在s4 mini上测试它时,这是很大的方法。 Now I don't know if this is because i'm just not understanding viewports or that I'm thinking way too complicated. 现在我不知道这是不是因为我只是不了解视口或我认为太复杂了。 The only thing that I know is that I'm totally lost and have no idea what else I could try. 我唯一知道的是,我完全迷失了,不知道我还能尝试什么。 So it would be great if someone could push me in the right direction, or could explain to me why this isnt working. 如果有人能够把我推向正确的方向,或者可以向我解释为什么这不起作用,那将会很棒。

ps: I also tested it with fixed sizes but this didn't make any difference for some reason ps:我也用固定尺寸进行了测试,但由于某些原因这没有任何区别

Sample of the fixed size one: http://pastebin.com/mpjkP9ru 固定大小的样本: http//pastebin.com/mpjkP9ru

private int VIRTUAL_WIDTH = 600; 
private int VIRTUAL_HEIGHT = 900; 

@Override
    public void create () {
            stage = new Stage(new StretchViewport(VIRTUAL_WIDTH, VIRTUAL_HEIGHT));
            cam = new OrthographicCamera(VIRTUAL_WIDTH, VIRTUAL_HEIGHT); 
    }

and my resize 我的调整大小

@Override
    public void resize(int Width, int Height) {
            Width = VIRTUAL_WIDTH;  
            Height = VIRTUAL_HEIGHT; 

            stage.getViewport().update(Width, Height, true); 
    }

If more example or anything is needed please let me know, I'm in a state were I would try anything to get it working 如果需要更多示例或任何内容请告诉我,我处于一种状态,我会尝试任何事情来使其正常工作

Edit: I managed to get it working with the help of @donfuxx . 编辑:我设法在@donfuxx的帮助下工作。 for those interested in the code: http://pastebin.com/8v2PB2t9 对于那些对代码感兴趣的人: http//pastebin.com/8v2PB2t9

In your posted code you seem to do the resize method wrong, because you are updating the viewport with a constant (!) value (so you always set the width/height to 600/900, no surprize you don't see adjustments to different screen sizes): 在您发布的代码中,您似乎错误地调整了调整大小的方法,因为您正在使用常量 (!)值更新视口(因此您始终将宽度/高度设置为600/900,不要惊讶于您看不到对不同的调整屏幕尺寸):

@Override
public void resize(int Width, int Height) {
        Width = VIRTUAL_WIDTH;  
        Height = VIRTUAL_HEIGHT; 

        stage.getViewport().update(Width, Height, true); 
}

You should instead do it like it is suggested in the the viewports wiki and set the proper width / height values: 您应该像在视口wiki中建议的那样执行此操作并设置正确的宽度/高度值:

@Override
public void resize(int width, int height) {
    // use true here to center the camera
    // that's what you probably want in case of a UI
    stage.getViewport().update(width, height, false);
}

BTW: also get rid of that Logo sizing code inside your resize method! 顺便说一下:还要在你的resize方法中删除那个Logo大小调整代码!

Update: Also I noticed that you should better initialize your viewport and camera like this (use the viewport constructor that has the camera parameter): 更新:另外我注意到你应该更好地初始化你的视口和相机(使用具有camera参数的视口构造函数):

 cam = new OrthographicCamera(VIRTUAL_WIDTH, VIRTUAL_HEIGHT);
 viewport = new StretchViewport(VIRTUAL_WIDTH, VIRTUAL_HEIGHT, cam); //notice cam param here!
 stage = new Stage(viewport);

Instead of: 代替:

 stage = new Stage(new StretchViewport(VIRTUAL_WIDTH, VIRTUAL_HEIGHT));
 cam = new OrthographicCamera(VIRTUAL_WIDTH, VIRTUAL_HEIGHT);

I personally have designed my app for 1920x1080 and just scale everything down on smaller screens. 我个人设计了我的应用程序1920x1080,只是在较小的屏幕上缩小一切。 My camera for the spritebatch: 我用于spritebatch的相机:

camera = new OrthographicCamera(screen_width, screen_height);
batch.setProjectionMatrix(camera.combined);

ensures that on every screen the sprites are drawn between -screen_width/2.0 and screen_width/2.0 (same for height) 确保在每个屏幕上精灵在-screen_width / 2.0和screen_width / 2.0之间绘制(高度相同)

using the default stage constructor draws every actor between 0 and screen_width 使用默认的阶段构造函数绘制0和screen_width之间的每个actor

stage = new Stage();

then I set the scale to: 然后我将比例设置为:

float global_scale = 1920.0f/screen_height;
actor.setScale(global_scale);

There are obviously other ways to do this, like with a viewport. 显然有其他方法可以做到这一点,比如使用视口。

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

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