Slick2D でゲームを作成しています。まず、行き詰まった GUI の状態をお見せしましょう。
そのため、クラス SettingsUI で、増加/減少値メソッドを使用して、増加/減少メソッドを呼び出して配列から値を選択するたびに、増加矢印ボタンを押すと何が起こるかという問題があります (i+1) {1000, 700} 幅/高さの値である値は、{1400, 900} である最後の値までループを繰り返します。逆に同じことが起こります。減少すると、ゼロ要素になります。これは、デフォルトでは {800, 600} です。そして、(値を印刷することによって)わかったことは、その矢印ボタンをクリックすると、1回のクリックでその値の30倍のように印刷されます。ループ内で増加/減少するときにステートメント ブレークを追加しようとしましたが、機能しません。どうすればこれを適切に機能させることができますか、何を追加/変更すればよいですか?
SettingsUI.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());
}