Player movement with Animations

greenwarden 2_12_2015

During this week I have implemented multiple things, this post will contain information of the a 8-directional movement system.

Some syntax information:

A vector is a container for two variables of a set type, in this case it holds two float values since its declared like this. sf::Vector2f playerAcceleration. sf is the namespace name which contains the possibility to declare a vector.

A Enum is a identifier which has a certain integral value( a whole number without decimals). The Enum itself has certain identifiers declared inside of it, and in this case our Enum has these identifiers. Action_up, Action_down, Action_left, Action_right and Action_count.

Enums first identifier get the value 0, the second 1 and the third 2

Explanation:

m_actions is a array which contains the amount of values as the size of Action_count. Since Action_count is the fifth element it holds the value 4, which is the number of the different inputs.

I then check the bool m_actions in a function called OnAction which checks if a certain key is pressed.

And if a key is pressed it sets the player velocity to a corresponding direction.

the velocity is applied in the movement functions, by calculating the players acceleration times 0.5 times the passed time elevated by two. + the players velocity  times time+ the players current position.

this is a standard rule of physics.

The animation is then applied depending on which direction the velocity is corresponding to. So if the velocity is higher then 0 horizontally and the velocity is higher then 0 vertically the animation will animate with the character facing south east.

The animation code is taken directly from the internet. Though i got help to alter it at certain places. The alteration itself taught me many things but the most substantial that i know of, is how to use std::map. a map is like a vector, except that the first value is its identifier, and in this case its second is a pointer to the animation class. So the code

std::pair< m_animations.insert(std::make_pair(“walkingEast_Anim”, walkingAnimationEast));

inserts a pointer to animation called walkingAnimationEast with the identifier walkingEast_Anim into the m_animations map.

then at the velocity checking code i search through the map for certain identifiers with the names that i declared and set the animation to its pointer of its identifier, the pointer value is the second, that is why it points to its second, value.

m_animatedSprite->setAnimation(*m_animations.find(“walkingSouthEast_Anim”)->second);

I added movement since it is one of the most important parts of the game, atleast for the prealpha which is 2/13/2015.

I made animation this way because on of our teachers Jerry told me it was bad to directly check if buttons were pressed to set the animation. It is better to check the velocity of the player since it is a direction.

About Gabriel Ajuwa

2014  Programming