Results 1 to 5 of 5
- 04-26-2011, 11:32 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
Passing variables through classes?
My problem is quite simple, yet I haven't found any ways of doing this as of this moment.
I need to get the integer "playerHP" working in a subclass. For example, I'm creating a class for all the items my text RPG needs, yet I need them to have an effect on the initial battle. This would mean that they'd for example heal the player. How do I bring the variable from the initial main to the subclass?
- 04-26-2011, 11:50 PM #2
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
It would make it a lot easier to help you if you posted some code.
- 04-27-2011, 12:05 AM #3
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
There isn't very much code displaying what I've tried to do, and the rest of the code doesn't refer to it very much, as I've had problems knowing where to start. Also, I need this for a text RPG I'm making.
What I do have of code to display, though, is this:
Where atkordfs is a string that's used to execute commands, items is a class, and Potion is a public void that executes a heal.Java Code:if (atkordfs.equals("drink potion")){ items itemsObject = new items(); itemsObject.Potion();
Where trainerHP is the HP of the player.Java Code:public class items { public void Potion(){ System.out.print("You drink the potion, healing 50 HP"); trainerHP += 50;
What my problem is, is basically to transfer trainerHP from the main to the Items-class.
- 04-27-2011, 12:31 AM #4
It sounds like you're at an early stage of development. It might be a good time to rethink your object design. Objects should model the real-world objects they represent. That means they should contain the data that represents their characteristics, and provide methods that represent actions they could perform, or have performed on them.
I'd create a class that represents your player, including all their stats and equipment. Then give it methods to alter its state, like this:
Then think about what your items have in common. All of them can probably be "used" in some fashion. You could create an interface called Item that includes methods that can be called on any Item. (Or if it makes sense to have code in common between Items, make Item a class.) Either way, your potion could look something like this:Java Code:class Player { private int hp; void modifyHP(int amount) { hp = hp + amount; // test if 0 < hp < max hp } }
Then when your player drinks a potion, it might look like this:Java Code:class Potion implements Item { private boolean used = false; void useItem(Player target) { if(used == false) { target.modifyHP(50); used = true; } } }
Java Code:Player self = new Player(); Item[] inventory = new Item[20]; inventory[0] = new Potion(); // yadda yadda inventory[0].useItem(self);
Last edited by kjkrum; 04-27-2011 at 12:37 AM. Reason: bugs in fake code :-P
- 04-27-2011, 12:46 AM #5
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
A bit of confusion with passing variables/infinite loop
By kaizen9001 in forum New To JavaReplies: 12Last Post: 03-27-2011, 09:05 PM -
please help with passing variables between class's
By jasonwucinski in forum New To JavaReplies: 4Last Post: 02-11-2011, 01:27 AM -
Passing dynamic variables between classes
By Brekk in forum New To JavaReplies: 1Last Post: 03-22-2010, 06:07 AM -
passing variables to another frame
By Grom in forum New To JavaReplies: 10Last Post: 11-09-2008, 05:55 PM -
is synchronization on method passing local variables as parameters needed
By reddzer in forum Java ServletReplies: 0Last Post: 11-10-2007, 04:47 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks