Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: KeyBoard Problem...  (Read 2063 times)

0 Members and 1 Guest are viewing this topic.

CytraL

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
    • GitHub
KeyBoard Problem...
« on: August 22, 2010, 04:16:31 am »
Hi... i have this code:

Code: [Select]

...

            /************************
                EVENTOS DE TECLADO
             ************************/
            //TECLA PRESIONADA
            if (screen.GetInput().IsKeyDown(sf::Key::P) && key_pressed == false)
            {
                world.paused = world.paused?false:true;
            }
            if (last_event.Type == sf::Event::KeyPressed && key_pressed == false)
            {
                CPerson::STATE *char_state = &world.person->state;
                if(screen.GetInput().IsKeyDown(sf::Key::Space) && char_state->character != STATE_DOUBLEJUMP)
                {
                    world.person->body->SetLinearVelocity(b2Vec2(world.person->body->GetLinearVelocity().x, 0));

                    if (char_state->character == STATE_GROUND)
                    {
                        char_state->character = STATE_JUMP;
                        world.person->body->ApplyImpulse(b2Vec2(0,-50), world.person->pos);
                    }
                    else if (char_state->character == STATE_JUMP)
                    {
                        char_state->character = STATE_DOUBLEJUMP;
                        world.person->body->ApplyImpulse(b2Vec2(0,-30), world.person->pos);
                    }
                    //playSound(SOUND_JUMP);
                }

                if((screen.GetInput().IsKeyDown(sf::Key::A) && world.person->body->GetLinearVelocity().x > 0.0f) || (screen.GetInput().IsKeyDown(sf::Key::D) && world.person->body->GetLinearVelocity().x < 0.0f))
                    world.person->body->SetLinearVelocity(b2Vec2(world.person->body->GetLinearVelocity().x/2, world.person->body->GetLinearVelocity().y));

                key_pressed = true;
            }
            //TECLA SOLTADA
            if (last_event.Type == sf::Event::KeyReleased && key_pressed == true)
                key_pressed = false;
        }

        //Raton
        sf::Vector2f mousepos = screen.ConvertCoords(screen.GetInput().GetMouseX(), screen.GetInput().GetMouseY());
        mouse.x = mousepos.x;
        mouse.y = mousepos.y;
        resources.sprt_cursor.SetPosition(mouse.x, mouse.y);

        //Movimientos del Personaje (TODO: Se tiene que mejorar la jugabilidad!)
        if(screen.GetInput().IsKeyDown(sf::Key::A))
        {
            CPerson::STATE *char_state = &world.person->state;
            //Velocidad inicial Positiva
            if (world.person->body->GetLinearVelocity().x > -1.0f)
            {
                if (char_state->character == STATE_GROUND )
                    world.person->body->ApplyForce(b2Vec2(-70.0f, 0.0f), world.person->pos);
                else if (world.person->body->GetLinearVelocity().x > -6.0f)
                    world.person->body->ApplyForce(b2Vec2(-30.0f, 0.0f), world.person->pos);
            }
            else
            {
                //Impulso
                if (world.person->state.character == STATE_GROUND)
                    world.person->body->ApplyForce(b2Vec2(-150.0f, 0.0f), world.person->pos);
                else
                    world.person->body->ApplyForce(b2Vec2(-10.0f, 0.0f), world.person->pos);
            }
        }
        if(screen.GetInput().IsKeyDown(sf::Key::D))
        {
            CPerson::STATE *char_state = &world.person->state;

            //Velocidad inicial Positiva
            if (world.person->body->GetLinearVelocity().x < 1.0f)
            {
                if (char_state->character == STATE_GROUND)
                    world.person->body->ApplyForce(b2Vec2(70.0f, 0.0f), world.person->pos);
                else if (world.person->body->GetLinearVelocity().x < 6.0f)
                    world.person->body->ApplyForce(b2Vec2(30.0f, 0.0f), world.person->pos);
            }
            else
            {
                //Impulso
                if (world.person->state.character == STATE_GROUND)
                    world.person->body->ApplyForce(b2Vec2(150.0f, 0.0f), world.person->pos);
                else
                    world.person->body->ApplyForce(b2Vec2(10.0f, 0.0f), world.person->pos);
            }
        }

...


When holding a key down and press another ... need to give two times the key to respond ...


Try It: http://filebeam.com/1dc9cc3d18b390b50625d2f897b1ef11

You can't run and jump... (need press jump two times :S)
dev@redneboa.es | WordPress | GitHub | YouTube

Mindiell

  • Hero Member
  • *****
  • Posts: 1261
    • ICQ Messenger - 41484135
    • View Profile
KeyBoard Problem...
« Reply #1 on: August 23, 2010, 09:17:19 am »
I didn't really try to read your code (very hard to read it I think), but if I were you I'll try to use some states for the player :
- Running
- Jumping
- Running & Jumping

And change the state with the GetEvent and not with the GetInput ;)
Mindiell
----

CytraL

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
    • GitHub
KeyBoard Problem...
« Reply #2 on: August 23, 2010, 04:13:00 pm »
Sry for my "dirty" code :\... but im not proffesional programmer...

Ok i fixed my code :).. the problem was this var "key_pressed"...




Sry for my bad english ;)
dev@redneboa.es | WordPress | GitHub | YouTube

 

anything