I am following CS106A lectures from youtube and I'm stuck with one of the assignments.
I am building a Breakout game Assignment PDF
I am encountering some difficulties on object collision department. I've created the method getCollidingObject() like instructions says.
Problem: When the ball collides with paddle the getCollidingObject method returns GRect and the will return true.
When the ball collides with a brick, the getCollidingObject will return something (when I printed returned object out, it just said GRect with some parameters) but the check does not work.
What I have tried to find out where the problem is:
1) I've printed out collision object when collided with bricks & paddle, both will return GRect object.
2) I've set getCollidingObject specifically to point to some random brick, it will return GRect object aswell.
When you are comparing two objects with the == operator you are asking if the two references point to the same object in memory i.e are those two objects the one and the same?
If collision == paddle returns true that means that collision and paddle are the same instance assigned to two references.
If collision == rect returns false it means that collision and rect are not the same object.
Conclusion: paddle and rect are not the same object i.e they might be of the same class but they are not the same instance of that class. Just looking at those two lines
How can you expect the colliding object to be both the paddle and the brick, or am I misunderstanding your code?