This site requires JavaScript, please enable it in your browser!
Greenfoot back
murphys
murphys wrote ...

2012/10/26

How to Access One Object From Another

murphys murphys

2012/10/26

#
I have followed the Greenfoot tutorial that explains how to access one abject from another. In the Pitch class I have added the following method: public Player1 getX() { return player; } and in the Stick class I access the method as follows: Pitch pitch = (Pitch) getWorld(); Player1 player= pitch.getX(); int xpos = player.getX(); This works fine as discussed in the tutorial. However, I wish to getX() for another class Player2. I have added the following to the Pitch class: public Player1 getX() { return player; } public Player2 getX() { return player2; } and it returns the following error: getX() is already defined in Pitch Is there anyway to define the same method for more than one object so they can be passed to other objects. Many thanks in advance.
davmac davmac

2012/10/26

#
You must give the two methods different names. It's usually best to choose a meaningful name, so I'd suggest that rather than 'getX' you use 'getPlayer1' and 'getPlayer2'.
You need to login to post a reply.