Right now for my game, I have a scene manager and it runs a scene. What it does is send event messages to the scene such as render, input, etc. This has allowed me to make the scene unaware of the scene manager. I would now like the scene to be able to send the scene manager a message saying which scene it would like to switch to (in plain text). How could I do this withou开发者_如何学Pythont making the scene aware of the scene manager class?
Thanks
Consider the Observer Pattern: your scene will provide an interface for observers to be notified of interesting events. The scene manager could be one such observer that is interested in "request scene change" events. The scene would then send a notification to all observers when it is ready to do the swap.
One way is to have the scene manager pass in NOT a reference to itself, but a reference to a smaller object that only supports the small number of methods needed for the particular messages to pass.
This could be an abstract class (aka "interface") which the scene manager implements, or a separate object.
And if you're using reference counting or smart pointers, consider making it a weak back-reference...
精彩评论