We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Class Variable Usage across classes
Page Index Toggle Pages: 1
Class Variable Usage across classes (Read 1242 times)
Class Variable Usage across classes
Oct 1st, 2009, 11:29pm
 
What is the best way to pass Instance variables between classes?

I tried asking this question before on this board but realized I was quite unclear about what I was asking so.........

I provided an example of my question
http://www.openprocessing.org/visuals/?visualID=4876

The class World has a static method called collision() that is supposed to detect if the big ball has been struck by the little ones.For collision() to work correctly it needs to access the instance variables from the method  ship()  inside the class SimpleShip and the instance variables from the method rockmove() inside the class Rockellipse.

One thing to note is that Rockellipse is an inner class of the class Rockfield.(from my understanding this shouldn't matter)


I have tried

1.Making the classes Rockfield and Simple ship subclasses of World and using the comparison as a static function.(thinking that as a super class it could reach subclass instance variables)

2.Using the word public before the needed variables.(shrug shooting in the dark and moving the function collision() inside one of the classes.)

please help!!!

ps. If you see any other things that would improve my work please critique away. This is mainly an attempt to learn Java OOP.
Re: Class Variable Usage across classes
Reply #1 - Oct 1st, 2009, 11:51pm
 
The syntax is ObjectName.VariableName ... as stated before:
http://processing.org/discourse/yabb2/num_1254373611.html#4
No need to delete your old posts, it makes the threads confusing, and there are others who might have the same questions.

OOP (or just P) advice: write function names that are descriptive of their action, and try to keep those functions self-contained when possible instead of passing variables.  For example, no need to have a "ship()" function to which you pass mouseX and mouseY each frame -- you could easily just have an "update()" function that contained this code:
x = mouseX;
y = mouseY;
and then just call ship.update(); each frame.
I agree with your comments in the Rockellipse bit -- I would suggest rewriting it, using only variables for: x, y, size, and speed.

--Ben
Re: Class Variable Usage across classes
Reply #2 - Oct 3rd, 2009, 1:21pm
 
Thank you again. My Tongue definitely needs some practice. Smiley I followed your instruction this time and any one who wants to see how it turned out can look at it here

http://www.openprocessing.org/visuals/?visualID=4886

As always if any one see something that I could improve please feel free I'm still learning.

Also I'm moving this over into

http://processing.org/discourse/yabb2/num_1254601139.html#0

for any one who would like to follow.


Re: Class Variable Usage across classes
Reply #3 - Oct 4th, 2009, 2:07am
 
Nice start, a few suggestions:

- you can hide the cursor.

- it seems slower than I can figure it has a reason to be -- are you performing some unnecessary operations?

- a reset button would be nice!  It can be as simple as:
void keyPressed(){ if (key=='r') setup(); }  (which is technically bad practice; you can make a "resetGame()" function to be more kosher)

- garbage collection is messing with your timing, because you're using built-in frame rate.  The "right" way to time your game is to separate game logic from rendering, so that you can update the game logic at a function of the millis() (time the program has been running), and still render each frame.

- it could be a lot harder!  Maybe your computer is faster than mine.  Again, accurate timing will fix that...

--Ben
Re: Class Variable Usage across classes
Reply #4 - Oct 4th, 2009, 3:26pm
 
I appreciate the advice. I don't totally follow how to go about separating the game speed from the logic but I think that should be the next thing I figure out. Cheesy

I also just noticed that I put up the link wrong to the WIP post that I have put up but I have correct this.

http://processing.org/discourse/yabb2/num_1254601139.html#0

Page Index Toggle Pages: 1