File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Game Development and the fly likes Attack method in Game Program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Game Development
Bookmark "Attack method in Game Program" Watch "Attack method in Game Program" New topic
Author

Attack method in Game Program

Ian Mcloud
Ranch Hand

Joined: Oct 04, 2012
Posts: 76
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

Joined: Oct 04, 2012
Posts: 76
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

Joined: Oct 02, 2003
Posts: 11256
    
  16

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



There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Oct 04, 2012
Posts: 76
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

Joined: Oct 04, 2012
Posts: 76
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

Joined: Oct 04, 2012
Posts: 76
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

Joined: Oct 04, 2012
Posts: 76
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?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Attack method in Game Program