I am writing a 2d game library on processing, and I am currently dealing with the physics side of things. I have a class called Object - which is used to manipulate an image. I want to be able to 'attach' my Physics class to the Object class - so that I can access all of the Physics functions through the Object i.e.:
//Scroll to left to see more of comments
class Object extends Game{ //It's worth pointing out that all of my classes extend a Game class
Object(String name){ //A way to add an image to my Object and initialise the class fully
PImage image = loadImage(name);
}
void attachPhysics(){ //I want to be able to call this so that I can directly access functions in the Physics class
}
}
class Physics extends Game { //My Physics class also extends the Game class
Physics(){
//Main initialisation here
}
void projectile(int angle, int speed, int drag){
//Projectile code goes here
}
}
So if I had these two classes, I would then be able to call on them like so:
//Scroll to left to see more of comments
void setup(){
Object ball = new Object("ball.gif");
}
void draw(){ //In processing draw is similar to main in java
ball.attachPhysics(); //I attach Physics
ball.projectile(40, 5, -1); //I should then be able to access Physics classes via the ball Object which can manipulate the ball Object (call on its functions as well)
}
If anyone could help me out on this I would be grateful, I can post the full code if you like. It's worth noting that processing is just java with some added functions, and this code currently isn't set up as a library, it is just being compiled directly from processing.
The ball and physics instances do not have reference to eachother. Consider doing something like this (in setup or draw, or better in some initialization routine outside both classes):
physics phys = new physics();
ball.attachPhysics(physics);
Then you have a reference to the physics instance in your ball and you can call methods on it. Probably the physics method projectile
would need to have a reference to your ball as well, so something like:
projectile(40, 5, -1, this);// this is the refence to the ball instance
Besides that, consider naming conventions of Java
I.e. start with Upper case (and don't call it object).