从指定索引处的数组中获取正确的值

时间:2013-09-21 01:08:04

标签: java arrays lwjgl slick2d

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

  

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

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

SettingsUI.clas

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());
    }

1 个答案:

答案 0 :(得分:1)

当鼠标按钮关闭时,每次更新都会调用

input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)

每次点击鼠标只会调用

input.isMousePressed(Input.MOUSE_LEFT_BUTTON)