Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7775397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:37:15+00:00 2026-06-01T17:37:15+00:00

EDIT (4/3/2017): Sorry, I was a noob back then. I’m trying to make a

  • 0

EDIT (4/3/2017): Sorry, I was a noob back then.

I’m trying to make a turn-based battle system where the player clicks buttons on his turn. But I can’t seem to find out how to code it. Below is the code on what I did.

What should happen here is that when I click the attack button(for example) the next turn will be the monster’s turn but the playerTurn variable doesn’t change when I click the button. playerTurn is always true. Can you help me correct this? It is a turn-based battle system.

 public class BattleFrame extends JFrame implements ActionListener, Runnable {

    private JButton atkButton = new JButton("Attack");
    private JButton runButton = new JButton("Run");
    private JButton itemButton = new JButton("Item");
    private JButton magicButton = new JButton("Magic");

    private JPanel panelButtons = new JPanel();

private Random rand = new Random();
private Boolean playerTurn;
private Thread t;

public BattleFrame() {
    setSize(480, 390);
    setLayout(null);

            // I have not included the code with the setting of the JButtons
    initPanel(); // initialize the panel with buttons

    setResizable(false);
    setVisible(true);
    playerTurn = true;
    t = new Thread(this);
    t.start();
}

// I'm not so familiar with 'synchronized' but I tried it here but it doesn't change anything
public void actionPerformed(ActionEvent e) {
   Object src = e.getSource();

     if(src.equals(atkButton) && playerTurn) {
          System.out.println("Attack!");
      playerTurn = false;
 }
 else if(src.equals(runButton) && playerTurn) {
      System.out.println("Run!");
      playerTurn = false;
 }

 else if(src.equals(itemButton) && playerTurn) {
      System.out.println("Item");
      playerTurn = false;
 }

 else if(src.equals(magicButton) && playerTurn) {
      System.out.println("Magic");
      playerTurn = false;
 }

}

public void run() {
    while(true) {
       if(playerTurn == false) {
          System.out.println("Monster's turn!"); // just printing whose turn it is
           playerTurn = true;
       }
       else System.out.println("player's turn!");
   }

 }

 public static void main(String[] args) {
    new BattleFrame();

   }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T17:37:17+00:00Added an answer on June 1, 2026 at 5:37 pm

    A Boolean is an object, so gets compared by identity, not value.

    assert new Boolean (true) == new Boolean(true);
    

    The above will fail, as the two different Boolean objects are not the same object.

    For general use, use the primitive type boolean, not the standard library class Boolean. Cases where you should use Boolean are pretty rare: it’s one of those things that exists more for symmetry than any real practical reason. If you do use it, you need to use a.equals(b) not a == b.

    For more details, see:

    http://www.java-samples.com/showtutorial.php?tutorialid=221

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT: I'll try and make it clearer sorry for the confusion. I have two
EDIT: Here's my call stack. System.ServiceModel.dll!System.ServiceModel.Channels.ServiceChannel.ThrowIfFaultUnderstood(System.ServiceModel.Channels.Message reply, System.ServiceModel.Channels.MessageFault fault, string action, System.ServiceModel.Channels.MessageVersion version, System.ServiceModel.Channels.FaultConverter
EDIT: Sorry about ellipsis that's not what I actually have. For declaring an array
Edit: based on a Ulf Rompe's comment, it is important you use 1 instead
[Edit: see below for final code] I have the following code and I'm trying
EDIT - added .h file I'm having difficulty trying to find the cause of
Edit: Translated I have a RSS-feed that i want to parse. It's a podcast
EDIT How to simplify the following code: if(x(a) > x(b)) s = b; e
EDIT: I have a table with 3 rows like so. ID NAME REV 1
EDIT I made a screen shot to show better what I'm attempting to do,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.