Safixk Safixk - 11 months ago 68
C++ Question

Achieving diagonal movement when 2 arrow keys are pressed (with SFML)

I do not think any code demonstrations will be necessary because it's not much SFML as it is C++. The question is, how would I achieve smooth movement with something that I'm blitting on the screen? For example, I blit a square to the screen, and I make it's position variables as integers that I've defined before so that I can move it with my arrow keys. When I press the keys that add values to the variables it works fine if I use only one key, but upon pressing two keys, it only adds to the value that was pressed second. How would I make it so it moves diagonally?

Answer Source


if (key.up())
    if (key.right())
        direction = up_right;
    else if (key.left())
        direction = up_left;
        direction = up;
else if (key.down())