We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hey,
I have a pretty massive class (well it's big for me!) that includes a collision detecting function. It's a class for objects like apples that fall down and when they hit the apple cart they disappear and add a point, or they hit the ground and just disappear.
I want to add two similar kinds of dropping items that also use the same collision points and also grant/detract points (this is why I use the 'points' int instead of just doing score++ for example). I'm confused how I can let something inherit this when I based my loops on the arraylist for this specific type of thing. Is it possible to use the @Override function somehow to replace it without having to basically copy/paste it all twice?
I have tried replacing the applelist.get(i).x part with x (and same for y), but then all the apples onscreen would be deleted instead of the specific apple.
This is the section of code that I want another class to inherit.
void objcollision() {
// boolean stuff
for (int i = applelist.size() - 1; i >= 0; i--) { // continuous loop
distX = pstartX - applelist.get(i).x;
distY = pstartY - applelist.get(i).y;
// boolean collision detection
if (distX > -55 && distX < 0
&& distY > -10 && distY < 6) {
distbool = true; // in range, true
} else {
distbool = false; // not in range, false
}
}
Answers
It's a while since I experimented with inheritance in Processing/Java; so not sure how up-to-date I am on this topic. Anyway - an abstract class might be a good place to start with this: you first define core properties that all your similar objects will share and then extend from this.
I've got a long forgotten demo on openprocessing that contains an example of inheriting from an abstract class (won't work in Processing 3 or possibly 2; but demonstrates the principles). In this case Ball extends the abstract class Body. Notice how Body sets properties that all children might have like x,y,vx,vy etc. and also contains methods that all children might need: move(), boundsWrap(), boundsBounce()
Also notice that when you know a method will need to be called from another method in the abstract class - as is the case with checkBounds() in move() - but the implementation is left to its children, you should define an empty placeholder method in the abstract class:
abstract void checkBounds();
IIRC without this the code won't compile and of course child classes can override parent properties/methods...
From your description this approach should be applicable to your class.
BtW, I've got a very old rudimentary online game example about a bucket catching raindrops:
http://studio.ProcessingTogether.com/sp/pad/export/ro.9mzpXAdqbigsO
Anyways, as @blindfish said, traditional OOP inheritance fits like a glove for your case here.
The trick is establishing some abstract class or interface which describes the general properties all your sprites/entities/actors got in common.
Let's call it Actor. It got coordinates x & y, dimensions w & h, and fill color c.
Also let's establish a contract that all Actor subclasses gotta implement methods update() & display().
Joining everything together we got something like this: ;;)
Now let's try out to be more specific and split Actor class into 2 more classes.
Dropper represents every Actor that drops towards the ground and got extra method hitGround()
Plus fields missed & sy representing respectively how many hit the ground and its vertical speed:
And finally another abstraction to represent each Actor that tries to catch Dropper objects.
This time its extra method is gotDropper() and fields caught & sx:
Finally we got our 1st non-
abstract class
: Apple. Whichextends
Dropper.In order to inherit from all those
abstract
methods, it is now obliged to finally implement them all.Therefore Apple gotta implement update(), display() & hitGround().
Below just some template of it so you got an idea how to finish it:
Now the same pattern for class Cart which
extends
Catcher:Thanks for the help, I'll have to try it next week once my other project is done :)
Hello again!
Thank you very much blindfish and GoToLoop. I made it work, although I eventually didn't use an abstract class anymore as I created two functions within the dropper class that all of them could use. It works perfectly, thank you :)
I just changed a few things like:
here I just used this.x = x etc :)
If I understand well the system :