aspose file tools*
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 7032
    
  16

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?
Artlicles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
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: 36478
    
  16
You’re welcome and thank you for the blessing.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struggling with using methods from objects in another class
 
Similar Threads
Using Lists/ArrayLists, code doesn't work
comparator help
Please help, java program terminating unexpectedly without reason
problem with program hanging
Multiplayer Pong