Controls don't work properly.

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
scarface
Posts: 5
Joined: Sun Jun 11, 2006 7:34 am

Controls don't work properly.

Post 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.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post 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
cadue
Posts: 72
Joined: Mon Mar 13, 2006 8:33 pm
Location: Italy - Friuli - Monfalcone - Staranzano

Post 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;
            }
        }
excuse me for my bad english and for my ignorance...but I'm 14 and i come from Italy, where the study of english is a optional (-:
cadue
Posts: 72
Joined: Mon Mar 13, 2006 8:33 pm
Location: Italy - Friuli - Monfalcone - Staranzano

Post by cadue »

:D we've posted the message in the same moment!
excuse me for my bad english and for my ignorance...but I'm 14 and i come from Italy, where the study of english is a optional (-:
Baal Cadar
Posts: 377
Joined: Fri Oct 28, 2005 10:28 am
Contact:

Post 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);
No offense :)
scarface
Posts: 5
Joined: Sun Jun 11, 2006 7:34 am

Post by scarface »

I understand, thanks ;)
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post 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].
Baal Cadar
Posts: 377
Joined: Fri Oct 28, 2005 10:28 am
Contact:

Post by Baal Cadar »

Yes. make it core::vector3df delta; then.
No offense :)
Post Reply