Slick2D不会渲染所有MouseOverAreas

时间:2015-11-23 09:54:24

标签: java slick2d


我正在使用Slick2D用Java编写一个小游戏
对于我的MainMenu,我有4个按钮,我使用MouseOverAreas来表示这些按钮 问题是我创建的最后一个MouseOverArea没有呈现
我像这样渲染它们

@Override
public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
    this.renderButtons(container, g);
}

private void renderButtons(GameContainer container, Graphics g) {
    this.buttonStartGame.render(container, g);
    this.buttonSettings.render(container, g);
    this.buttonAchievements.render(container, g);
    this.buttonClose.render(container, g);
}

buttonClose未呈现...我创建了这样的按钮

@Override
public void init(GameContainer container, StateBasedGame game) throws SlickException {
    this.initButtons(container);
}

private void initButtons(GameContainer container) throws SlickException {
    this.buttonStartGame = createButton(container, "Start Game", 0);
    this.buttonSettings = createButton(container, "Settings", 1);
    this.buttonAchievements = createButton(container, "Achievements", 2);
    this.buttonClose = createButton(container, "Exit", 3);

//      MouseOverArea foobar = createButton(container, "Foobar", 5);
}

private MouseOverArea createButton(GameContainer container, String text, int buttonIndex) throws SlickException {
    /* x, y, width and height are calculated so that I can use them.
       They all are local variables with the right values but the code for them is not important */

    Image image = new Image(width, height);
    Graphics g = image.getGraphics();

    int textX = (width - g.getFont().getWidth(text)) / 2;
    int textY = (height - g.getFont().getHeight(text)) / 2;

    g.drawString(text, textX, textY);

    MouseOverArea button = new MouseOverArea(container, image, x, y, width, height, this::onComponentActivated);
    button.setNormalColor(Color.white);
    button.setMouseOverColor(Color.gray);
    button.setMouseDownColor(Color.darkGray);

    return button;
}

但是,如果我也创建了此foobar,则会显示关闭按钮 我错过了什么或为什么我创建的最后一个MouseOverArea永远不可渲染?

1 个答案:

答案 0 :(得分:0)

我不确定原因,但是调用g.destroy()完成了这项工作