Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By crmbuddy

Thread: Quick Question

  1. #1
    crmbuddy is offline Member
    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0

    Default Quick Question

    Hey guys I just have a quick question about a java program I am working on. So I'm working on a text based video game in NetBeans and I have my GUI class, an enemy class, player class, actor class (superclass of enemy and player) and a database class. The problem is that my database class uses an ArrayList to store all of the enemies players can fight. Now when players click the battle button in the GUI class this is what happens code wise:
    Java Code:
    private void battleButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
            if(!gamer.isFighting())
            {
                gamer.fighting();
            }
            
            if(gamer.isFighting())
            {
            monster = spawner.getMob(rand.nextInt(3));
            eName.setText(monster.getName());
            eLvl.setText("Lvl " + monster.getLevel());
            eHP.setText("HP: " + monster.getHealth());
            eMana.setText("Mana: " + monster.getMana());
            eAttack.setText("Attack: " + monster.getAttack());
            eArmor.setText("Armor: " + monster.getArmor());
            bLog1.setText("You've encountered a " + monster.getName());
            System.out.println(monster.getHealth());
            }
        }
    The problem is once the player clicks the battle button again after beating a monster, the data shown on the GUI shows zero health and zero armor. I think whats happening is the attack method (code here):
    Java Code:
     public void attack(Enemy badGuy)
        {
            badDude = badGuy;
            if(badDude.getHealth() > 0)
            {
            if(badDude.getArmor() > 0)
            {
                if(this.getAttack() > badDude.getArmor())
                {
                    badDude.setHealth(badDude.getHealth() -(this.getAttack() - badDude.getArmor()));
                    badDude.setArmor(0);
                }else{
                badDude.setArmor(badDude.getArmor() - this.getAttack());
                }
            }else{
               badDude.setHealth(badDude.getHealth() - this.getAttack());
            }
            
        }
            if(badDude.getHealth() < 0)
            {
                badDude.setHealth(0);
                
            }
        }
    is actually changing the value of the enemy in the arraylist. Thanks for your time and I hope you guys can help me out!
    -crmbuddy
    Last edited by crmbuddy; 02-11-2013 at 01:12 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Quick Question

    I'm having some difficulty understanding your code and your question, and I may not be alone. Please let me offer suggestions on how to get better help.
    • Please use a better subject heading for your question. "Quick Question" tells us nothing about your problem and you lose a valuable opportunity to communicate to us a very brief summary of just what is wrong. We already know that you have a question, else you wouldn't be posting here. Consider this heading to be similar to a newspaper article heading.
    • When posting code in the forum, please use code tags around well formatted code to allow us to be able to better be able to read and understand your code. The tag [code] goes above your posted code block, and the tag [/code] goes below it.
    • Please tell us more about what your current code is doing that it shouldn't and what it isn't doing that it should.


    Much luck!

  3. #3
    crmbuddy is offline Member
    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0

    Default Re: Quick Question

    Well what I have is an object of type Database. This object has an ArrayList variable in it that holds all the Enemy objects for the game. The problem is that I want the ArrayList to act as a reference for Enemy information. This means I don't want the data changing. The thing is though, when players of this game hit the battle button and defeat an Enemy the actual variables of the Enemy in the ArrayList in the Database class change. As seen above in the first code segment I create another Enemy object that takes the information from the Database object so that the information in the ArrayList doesnt change. Hopefully you can help and thanks for the tips!

    -Crmbuddy
    Fubarable likes this.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Quick Question

    The key information will likely be held in the spawner.getMob(rand.nextInt(3)) method. It sounds like you're returning an object held by the ArrayList, when in reality, you want this method to return a *copy* of an object held by list. So consider creating some facility that will allow you to copy objects held by the list, such as making your Monster Clonable and by returning a deep clone in the getMob(...) method, or give it a copy constructor or even a static factory method to allow you to create random Monster instances.

    Thanks for the information and question improvement by the way!!

  5. #5
    crmbuddy is offline Member
    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0

    Default Re: Quick Question

    Well here is the code for the getMob(...) method. I am not too sure as to how I would do that with what I've done so far.
    Java Code:
     public Enemy getMob(int inx)
        {
            creat = creatures.get(inx);
            return creat;
        }
    -Crmbuddy

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. quick question
    By biggerthanblue in forum New To Java
    Replies: 2
    Last Post: 04-10-2011, 04:33 AM
  2. Really Quick question
    By Ryan10 in forum New To Java
    Replies: 63
    Last Post: 03-02-2011, 02:11 AM
  3. Quick Question...
    By FatalSylence in forum New To Java
    Replies: 4
    Last Post: 10-15-2010, 02:38 PM
  4. Really quick question
    By shadycharacter in forum New To Java
    Replies: 2
    Last Post: 04-22-2010, 10:06 PM
  5. Quick Question
    By Spenc in forum New To Java
    Replies: 3
    Last Post: 09-22-2008, 02:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •