Reputation: 1
http://text-share.com/view/044ca96e
So I have recently started to learn Java (with no no previous programming exp), and by now I have got some very basic concepts in my head clear. Or maybe not so, because I have just started my first "real life program" and I seem to be lost with access levels or something like that.
http://text-share.com/view/044ca96e
But to ask something concrete -
(I have Main.java, Creature.java, Player.java, Monster.java and Room.java in the same package.)
1, I suppose I should create the Player/Monster objects with the constructor in the Player/Monster class itself, right? Problem is, when I do so, I do not have access to these objects or their methods in main?
2, I can not use player1.getHp or any methods from Player class within Monster class either.
I suspect that there is some structural issue in my classes, but can not figure out what it is. This might be the noobest question of 2018 so far, but I would appreciate any help. :)
Thanks in advance.
Upvotes: 0
Views: 139
Reputation:
OK, ok. As I mentioned before, you need to think about "main" and basic oo principles. You have defined (I guess, because you have posted no code) some classes. I'm again trying to adjust my mind to what it is that you are trying to do now.
I think you are (conceptually) trying to place Monster, Creature, Player inside Room. I get that. However, your class - Room - is just another object (if you instantiate it) within Main.
In the eyes of any oo language, Room, Creature, Player are all nothing more than objects ; despite the fact that you think (conceptually) the Creature is inside the Room with the Monster. There is no such hierarchy in the program like there is in your brain.
Additionally, you are using the words "package" and "public" in here as though they might be the problem and hence reveal a solution : they won't.
You could watch a really good youtube tutorial on Java programming which will take 2-3 hours of your time. OR - you could keep hacking away at stuff and never get it right. I myself have been guilty of "hacking away" over the years to my own cost.
If you do watch the tutorial(s) then I implore you to not skip past the bits that bore you. It's ALL important. Otherwise it wouldn't exist. And given what you are trying to do, I would pay special attention to something called "static".
Upvotes: 0
Reputation: 9609
You have to understand the scope of your objects. Just because you created a Player and Room in your Main class, doesn't mean instances of those classes have access to each other by default. For example, in Room, you try to use player1 but there is nothing in Room that takes in a Player argument. You either have to create a Player instance inside Room or pass it in. So, a constructor or setter would have to set the player in the Room class for this to work.
So, in Main, you'd have something like
Player player = new Player();
Room room = new Room(player);
And in Room, you'd have a local Player variable which the constructor would set.
Upvotes: 1
Reputation:
Declare your Monster, Creature etc in their respective .java files then in Main.java you can go create your objects :
Monster mon = new Monster(); //default constructor
Creature cre = new Creature(); // ditto
Then you can access mon.variable
or call method mon.method()
etc within your Main.java.
I think that's where you're getting confused.
You want your Creature
, Monster
, Player
to interact with each other in that Room
don't you(?)
So now if Monster kills Creature and takes all it's health points in the process then the code in main.java would look like this.
mon.health = mon.health + cre.health;
cre.health = 0; //i.e dead
There's a few more things to do with the way you are thinking about "main" but that's enough for now.
Upvotes: 0