class MyEventReceiver : public IEventReceiver{
public:
virtual bool OnEvent(SEvent event) {
if(animNode != 0 && event.EventType == irr::EET_KEY_INPUT_EVENT && !event.KeyInput.PressedDown) {
switch(event.KeyInput.Key)
{
case KEY_KEY_W :
case KEY_KEY_S : {
vector3df v = animNode->getPosition();
v.Y += event.KeyInput.Key == KEY_KEY_W ? 2.0f : -2.0f;
animNode->setPosition(v);
}//end case
}//end switch
}//end if
return false;
}//end method
};//end class
And it works, but not the way that I want it to. When I press and release W or S it moves the object up or down respectively. I want it, however, to be constant feedback. That while I'm holding down the key, it continues to move the object. I've been trying to figure it out for a while, and can't. Any suggestions?
You have to change a state variable when one of the keys is pressed.
During the loop, you check the state and modify the position at every iteration of the game loop.
Your code does not work because your conditions turn to false once you released the keys.
The following should work.. The 'event.KeyInput.PressedDown' event is spawned for every game loop iteration that the key is pressed down.. the '!event.KeyInput.PressedDown' is only spawned once, when you release the key.
class MyEventReceiver : public IEventReceiver{
public:
virtual bool OnEvent(SEvent event) {
if(animNode != 0 && event.EventType == irr::EET_KEY_INPUT_EVENT && event.KeyInput.PressedDown) {
switch(event.KeyInput.Key)
{
case KEY_KEY_W :
case KEY_KEY_S : {
vector3df v = animNode->getPosition();
v.Y += event.KeyInput.Key == KEY_KEY_W ? 2.0f : -2.0f;
animNode->setPosition(v);
}//end case
}//end switch
}//end if