Irrlicht Event Receiver

Started by
3 comments, last by Mr_Fhqwhgads 18 years ago
Sigh I have this code, but my keys aren't doing anything...

#include <iostream>
#include <irrlicht.h>

using namespace std;
using namespace irr;
using namespace video;
using namespace core;
using namespace io;
using namespace scene;

#pragma comment(lib, "Irrlicht.lib")

int ballX = 25;
int	ballY = 25;
int barX = 22;
int barY = 48;
ISceneNode *node = 0;

class MyEventReceiver : public IEventReceiver
{
public:
	virtual bool OnEvent(SEvent event)
	{
		/*
		If the key 'W' or 'S' was left up, we get the position of the scene node,
		and modify the Y coordinate a little bit. So if you press 'W', the node
		moves up, and if you press 'S' it moves down.
		*/

		if (node != 0 && event.EventType == irr::EET_KEY_INPUT_EVENT&&
			!event.KeyInput.PressedDown)
		{
			switch(event.KeyInput.Key)
			{
			case KEY_RIGHT:
				{
					barX = barX + 1;
				}
			case KEY_LEFT:
				{
					barX = barX - 1;
				}
				return true;
			}
		}

		return false;
	}
};


int main()
{

	MyEventReceiver events;

	// Setup the device
	IrrlichtDevice *device = createDevice(EDT_OPENGL, dimension2d<s32>(50,50),8,false,false,false, &events);
	// Get the video Driver
	IVideoDriver *driver = device->getVideoDriver();
	// Window Caption
	device->setWindowCaption(L"Break Da Pixels");
	// Get images
	ITexture *ball = driver->getTexture("./graphics/ball.bmp");
	ITexture *bar = driver->getTexture("./graphics/bar.bmp");

	// Variables
	bool keys[2]; // For the keys



	// Game Loop
	while(device->run() && driver)
	{
		if(device->isWindowActive())
		{
			driver->beginScene(true,true,SColor(0,0,0,0));
				driver->draw2DImage(ball,position2d<s32>(ballX,ballY),rect<s32>(0,0,1,1));
				driver->draw2DImage(bar,position2d<s32>(barX,barY),rect<s32>(0,0,5,1));		
			driver->endScene();

		}
	}
	device->drop();
	return 0;
}


Blupix Games
Advertisement
Well, with your setup, you have that global variable "node", right? And in your event message deal, one of your conditions is "if(node != 0)", and by looking at the code you've provided, node is never used anywhere else or set or anything, so node is zero when it comes around to your if statement, which is probably the problem.
Ok, that fixed it. Thanks.

I have another couple of problems tho. When I press the right key, the bar doesn't move at all. Also, any Irrlicht Gurus know how to make it so you can hold down the key and it'll keep moving? Thanks!
Blupix Games
it is moving, its just that it runs into the next case and moves back. You need another return :)
Aha, ok. Thanks :P I guess I'm not as good at c++ as I first tought :P

EDIT: I fixed my being able to hold the key down. Thanks!

[Edited by - Mr_Fhqwhgads on March 29, 2006 12:11:21 AM]
Blupix Games

This topic is closed to new replies.

Advertisement