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

2016/3/23

Shared variables

zobear888 zobear888

2016/3/23

#
I am making a game where one object needs to get the rotation of another. I want to have one object (a banana) put its rotation into the variable and have the 2nd object access it and set its own rotation to match. ex:(pseudo code) Banana: Var = getRotation() 2nd object: setRotation(Var) I think I could do this by defining the variable in a common super class, but I am not sure how this would work.
danpost danpost

2016/3/23

#
The rotation is already saved in a field provided by the Actor class and its value can be retrieved by the 'getRotation' method. You just need a reference to the Banana object itself so you can get the rotation of that specific object:
// with a defined variable
Actor banana =  < assignment of a reference to the banana >;
// in code
int bananaRotation = banana.getRotation();
zobear888 zobear888

2016/3/25

#
Thank you for the quick reply. Would this code be inside bananas's code or the 2nd object that is set to banana's rotation?
zobear888 zobear888

2016/3/25

#
Also, how would I assign a reference to banana? I have seen it before but am not clear on how a reference works. Thanks!
danpost danpost

2016/3/25

#
zobear888 wrote...
Also, how would I assign a reference to banana? I have seen it before but am not clear on how a reference works. Thanks!
It depends on whether you only have one banana in the world or more -- and then how would you want to determine which one, if more.
brooklynnetswm brooklynnetswm

2016/3/26

#
hello
brooklynnetswm brooklynnetswm

2016/3/28

#
hi
zobear888 zobear888

2016/3/28

#
Thank you again for the help, danpost. There will only be one banana on the world, the (unfinished) game is on my profile page. If you don't have the time to look at it, no problem. PS: yes I know banana is spelled wrong and no, no one thinks brooklynnetswn is cool.
zobear888 zobear888

2016/3/28

#
never mind, having problems with images and posting.
You need to login to post a reply.