wood burning stoves
The moose likes Beginning Java and the fly likes Struggling with using methods from objects in another class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Struggling with using methods from objects in another class" Watch "Struggling with using methods from objects in another class" New topic
Author

Struggling with using methods from objects in another class

John Simer
Greenhorn

Joined: Dec 19, 2011
Posts: 16
So in my shooter (main) class

in my gameObject class


in my Player class which extends GameObject


and finally here's the problem in my shooter class under public void keyReleased(KeyEvent e) {

In my game, when you press ENTER, it sets player2's AND player1's ammo minus 1 of what player2's ammo was before
and when you press SPACE, it does not do anything.

Why is it recognizing only player2's methods and objects and not player 1's?

Thank you,
God Bless you

----EDIT------
Nevermind i solved the problem, when i was displaying the ammo on my game screen, i made a typo and put player2 for both
John Simer
Greenhorn

Joined: Dec 19, 2011
Posts: 16
But I do have another question.

should i code this as


or


both work fine, which is the better coding practice.
player2 is private in the shooter class, but the Player class is public.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8661
    
  23

John Simer wrote:both work fine, which is the better coding practice.

The getAmmo() one. If you're in doubt, always prefer getters over direct access of attributes.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 44038
    
  33
You ought to restrict direct access to fields; you would usually use private access rather than protected.
John Simer
Greenhorn

Joined: Dec 19, 2011
Posts: 16
Thank you guys, God Bless you Guys. He surely has personal plans for you guys
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 44038
    
  33
You’re welcome and thank you for the blessing.
 
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: Struggling with using methods from objects in another class
 
It's not a secret anymore!