Simple event receiver questions

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
tommitytom
Posts: 1
Joined: Sun Oct 09, 2005 1:45 pm
Location: Norwich/Huddersfield, England
Contact:

Simple event receiver questions

Post by tommitytom »

Hey there. I've been learning C++ now for about a month now, so my skills are pretty limited, but im learning more every day!

I'd just like to ask some simple questions about the event receiver, and how it works.

I was looking at the "movement" tutorial, and I created a simple version of it, with just a single TestSceneNode. I wanted to see how I could move the block around with the keyboard. In the tutorial there is this code:

Code: Select all

class MyEventReceiver : public IEventReceiver
{
public:
	virtual bool OnEvent(SEvent event)
	{
		if (node != 0 && event.EventType == irr::EET_KEY_INPUT_EVENT &&
				!event.KeyInput.PressedDown)
	{
			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;
			}
	}

	return false;
	}
};
Could someone possibly explain how this works? In particular, this line: v.Y += event.KeyInput.Key == KEY_KEY_W ? 2.0f : 2.0f;
I just can't work out how that would move the block? Most C++ seems quite logical, where to me this just seems quite confusing! Just another bit of syntax im yet to learn i guess, heh.

I'd also like to be able to move the block around when a key is held down, or the left/right with the A and D keys. I played around with a few functions, like "event.KeyInput.PressedDown" or something similar but had no luck.

Thanks in advance!
specialtactics
Posts: 17
Joined: Tue Dec 09, 2003 9:03 am
Location: Germany

Post by specialtactics »

Hi,

Code: Select all

v.Y += event.KeyInput.Key == KEY_KEY_W ? 2.0f : -2.0f
(you missed a minus in your text, whereas you have it in the code snippet)
is essentially the same as

Code: Select all

if (event.KeyInput.Key == KEY_KEY_W) {
    v.Y += 2.0;
} else {
    v.Y += -2.0;
}
since this gets executed when KeyInput.Key is either KEY_KEY_W or KEY_KEY_S, it increases v.Y when W was pressed and decreases v.Y when S was pressed.

Doing stuff when a key is held down is a little more complex since the eventreceiver is only called when a key is pressed or released. So you will probably need a boolean variable you set to true when a key is pressed and to false when a key is released. Then somewhere in the main-loop you query that variable and know if the key is being held down.
Post Reply