Scrollpane在libgdx中出现奇怪的缩放

时间:2016-01-14 13:09:29

标签: java libgdx viewport scrollpane

由字体绘制并在Scrollpane中的List中绘制的文本似乎有不同的大小。这就是我绘制文字的方式:

let

我如何在ScrollPane中的List中绘制文本:

font.draw(batch, "score", 500, 700);

这是奇怪的(?)结果:

Result

似乎滚动窗格以某种方式缩放。我不希望滚动窗格内的文字被缩放。

这是整个代码:

 tfBackground = new Texture(Gdx.files.internal("tfbackground.png"));
    knob_scroll = new Texture(Gdx.files.internal("knob_scroll.png"));
    scroll_horizontal = new Texture(Gdx.files.internal("scroll_horizontal.png"));

    sps.background = new TextureRegionDrawable(new TextureRegion(tfBackground));
    sps.vScroll = new TextureRegionDrawable(new TextureRegion(scroll_horizontal));
    sps.vScrollKnob = new TextureRegionDrawable(new TextureRegion(knob_scroll));

    listS = new List.ListStyle();
    listS.font = font;
    listS.fontColorSelected = Color.BLACK;
    listS.fontColorUnselected = Color.GRAY;
    listS.selection = new TextureRegionDrawable(new TextureRegion(tfBackground));

    scoreList = new List<String>(listS);

    items = new Array<String>();

    res += "score 1, score 2, score 3, score 4, score 5, score 6, score 7, score 8, score 9, score 10";

    String name_score[] = res.split(",");
    for(String s: name_score)
    {
        items.add(s);
    }

    scoreList.setItems(items);
    scoreList.pack();

    scrollPane = new ScrollPane(scoreList, sps);

    addActor(scrollPane);

PS:抽象屏幕扩展了Stage并实现了Screen。

1 个答案:

答案 0 :(得分:0)

您的构造函数不会调用超类构造函数,该构造函数还需要调用其中一个Stage构造函数以确保您的Stage已正确设置。

这里发生的是你设置了一个摄像头和视口(和SpriteBatch,虽然不是问题的一部分),它只用于你的font.draw,与舞台使用的不同。

你的班级需要看起来像这样:

public class ResultScreen extends AbstractScreen{

    private OrthographicCamera camera;
    private Viewport viewport;
    private BitmapFont font;
    private Batch batch; //<------------changed
    private String res;
    private float time = 10;
    private Texture tfBackground, knob_scroll, scroll_horizontal;
    private List<String> scoreList;
    private ScrollPane scrollPane;
    private List.ListStyle listS;
    private ScrollPane.ScrollPaneStyle sps;
    private Array<String> items;

    public ResultScreen(float time, String res) {
        super(new StretchViewport(800, 1024)); //Call an AbstractScreen constructor that calls a Stage constructor
        this.time = time;
        font = new BitmapFont(Gdx.files.internal("aw.fnt"));
        font.setColor(Color.BLACK);
        camera = getCamera(); //<------------changed
        viewport = getViewport(); //<------------changed
        batch = getBatch(); //<------------changed
        camera.position.x = 400;
        camera.position.y = 512;
        // unnecessary can remove because this is called in resize: viewport.update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        this.res = res;
        fillList();
    }
}

例如,AbstractScreen可以有这样的构造函数:

public AbstractScreen(Viewport viewport){
    super(viewport);
}