質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Swing

SwingはJavaに標準で付属するグラフィック関連のクラスライブラリを指します。

Q&A

解決済

1回答

2802閲覧

ドラクエ風の戦闘画面を作成したものの、メソッド間の移動がうまくいきません

JavaTakashi

総合スコア2

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Swing

SwingはJavaに標準で付属するグラフィック関連のクラスライブラリを指します。

1グッド

0クリップ

投稿2020/09/27 03:52

発生している問題・エラーメッセージ

ドラクエ風の戦闘画面を作成したのですが、戦闘が終了してもフィールド画面に切り替わらず、再び新しい戦闘画面に突入してしまいます。
添付のソースコードで説明させていただくと、

  1. fightメソッドで怪物と対峙、そして退治します。
  2. winメソッドに移り、choice1ボタンを押下。
  3. battleEndメソッドに切り替わるはずが、なぜか再びfightメソッドに戻ります。

他のメソッド間の切り替えは問題ないのに、なぜかここだけがうまくいきません。

該当のソースコード

import java.awt.CardLayout; import java.awt.Color; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.function.Consumer; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.UIManager; import javax.swing.border.LineBorder; public class HogeQuest extends JFrame { public static final Font normalFont = new Font(Font.SERIF, Font.PLAIN, 20); public static void main(String[] args) { try { //macで起動した場合に発生する表示バグを防ぐ UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); } catch (Exception e) { e.printStackTrace(); } new HogeQuest().setVisible(true); } public HogeQuest() { setBounds(0, 0, 1300, 950); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new CardLayout()); getContentPane().setBackground(Color.BLACK); add(new Home()); } class Home extends JPanel { private final JTextArea textArea; private final JButton choice1; private final JButton choice2; private final JButton choice3; private final JLabel imageLabel; private final JLabel hpLabelNumber; private final JLabel lvLabelName; private final ImageIcon image1 = new ImageIcon("./image01.jpg"); private final ImageIcon image2 = new ImageIcon("./image02.jpg"); int playerHP, playerHP_MAX, playerLevel, exp, monsterHP; private Consumer<String> choiceAction; //// ゲームのセットアップ public Home() { setLayout(null); setBackground(Color.BLACK); playerHP = 20; playerHP_MAX = 20; //最大HP playerLevel = 1; //レベル exp = 0; //経験値 monsterHP = 20; //怪物のHP JPanel imagePanel = new JPanel(); //画像が表示されるエリア imagePanel.setBounds(40,40,720,480); imageLabel = new JLabel(); imagePanel.add(imageLabel); add(imagePanel); JPanel textPanel = new JPanel(); //テキストが表示されるエリア textPanel.setBounds(60, 540, 680, 200); textPanel.setBackground(Color.BLACK); textArea = new JTextArea(); textArea.setBounds(60, 520, 680, 200); textArea.setBackground(Color.BLACK); textArea.setForeground(Color.WHITE); textArea.setFont(normalFont); textArea.setLineWrap(true); textPanel.add(textArea); add(textPanel); JPanel choicePanel = new JPanel(); //選択肢のボタン choicePanel.setBounds(800, 400, 350, 180); choicePanel.setBackground(Color.BLACK); choicePanel.setLayout(new GridLayout(3,1)); add(choicePanel); ChoiceHandler choiceHandler = new ChoiceHandler(); choice1 = new JButton(); choice1.setBackground(Color.BLACK); choice1.setForeground(Color.WHITE); choice1.setFont(normalFont); choice1.setFocusPainted(false); choice1.addActionListener(choiceHandler); choice1.setActionCommand("c1"); choicePanel.add(choice1); choice2 = new JButton(); choice2.setBackground(Color.BLACK); choice2.setForeground(Color.WHITE); choice2.setFont(normalFont); choice2.setFocusPainted(false); choice2.addActionListener(choiceHandler); choice2.setActionCommand("c2"); choicePanel.add(choice2); choice3 = new JButton(); choice3.setBackground(Color.BLACK); choice3.setForeground(Color.WHITE); choice3.setFont(normalFont); choice3.setFocusPainted(false); choice3.addActionListener(choiceHandler); choice3.setActionCommand("c3"); choicePanel.add(choice3); JPanel statusPanel = new JPanel(); //主人公のHP、レベルの数値を表示するパネル statusPanel.setBounds(900, 40, 240, 120); statusPanel.setBackground(Color.BLACK); statusPanel.setBorder(new LineBorder(Color.WHITE, 4, false)); statusPanel.setLayout(new GridLayout(2,2)); JLabel hpLabel = new JLabel(" HP:"); /////// HP hpLabel.setFont(normalFont); hpLabel.setForeground(Color.WHITE); statusPanel.add(hpLabel); hpLabelNumber = new JLabel(); hpLabelNumber.setFont(normalFont); hpLabelNumber.setForeground(Color.white); statusPanel.add(hpLabelNumber); JLabel lvLabel = new JLabel(" LV:"); /////// レベル lvLabel.setFont(normalFont); lvLabel.setForeground(Color.WHITE); statusPanel.add(lvLabel); lvLabelName = new JLabel(); lvLabelName.setFont(normalFont); lvLabelName.setForeground(Color.WHITE); statusPanel.add(lvLabelName); add(statusPanel); hpLabelNumber.setText("" + playerHP + "/" + playerHP_MAX); lvLabelName.setText("" + playerLevel); field(); /////// 最初のシーンへ移動 } private void field() { imageLabel.setIcon(image1); textArea.setText("あなたの眼前を怪物が徘徊している"); choice1.setText("戦う"); choice1.setVisible(true); choice2.setVisible(false); choice3.setVisible(false); // 匿名クラスで choiceAction = new Consumer<String>() { @Override public void accept(String yourChoice) { switch (yourChoice) { case "c1": fight(); break; } } }; } private void fight() { imageLabel.setIcon(image2); int message =(int)( Math.random()*2); switch(message){ case 0: textArea.setText("怪物は牙を剥き出し、あなたに襲いかかろうとしている");break; case 1: textArea.setText("怪物は低い唸り声をあげ、あなたの周囲を廻りはじめた");break; } choice1.setText("攻撃する"); choice2.setText("薬草をのむ"); choice3.setText("逃げる"); choice2.setVisible(true); choice3.setVisible(true); choiceAction = x -> { switch (x) { case "c1": attack(); break; case "c2": heal(); break; case "c3": run(); break; } }; } private void heal() { /////////////「薬草をのむ」を選択 textArea.setText("体力が回復した!"); choice1.setText(">"); choice2.setVisible(false); choice3.setVisible(false); playerHP = playerHP_MAX; hpLabelNumber.setText("" + playerHP + "/" + playerHP_MAX); choiceAction = x -> { switch (x) { case "c1": fight(); break; } }; } private void run() { /////////////「逃げる」を選択 textArea.setText("あなたは怪物の間隙を窺い、命からがら逃げ出すことに成功した"); choice1.setText(">"); choice2.setVisible(false); choice3.setVisible(false); choiceAction = x -> { switch (x) { case "c1": field(); break; } }; } public void attack(){ /////////////「攻撃する」を選択 int playerDamage =0; int monsterDamage =0; playerDamage = new java.util.Random().nextInt(6)+1 ; monsterDamage = new java.util.Random().nextInt(4) +2; choice1.setText(">"); choice2.setVisible(false); choice3.setVisible(false); textArea.setText("あなたは怪物に斬りかかった!\n怪物に" + playerDamage + " のダメージを与えた!\n\n" + "怪物は猛烈な勢いで飛びかかってきた!\nあなたは" + monsterDamage + " のダメージを受けた!"); monsterHP = monsterHP - playerDamage; playerHP = playerHP - monsterDamage; hpLabelNumber.setText("" + playerHP + "/" + playerHP_MAX); if(playerHP <= 0){ hpLabelNumber.setText("0/" + playerHP_MAX); dead(); } else if(monsterHP < 1){ win(); } else if(playerHP > 0){ } choiceAction = x -> { switch (x) { case "c1": fight(); break; } }; } public void win(){ int expUp; //獲得する経験値 expUp = new java.util.Random().nextInt(30) + 200; textArea.append("\n\n激しい格闘の末、怪物を退治した。\nあなたは " + expUp + " の経験値を獲得した。"); exp = exp + expUp; monsterHP = 20; //怪物のHPをリセット choice1.setText(">"); choiceAction = x -> { switch (x) { case "c1": battleEnd();break; } }; } public void battleEnd() { if((exp >= 700) && (playerLevel==2)){ textArea.setText( "あなたはレベル 3 にあがった!"); playerLevel = 3; playerHP_MAX = 25; hpLabelNumber.setText("" + playerHP + "/" + playerHP_MAX); lvLabelName.setText("" + playerLevel); } else if((exp >= 260) && (playerLevel==1)){ textArea.setText( "あなたはレベル 2 にあがった!"); playerLevel = 2; playerHP_MAX = 20; hpLabelNumber.setText("" + playerHP + "/" + playerHP_MAX); lvLabelName.setText("" + playerLevel); } else { field(); //レベルアップの条件を満たさない場合はフィールドへ } choice1.setText(">"); choiceAction = x -> { switch (x) { case "c1": field();break; } }; } public void dead(){ textArea.setText( "GAME OVER"); choice1.setVisible(false); choice2.setVisible(false); choice3.setVisible(false); } private class ChoiceHandler implements ActionListener { @Override public void actionPerformed(ActionEvent event) { String yourChoice = event.getActionCommand(); choiceAction.accept(yourChoice); } } } }

補足情報

環境:java 1.8 / macOS

TN8001👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

choiceActionが上書きされてしまっています。

Java

1public void attack() { 2// 省略 3 4 if (playerHP <= 0) { 5 hpLabelNumber.setText("0/" + playerHP_MAX); 6 dead(); 7 } else if (monsterHP < 1) { 8 win(); 9 } else { 10// こうしなければならない 11 choiceAction = x -> { 12 switch (x) { 13 case "c1": fight(); break; 14 } 15 }; 16// 選択肢が一個しかないのが確定しているならばこれでいい 17// choiceAction = x -> fight(); 18 } 19 20// winを抜けた後ここでfightに上書きされている 21// choiceAction = x -> { 22// switch (x) { 23// case "c1": fight(); break; 24// } 25// }; 26}

自分で提案しておいてなんですが、やっぱり処理が追いにくいですね^^;
async/await的なものがあれば、すっきり書けそうな気もするんですが。

投稿2020/09/27 04:56

TN8001

総合スコア9317

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

JavaTakashi

2020/09/27 14:14

さっそくのご回答をありがとうございます。 ご指摘の通りに修正したところ、問題が解決しました。前回に引き続き誠にありがとうございます。 こんな風にchoiceActionが上書きされてしまっていたとは、なんとも奥深いjavaの世界。。。 >自分で提案しておいてなんですが、やっぱり処理が追いにくいですね^^; 前回ご提案いただいたラムダ式の記述で、その後見違えるほど可読性が良くなりました。 ただ、見よう見真似で理解が追いついていない現状でもあります。 回答でのご指摘はたいへん勉強になります。重ねてありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問