• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Attack method in Game Program

 
Ian Mcloud
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ihave a program with a Driver class, an super class called Entity, a sub-class called Player, and three more sub-classes called Sowrdsman, Archer and Ogre. Everthing compiles and works fine. However, I cannot seem to get the attack method to work as intended. What I want it to do is subtract an amount of energy from the players life points. Here is what I have so far:




Just so you can see, this is the Entity class:

 
Ian Mcloud
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried multiple things in the if statement, but nothing seems to work. I'm wondering if an "if statement" is even the right thing to use at this point.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12100
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
first, I find it interesting that you call it "life points" in your text, but name the variable "intLife" in your code. I assume these are the same thing, but can't be sure. If they are, they should be named the same.

Next, as the code sits, you would have to do something convoluted. You have a way to get the Entitiy's life points, and a way to set them. You could do something like

playerToAttack.setIntLife(playerToAttack.getIntLife() - numPointsToReduce);

or you could write a method

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You haven't said what's going wrong. (See ItDoesntWorkIsUseless(←click) for details.)

However:

Ian Burres wrote:



#1: You pass in a parameter damageToInflict. Passing a parameter means you're providing a value for that method to use. But then the very first thing you do in that method is throw away that value without using it, by assigning a new value to the parameter variable. Presumably what you want to do is subtract that damage value from playerToAttack's strength?

#2: That if test is pointless. It will always be true. We know it's an Entity because the parameter declaration Entity playerToAttack says it is.
 
Ian Mcloud
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about the confusion. I was using life points as a descriptor, such as the characters hit point level (D&D terminology if you're familiar with it).
 
Ian Mcloud
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Technically, nothing is going wrong. When the program executes it says that the player (a Swordsman, Archer or Ogre) is attacking; however, I want a random int value to be subtracted from the player's intLife value when the method is called in my Driver class. I think Fred's suggestion might work, so I'll try it and let you know how it goes. I should also mention that my Driver class creates two players. Couldn't cause damage to one without the other, right?
 
Ian Mcloud
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my current output when the program executes:

Player 1: Ogre3
Player 2: Archer4
Attacking
Please enter 1 to continue, 0 to quit.
 
Ian Mcloud
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess I should mention that given the assignment parameters, I have completed it correctly. However, I want to go above and beyond that. I'm probably stepping into an area I am not yet familiar with, but that's how we learn, right?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic