76
SFML projects / Re: Flappy-Bird Clone Attempt
« on: January 25, 2018, 02:54:14 am »
If an object needs to know some data of another object (that is very common), the only way (at least what my experience tells me) is to pass a reference of the 2nd to the 1st(*). Usually in my games, every enemy or animated object (such as blocks in Super Mario games) has a reference of the player (that is passed in its constructor) and so they are able to process both their atacks to the player and player's attacks to them.
If the bird is your game main character, I think it would be better that the other objects have a reference to it. In your game class you create a Bird object, and then when you create the enemies and other objects, you pass them a reference to the Bird. So they can read and modify the player's attributes, and theirs, in function of it. Because the bird's control depends on you (even if you crash against an enemy and die), not on the enemies, but what they do (attack you, die if you kill them, etc) does depend on the bird (and on other instruction set or behavior you program them to do that are independent of anything). That's why it is better to pass a reference of the player to the other objects and not on the contrary.
(*) Remember that in C++ the subject of references and pointers and all that stuff is not simple as it is in C#.
Hope this helps
If the bird is your game main character, I think it would be better that the other objects have a reference to it. In your game class you create a Bird object, and then when you create the enemies and other objects, you pass them a reference to the Bird. So they can read and modify the player's attributes, and theirs, in function of it. Because the bird's control depends on you (even if you crash against an enemy and die), not on the enemies, but what they do (attack you, die if you kill them, etc) does depend on the bird (and on other instruction set or behavior you program them to do that are independent of anything). That's why it is better to pass a reference of the player to the other objects and not on the contrary.
(*) Remember that in C++ the subject of references and pointers and all that stuff is not simple as it is in C#.
Hope this helps