Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

[smfl] ruch obiektu za pomocą klawiatury - ruch po skosie

Ostatnio zmodyfikowano 2017-04-05 09:37
Autor Wiadomość
aaadam
Temat założony przez niniejszego użytkownika
[smfl] ruch obiektu za pomocą klawiatury - ruch po skosie
» 2017-04-04 11:44:28
witam, gdy wciskam przycisk w prawo Player porusza się w prawo itp, ale gdy chcę żeby poruszał sie po skosie to jest problem ponieważ trzymająć przycisk w prawo dokładam w dół lub w górę to Player zaczyna poruszać w górę lub w dół a nie po skosie...
w allegro nie miałem tego problemu, gdy wcisne dwa przyciski na raz np prawo oraz góra to będzie się poruszał to skosie. Może ktoś pomóc ??? a o to kod
C/C++
g ł ó wna p ę tla gry
Window.setKeyRepeatEnabled( true );

float dt = 0.0f;
float lastUpdate = GetCurrentTime();


float accumulator = 0.0f;
const float TIME_STEP = 0.03;
const float MAX_ACCUMULATED_TIME = 1.0;

while( Window.isOpen() )
{
    dt = GetCurrentTime() - lastUpdate;
    lastUpdate += dt;
    dt = std::max( 0.0f, dt );
    accumulator += dt;
    accumulator = clamp( accumulator, 0.0f, MAX_ACCUMULATED_TIME );
    sf::Event event;
    while( Window.pollEvent( event ) )
    {
        if( event.type == sf::Event::Closed || event.key.code == sf::Keyboard::Escape )
        {
            Window.close();
        }
        ScreenManager::GetInstance().UpdateEvent( event );
       
    }
    while( accumulator > TIME_STEP )
    {
        ScreenManager::GetInstance().Update( Window );
        accumulator -= TIME_STEP;
    }
    Window.clear();
    ScreenManager::GetInstance().Draw( Window );
   
    Window.display();
   
}
funkcja updateEvent obiektu player
C/C++
void Player::UpdateEvent( sf::Event event, InputManager input )
{
    //Entity::UpdateEvent(event, input);
    //active = true;
   
    if( event.type == sf::Event::KeyPressed )
    {
        if( event.key.code == sf::Keyboard::Right )
        {
            position.x += moveSpeed;
            direction = Direction::Right;
            std::cout << "right";
        }
        if( event.key.code == sf::Keyboard::Left )
        {
            std::cout << "left";
            position.x -= moveSpeed;
            direction = Direction::Left;
        }
        if( event.key.code == sf::Keyboard::Up )
        {
            std::cout << "up";
            position.y -= moveSpeed;
            direction = Direction::Up;
        }
        if( event.key.code == sf::Keyboard::Down )
        {
            std::cout << "down";
            position.y += moveSpeed;
            direction = Direction::Down;
        }
    }
   
};
P-159768
aaadam
Temat założony przez niniejszego użytkownika
» 2017-04-05 09:37:53
siemano, rozwiązałem Problem
zamiast Event.key.code użyłem sf::Keyboard::isKeyPressed  :

C/C++
event.key.code == sf::Keyboard::Up )

C/C++
if( sf::Keyboard::isKeyPressed( sf::Keyboard::Left ) )
     position.x -= 0.3;

if( sf::Keyboard::isKeyPressed( sf::Keyboard::Right ) )
     position.x += 0.3;

if( sf::Keyboard::isKeyPressed( sf::Keyboard::Down ) )
     position.y += 0.3;

if( sf::Keyboard::isKeyPressed( sf::Keyboard::Up ) )
     position.y -= 0.3;


co Prawda nie testowałem jeszcze tego dokładnie ale jak trzymam np. przycisk w Prawo i dokładam góra lub dół to obiekt porusza się po skosie i o to mi chodziło,
P-159784
« 1 »
  Strona 1 z 1