2013-09-21 49 views
1

我正在Slick2D中创建一个游戏。首先,让我向你展示卡住的GUI状态。从指定索引处的数组中获取适当的值

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

所以,我有一个类中的问题SettingsUI,随着/ decreasingValue方法,每当我呼吁增加/减少方法,从数组中选择值,当我按下时会发生什么增加箭头按钮,而不是给我(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

请a)http://sscce.org和b)至少如果您发布了您的代码,请不要发布两次。无论如何,“打印重复30次”的问题可能意味着你可能会添加到同一个鼠标监听器的许多副本。 – SJuan76

回答

1

input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)被称为在每次更新时,鼠标按键时。

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

+0

嗯,你迟到了..但是谢谢:) – nellykvist

相关问题