Я пытаюсь создать игрока, который прыгает и двигается влево и вправо.
В данный момент игрок не двигается в стойле, и я считаю, что существует проблема с установкой состояния enum на true нажатие клавиши, но я не уверен.
Вот функции обновления плеера, нажатия клавиш и отпускания клавиш:
void Character::update()
{
b2Vec2 pos = m_body->GetPosition();
setPosition(pos.x, pos.y);
float angle = m_body->GetAngle()* RAD2DEG;
setRotation(angle);
b2Vec2 vel = m_body->GetLinearVelocity();
float velChange = mySpeed - vel.x;
float impulse = m_body->GetMass() * velChange;
m_body->ApplyLinearImpulseToCenter(b2Vec2(impulse, 0.f), true);
if (m_movingStates[LEFT])
{
mySpeed = -2.0f;
}
if (m_movingStates[RIGHT])
{
mySpeed = 2.0f;
}
if (m_movingStates[JUMP] && m_sit)
{
m_body->ApplyLinearImpulseToCenter(b2Vec2(0.f, -10.f), true);
}
if (!m_movingStates[LEFT] && !m_movingStates[RIGHT])
{
mySpeed = 0.f;
}
}
void Character::onKeyPress(sf::Event event)
{
if (event.key.code == sf::Keyboard::A)m_movingStates[LEFT] = true;
if (event.key.code == sf::Keyboard::D)m_movingStates[RIGHT] = true;
if (event.key.code == sf::Keyboard::Space)m_movingStates[JUMP] = true;
}
void Character::onKeyRelease(sf::Event event)
{
if (event.key.code == sf::Keyboard::A) m_movingStates[LEFT] = false;
if (event.key.code == sf::Keyboard::D) m_movingStates[RIGHT] = false;
if (event.key.code == sf::Keyboard::Space) m_movingStates[JUMP] = false;
}
Класс символов:
class Character: public sf::RectangleShape, public PhysicalThing
{
public:
Character() {};
Character(b2World * world, const sf::Vector2f & position, const sf::Vector2f & size, const float orientation, sf::Texture * texture);
void update(); //!< Update rendering infomation
void onKeyPress(sf::Event event); //!< Apply impulse to the body
void onKeyRelease(sf::Event event);
void sitOn(b2Fixture * other);
void sitOff(b2Fixture * other);
bool m_sit = true;
enum movingStates { LEFT, RIGHT, JUMP };
float mySpeed = 0.f;
bool m_movingStates[3]{ 0,0,0 };
};
Спасибо,
Иордания