1

我正在 Slick2D 中创建游戏。首先,让我向您展示我被卡住的 GUI 状态。

http://imageshack.us/photo/my-images/593/4v3v.jpg/

所以,我在类 SettingsUI 中遇到问题,使用增加/减少值方法,每当我调用增加/减少方法时,从数组中选择值,当我按下增加箭头按钮时会发生什么,而不是给我 (i+1) value,它是一个值 {1000, 700} 宽度/高度,循环迭代到最后一个值 {1400, 900}。反过来也会发生同样的情况,当我减少它时,它会转到零元素,默认情况下是 {800, 600}。而我发现(通过打印值),当我单击那个箭头按钮时,只需单击一次,它就会打印出该值的 30 倍。我试图在循环内增加/减少时添加语句中断,但不起作用。那么你能告诉我,我怎样才能让它正常工作,我应该添加/更改什么?

设置UI.class

public class SettingsUI implements UIConstants {

    private PText menuTitle;
    private PText optionTitle;

    private static final int VIDEO_ROWS = 3;
    private static final int AUDIO_ROWS = 2;
    private static final int TOTAL_COLS = 2;

    private PButton[][] videoButtons = new PButton[VIDEO_ROWS][TOTAL_COLS];
    private PButton[][] audioButtons = new PButton[AUDIO_ROWS][TOTAL_COLS];

    private static final int BLOCK_X_SPACING = 40;
    private static final int BLOCK_Y_SPACING = 50;

    private static final String ARROW_LEFT_RES = "res/gui/button_arrow_left.png";
    private static final String ARROW_RIGHT_RES = "res/gui/button_arrow_right.png";

    public SettingsUI() throws SlickException {
        initComponents();
    }

    private void initComponents() throws SlickException {
        this.menuTitle = new PText(BASE_COLOR, 34, false);
        this.optionTitle = new PText(BASE_COLOR, 22, false);

        for (int row = 0; row < VIDEO_ROWS; row++) {
            for (int col = 0; col < TOTAL_COLS; col++) {
                switch (col) {
                    case 0:
                        videoButtons[row][col] = new PButton(ARROW_LEFT_RES, null);
                        continue;
                    case 1:
                        videoButtons[row][col] = new PButton(ARROW_RIGHT_RES, null);
                        continue;
                }
            }
        }
        for (int row = 0; row < AUDIO_ROWS; row++) {
            for (int col = 0; col < TOTAL_COLS; col++) {
                switch (col) {
                    case 0:
                        audioButtons[row][col] = new PButton(ARROW_LEFT_RES, null);
                        continue;
                    case 1:
                        audioButtons[row][col] = new PButton(ARROW_RIGHT_RES, null);
                        continue;
                }
            }
        }
    }

    public void update(GameContainer container, int delta) throws SlickException {
        Input input = container.getInput();
        int mx = input.getMouseX();
        int my = input.getMouseY();

        // case width/height
        if (videoButtons[0][0].contains(mx, my)) {
            if (videoButtons[0][0].isButtonPressed(input)) {
                decreaseValue();
            }
        } else if (videoButtons[0][1].contains(mx, my)) {
            if (videoButtons[0][1].isButtonPressed(input)) {
                increaseValue();
            }
        }
        /*
        for (int row = 0; row < VIDEO_ROWS; row++) {
            for (int col = 0; col < TOTAL_COLS; col++) {
                if (videoButtons[row][col].contains(mx, my)) {
                    if (!videoButtons[row][col].isButtonPressed(input)) continue;
                    System.out.println("row: " + row + " / col: " + col);
                    switch (col) {
                        case 0:
                            decreaseValue();
                            break;
                        case 1:
                            increaseValue();
                            break;
                    }
                }
            }
        }

        for (int row = 0; row < AUDIO_ROWS; row++) {
            for (int col = 0; col < TOTAL_COLS; col++) {
                if (audioButtons[row][col].contains(mx, my)) {
                    if (!audioButtons[row][col].isButtonPressed(input)) continue;
                    switch (col) {
                        case 0:
                            break;
                        case 1:
                            break;
                    }
                }
            }
        }
        */
    }

    // temp
    private static final int[][] screen_size = {
            {800, 600},
            {1000, 700},
            {1200, 800},
            {1400, 900}
    };
    private void increaseValue() {
        for (int i = 0; i < screen_size.length; i++) {
            if (screen_size[i][0] == Settings.Video.getWidth() && screen_size[i][1] == Settings.Video.getHeight()) {
                if (i < screen_size.length - 1) {
                    Settings.Video.setWidth(screen_size[i+1][0]);
                    Settings.Video.setHeight(screen_size[i+1][1]);
                    break;
                }
            }
        }
        System.out.println("WIDTH SIZE: " + Settings.Video.getWidth() + " / HEIGHT SIZE: " + Settings.Video.getHeight());

    }
    private void decreaseValue() {
        for (int i = 0; i < screen_size.length; i++) {
            if (screen_size[i][0] == Settings.Video.getWidth() && screen_size[i][1] == Settings.Video.getHeight()) {
                if (i > 0) {
                    Settings.Video.setWidth(screen_size[i-1][0]);
                    Settings.Video.setHeight(screen_size[i-1][1]);
                    break;
                }
            }
        }
        System.out.println("WIDTH SIZE: " + Settings.Video.getWidth() + " / HEIGHT SIZE: " + Settings.Video.getHeight());
    }

    // temp
    private static String[][] VIDEO_ORDER = {
            {"FRAME SIZE", String.valueOf(Settings.Video.getWidth() + " x " + Settings.Video.getHeight())}, 
            {"FULL SCREEN", String.valueOf(Settings.Video.isFullscreen())},
            {"FPS", String.valueOf(Settings.Video.isFps())}
    };
    private static String[][] AUDIO_ORDER = {
            {"TURN SOUND", String.valueOf(Settings.Audio.isSound())}, 
            {"VOLUME", String.valueOf(Settings.Audio.getVolume())}
    };

    public void render(GameContainer container, Graphics g) throws SlickException {
        int x = CONTAINER_CENTER_X / 2 - 15;
        int y = CONTAINER_MAX_Y / 6 + 40;

        menuTitle.renderText("VIDEO:", g, x, y);
        for (int row = 0; row < VIDEO_ROWS; row++) {
            renderBlock(g, optionTitle, videoButtons, VIDEO_ORDER[row][0], VIDEO_ORDER[row][1], x, y, row);
        }

        x = CONTAINER_CENTER_X / 2 - 15;
        y = CONTAINER_CENTER_Y + 40;

        menuTitle.renderText("AUDIO:", g, x, y);
        for (int row = 0; row < AUDIO_ROWS; row++) {
            renderBlock(g, optionTitle, audioButtons, AUDIO_ORDER[row][0], AUDIO_ORDER[row][1], x, y, row);
        }
    }

    private void renderBlock(Graphics g, PText optionTitle, PButton[][] buttons, String option, String value, int x, int y, int row) throws SlickException {
        x += BLOCK_X_SPACING;
        y += BLOCK_Y_SPACING;

        optionTitle.renderText(option, g, x, y + (BLOCK_Y_SPACING * row));

        for (int col = 0; col < TOTAL_COLS; col++) {
            int bx = x + x - BLOCK_X_SPACING + (BLOCK_X_SPACING * col + (x * col));
            int by = y - 10 + (row * BLOCK_Y_SPACING);

            buttons[row][col].renderImage(g, bx, by);
        }

        x = x + x + BLOCK_X_SPACING + 25;
        y = y + (BLOCK_Y_SPACING * row);

        optionTitle.renderText(value, g, x, y);
    }

}

SettingsScreenState.class

public class SettingsScreenState extends BasicGameState implements UIConstants {

    public static final int ID = 5;
    private SettingsUI settingsUI;
    private Image titleImage;
    private PButton backButton;

    @Override
    public int getID() {
        return ID;
    }

    @Override
    public void init(GameContainer container, StateBasedGame game) throws SlickException {
        this.settingsUI = new SettingsUI();
        this.titleImage = new Image("res/gui/title_settings.png");
        this.backButton = new PButton("res/gui/button_back.png", "res/gui/button_back_hover.png");
    }

    @Override
    public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
        RandomUtility.bgTheme(container, g);

        g.drawImage(titleImage, 50, 50);
        this.settingsUI.render(container, g);
        this.backButton.renderImage(g, CONTAINER_CENTER_X - backButton.getWidth() / 2, CONTAINER_MAX_Y - backButton.getHeight() - 30);
    }

    @Override
    public void update(GameContainer container, StateBasedGame game, int delta) throws SlickException {
        Input input = container.getInput();
        int mx = input.getMouseX();
        int my = input.getMouseY();

        this.settingsUI.update(container, delta);

        if (backButton.contains(mx, my)) {
            if (backButton.isButtonPressed(input)) {
                game.enterState(StartScreenState.ID, new FadeOutTransition(), new FadeInTransition());
            }
        }
    }

}

PButton.class

public boolean contains(int x, int y) throws SlickException {
    int minX = this.x;
    int minY = this.y;
    int maxX = this.x + image.getWidth();
    int maxY = this.y + image.getHeight();

    if ((x > minX && x < maxX) && (y > minY && y < maxY)) {
        if (hoverImage != null) {
            image = new Image(hoverImage);
        }
        return true;
    }
    image = new Image(normalImage);
    return false;
}

public boolean isButtonPressed(Input input) throws SlickException {
    return input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON);
}

所以,这是有问题的代码:

public void update(GameContainer container, int delta) throws SlickException {
        Input input = container.getInput();
        int mx = input.getMouseX();
        int my = input.getMouseY();

        // case width/height
        if (videoButtons[0][0].contains(mx, my)) {
            if (videoButtons[0][0].isButtonPressed(input)) {
                decreaseValue();
            }
        } else if (videoButtons[0][1].contains(mx, my)) {
            if (videoButtons[0][1].isButtonPressed(input)) {
                increaseValue();
            }
        }

这是我用来增加/减少价值的方法:

private static final int[][] screen_size = {
            {800, 600},
            {1000, 700},
            {1200, 800},
            {1400, 900}
    };
    private void increaseValue() {
        for (int i = 0; i < screen_size.length; i++) {
            if (screen_size[i][0] == Settings.Video.getWidth() && screen_size[i][1] == Settings.Video.getHeight()) {
                if (i < screen_size.length - 1) {
                    Settings.Video.setWidth(screen_size[i+1][0]);
                    Settings.Video.setHeight(screen_size[i+1][1]);
                    break;
                }
            }
        }
        System.out.println("WIDTH SIZE: " + Settings.Video.getWidth() + " / HEIGHT SIZE: " + Settings.Video.getHeight());

    }
    private void decreaseValue() {
        for (int i = 0; i < screen_size.length; i++) {
            if (screen_size[i][0] == Settings.Video.getWidth() && screen_size[i][1] == Settings.Video.getHeight()) {
                if (i > 0) {
                    Settings.Video.setWidth(screen_size[i-1][0]);
                    Settings.Video.setHeight(screen_size[i-1][1]);
                    break;
                }
            }
        }
        System.out.println("WIDTH SIZE: " + Settings.Video.getWidth() + " / HEIGHT SIZE: " + Settings.Video.getHeight());
    }
4

1 回答 1

1

input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)当鼠标按钮按下时,每次更新都会调用。

input.isMousePressed(Input.MOUSE_LEFT_BUTTON)每次鼠标点击只调用一次。

于 2013-09-21T23:23:18.263 回答