This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Struggling with using methods from objects in another class

 
John Simer
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You ought to restrict direct access to fields; you would usually use private access rather than protected.
 
John Simer
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys, God Bless you Guys. He surely has personal plans for you guys
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome and thank you for the blessing.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic