Multiple objects listening for the same key press
Posted
by
xiaohouzi79
on Game Development
See other posts from Game Development
or by xiaohouzi79
Published on 2012-11-23T01:06:04Z
Indexed on
2012/11/23
5:11 UTC
Read the original article
Hit count: 259
architecture
I want to learn the best way to implement this:
I have a hero and an enemy on the screen. Say the hero presses "k" to get out a knife, I want the enemy to react in a certain way.
Now, if in my game loop I have a listener for the key press event and I identify a "k" was pressed, the quick and easy way would be to do:
// If K pressed
// hero.getOoutKnife()
// enemy.getAngry()
But what is commonly done in more complex games, where say I have 10 types of character on screen and they all need to react in a unique way when the letter "k" is pressed?
I can think of a bunch of hacky ways to do this, but would love to know how it should be done properly.
I am using C++, but I'm not looking for a code implementation, just some ideas on how it should be done the right way.
© Game Development or respective owner