Page 1 of 1

Controls don't work properly.

Posted: Sun Jun 11, 2006 7:38 am
by scarface
My code:

Code: Select all

 if (node != 0 && event.EventType == irr::EET_KEY_INPUT_EVENT&&
                !event.KeyInput.PressedDown)
        {
            switch(event.KeyInput.Key)
            {
                case KEY_KEY_S:
                {
                    core::vector3df v = cow->getPosition();
                    v.Y -=2.0f;
                    cow->setPosition(v);
                }
                case KEY_KEY_Z:
                {
                    core::vector3df v = cow->getPosition();
                    v.Y +=2.0f;
                    cow->setPosition(v);
                }
                case KEY_KEY_Q:
                {
                    core::vector3df v = cow->getPosition();
                    v.X -=2.0f;
                    cow->setPosition(v);
                }
                case KEY_KEY_D:
                {
                    core::vector3df v = cow->getPosition();
                    v.X +=2.0f;
                    cow->setPosition(v);
                }
                //return true;
            }
        }
Negative movements don't work (left/down), why?


Thanks.

Posted: Sun Jun 11, 2006 9:09 am
by vitek
Because you forgot to break out of the case blocks. If you press S, cases S, Z, Q and D would be executed and nothing would happen. If you just press Z then Z, Q and D would be executed and the object would be moved up.

Travis

Posted: Sun Jun 11, 2006 9:10 am
by cadue
excusme, you know how the switch work? you've forgot the "break" at the end of the "case". this is corret:

Code: Select all

 if (node != 0 && event.EventType == irr::EET_KEY_INPUT_EVENT&&
                !event.KeyInput.PressedDown)
        {
            switch(event.KeyInput.Key)
            {
                case KEY_KEY_S:
                {
                    core::vector3df v = cow->getPosition();
                    v.Y -=2.0f;
                    cow->setPosition(v);
                     break;  
                }
                case KEY_KEY_Z:
                {
                    core::vector3df v = cow->getPosition();
                    v.Y +=2.0f;
                    cow->setPosition(v);
                    break;  
                }
                case KEY_KEY_Q:
                {
                    core::vector3df v = cow->getPosition();
                    v.X -=2.0f;
                    cow->setPosition(v);
                    break;  
                }
                case KEY_KEY_D:
                {
                    core::vector3df v = cow->getPosition();
                    v.X +=2.0f;
                    cow->setPosition(v);
               break;                
               }
                //return true;
            }
        }

Posted: Sun Jun 11, 2006 9:12 am
by cadue
:D we've posted the message in the same moment!

Posted: Sun Jun 11, 2006 9:29 am
by Baal Cadar
The code is also very repetitive. Better style:

Code: Select all

    core::vector3df delta();

    if (event.KeyInput.Key == KEY_KEY_S)
        delta.Y = -2.0f;
    else if (event.KeyInput.Key == KEY_KEY_Z)
        delta.Y =  2.0f;
    else if (event.KeyInput.Key == KEY_KEY_Q)
        delta.X = -2.0f;
    else if (event.KeyInput.Key == KEY_KEY_D)
        delta.X =  2.0f;

    cow->setPosition(cow->getPosition() + delta);

Posted: Sun Jun 11, 2006 9:36 am
by scarface
I understand, thanks ;)

Posted: Sun Jun 11, 2006 9:42 am
by vitek
The code is also very repetitive. Better style:...
Well, except yours wouldn't compile [core::vector3df delta() would seen as a function declaration].

Posted: Sun Jun 11, 2006 10:12 am
by Baal Cadar
Yes. make it core::vector3df delta; then.