Movement Edit

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Lokzor
Posts: 4
Joined: Tue Apr 17, 2007 12:03 am
Contact:

Movement Edit

Post by Lokzor »

I was editing the movement tutorials, and the only change I made was the event reciever class. My exact MyEventReciever follows.

Code: Select all

class MyEventReceiver : public IEventReceiver
{
public:
       virtual bool OnEvent (SEvent event)
       {
       if (node != 0 && event.EventType == EET_KEY_INPUT_EVENT && !event.KeyInput.PressedDown)
       {
                switch(event.KeyInput.Key)
                {
                case KEY_KEY_W:
                case KEY_KEY_S:
                            {
                                  core::vector3df v = node->getPosition();
                                  while(event.KeyInput.PressedDown)
                                  {
                                  v.Y += event.KeyInput.Key == KEY_KEY_W ? 2.0f : -2.0f;
                                  node->setPosition(v);
                                  }
                            }
                            return true;
                switch(event.KeyInput.Key)
                {
                 case KEY_KEY_D:
                 case KEY_KEY_A:
                             {
                                  core::vector3df v = node->getPosition();
                                  while(event.KeyInput.PressedDown)
                                  {
                                  v.X += event.KeyInput.Key == KEY_KEY_D ? 2.0f : -2.0f;
                                  node->setPosition(v);
                                  }
                }
                }
                return true;
       }
       }
       return false;
       }
};
When compiled, the object supposed to move when the keys are depressed doesn't move. Everything else works perfectly. Any errors in my code?
olivehehe_03
Posts: 157
Joined: Tue Mar 20, 2007 8:30 am

Post by olivehehe_03 »

Make sure you're setting the receiver either when you create the device, like this...

Code: Select all

MyEventReceiver receiver;
IrrlichtDevice *device = createDevice(EDT_DIRECT3D9, core::dimension2d<s32>(640, 480), 32, false, false, false, &receiver);
...or like this

Code: Select all

MyEventReceiver receiver;
device->setEventReceiver(&receiver);
If that doesn't work, try this, it works better and it's much easier to use...

Code: Select all

class MyEventReceiver : public IEventReceiver
{
	public:
		bool OnEvent(SEvent event)
		{
			if (event.EventType == EET_KEY_INPUT_EVENT)
			{
				Keys[event.KeyInput.Key] = event.KeyInput.PressedDown;
				return true;
			}
			return false;
		}
		MyEventReceiver()
		{
			for ( s32 i=0; i<256; i++ )
			{
				Keys[i] = false;
			}
		}
		bool Keys[KEY_KEY_CODES_COUNT];
};
To use that, just set the receiver like you normally would, then to read a certain key use the following code (look in the documentation for the exact keycodes)

Code: Select all

if (receiver.Keys[KEY_KEY_W])
	{
		doWhatever();
	}
Tell me what you cherish most. Give me the pleasure of taking it away.
Perceval
Posts: 158
Joined: Tue May 30, 2006 2:42 pm

Post by Perceval »

I think you should replace your switch instruction by

Code: Select all

switch(event.KeyInput.Key)
                {
                case KEY_KEY_W:
                case KEY_KEY_S:
                                  core::vector3df v = node->getPosition();
                                  v.Y += event.KeyInput.Key == KEY_KEY_W ? 2.0f : -2.0f;
                                  node->setPosition(v);
                            return true;
                 case KEY_KEY_D:
                 case KEY_KEY_A:
                                  core::vector3df v = node->getPosition();
                                  v.X += event.KeyInput.Key == KEY_KEY_D ? 2.0f : -2.0f;
                                  node->setPosition(v);

                            return true; 
}
The while instruction in your switch is useless (onEvent() is called in your game loop) and i think it will "freeze" your app because while the key is pressed, nothing else is done, so irrlicht can't render your scene.
Lokzor
Posts: 4
Joined: Tue Apr 17, 2007 12:03 am
Contact:

Post by Lokzor »

I changed the class to the one olive suggested, and I can't get it to move. Would I put

Code: Select all

if(receiver.Keys[KEY_KEY_W])
        {
           v.Y = v.Y + 2.0f;
        }
		
in the game loop or the class OnEvent()?
Perceval
Posts: 158
Joined: Tue May 30, 2006 2:42 pm

Post by Perceval »

I think olivehehe_03 was suggesting to put it in the game loop.
Lokzor
Posts: 4
Joined: Tue Apr 17, 2007 12:03 am
Contact:

Post by Lokzor »

The completly revamped class doesn't work, I tried the game loop, the class, and in the middle of the program. I tried Perceval's switch statement, and it works. The only thing that I need to do now is figure out how to get them to continue to move while the key is down. Thanks.

EDIT: Fixed the problem, all I had to do was take out a !
Post Reply