I am working on a video game that is based on a tutorial I found online and I ran into an issue associated with accessing an object's method inside another class object's function without making the object global which seems bad.
Here is basically how the tutorial set things up in the game:
The game has a base class called GameObject then there are other child classes that inherit from this class. The GameObject class has all the basic information an object would have that needs rendered on the screen (x/y position, size of bitmap, bitmap, whether or not it is collidable, etc).
One of the child classes SpaceShip, which is the player so it has attributes and methods associated with managing # of lives and points scored.
There are other child classes of GameObjects in the game that need to take life and add points from the SpaceShip object if they collide with the spaceship or other objects.
In the collision handling routine I basically call a function "void Collided(int objectID)" when a game object collides with another. Within the Collided() routine, there is logic that executes code or other functions based on the objectID it collided with. Some of the collisions require taking life from the spaceship or adding points. The way this was accomplished in the tutorial was with function pointers (see Bullet class constructor and collided method for example). Is this really the best way to handle this sort of thing? It seems like there has to be a simpler way than to keep referencing function pointers in any new class I want to add to the game. I realized this when I went to add a method for the spaceship to fire bullets rather than inside my game class.
My Project Source found here:
https://www.dropbox.com/sh/zxoa1icyzjj6e0t/AAAZlXfFier09uG5gcQdCK8ba?dl=0