You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers. No questions about C++ programming or topics which are answered in the tutorials!
I'm setting up a simple flashlight for my project, and I have it set to turn on the flashlight whenever F is pressed (HL2 Style). The problem is that input is handled way too fast and that the light flickers extremely fast.
class MyEventReceiver : public IEventReceiver
{
public:
// This is the one method that we have to implement
virtual bool OnEvent(const SEvent& event)
{
// Remember whether each key is down or up
if (event.EventType == irr::EET_KEY_INPUT_EVENT)
KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
return false;
}
// This is used to check whether a key is being held down
virtual bool IsKeyDown(EKEY_CODE keyCode) const
{
return KeyIsDown[keyCode];
}
MyEventReceiver()
{
for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
KeyIsDown[i] = false;
}
private:
// We use this array to store the current state of each key
bool KeyIsDown[KEY_KEY_CODES_COUNT];
};
int LightSwitch = 0; //1 is ON and 0 is OFF
(This is in while (device->run().. part)
if(receiver.IsKeyDown(KEY_KEY_F))
{
if (LightSwitch == 0)
{
FlashLight->setVisible(true);
LightSwitch = 1;
}
else
{
FlashLight->setVisible(false);
LightSwitch = 0;
}
}
My desired goal is to be able to hold the key down and it turns on, once I release and press it again, it will turn off.
int LightSwitch = 0; //1 is ON and 0 is OFF
bool LastState = false;
(This is in while (device->run().. part)
bool State = receiver.IsKeyDown(KEY_KEY_F);
if (State != LastState && State)
{
if (LightSwitch == 0)
{
FlashLight->setVisible(true);
LightSwitch = 1;
}
else
{
FlashLight->setVisible(false);
LightSwitch = 0;
}
LastState = State;
}
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
if(receiver.IsKeyDown(KEY_KEY_F))
light on;
else
light off
no that will turn the light off when u let go of the button.
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
Thanks Sudi, but using that code makes the light turn on the way I want, but it's impossible to turn it off. I'll experiment laater when I'm back from school.
JP wrote:Good lord isn't this a really simple problem?
You just toggle the light on/off when the key is pressed....
Yes i know, but the input is very quick. It turns the light on and off rapidly. I'm going to mess around now, as school is out. I'll post with results soon.
wtf
Thats exactly the same code i posted ealier.....
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.