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

Author Topic: Moving without using diagonal movement.  (Read 3782 times)

0 Members and 1 Guest are viewing this topic.

TXRSTXN

  • Newbie
  • *
  • Posts: 6
    • View Profile
Moving without using diagonal movement.
« on: February 18, 2016, 06:28:55 pm »
Hello!  :)
So basically I'm trying to make my character move without using diagonal movement. As for the basic structure of the movement it is going good. Now the problem is control the movement.

What I want is that when you for example press down, the character moves down. If you then press right, while still pressing down, the character moves right, and if you release right, the character goes back to moving down.

I've been trying to create this with several if's in all possible ways, but it doesn't work as it's supposed to.

Any ideas on how to make it work?

ZeroZ30o

  • Newbie
  • *
  • Posts: 47
    • View Profile
Re: Moving without using diagonal movement.
« Reply #1 on: February 18, 2016, 07:27:18 pm »
Yeah, everytime you press a key make it overwrite a variable.

So for instance, when you press right, you set
direction = "right"
and then after that if you press left you overwrite with
direction = "left"

And when you release a key you can check which keys are still pressed and overwrite with one of them, or alternatively you can store the overwritten keypress on an array and release them back in order. But this is a bit overdoing it.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Moving without using diagonal movement.
« Reply #2 on: February 18, 2016, 08:11:51 pm »
Yes, as ZeroZ30o says, simply overwrite a variable on key presses. Unless you have your graphics and input strictly separated, you could also use sf::Vector2, as saves you from an extra case differentiation:

if (event.key.code == sf::Keyboard::Left)
    direction = sf::Vector2f(-1.f, 0.f);
...
// multiply with speed later
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

TXRSTXN

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Moving without using diagonal movement.
« Reply #3 on: February 18, 2016, 08:58:50 pm »
Yeah, everytime you press a key make it overwrite a variable.

So for instance, when you press right, you set
direction = "right"
and then after that if you press left you overwrite with
direction = "left"

And when you release a key you can check which keys are still pressed and overwrite with one of them, or alternatively you can store the overwritten keypress on an array and release them back in order. But this is a bit overdoing it.

This is about the same method that I have been trying to use, but instead I used bool's to hold the keys. The problem is if I were to do like this for example:

if(Key == Left)

    direction = left;

if(Key == Up)

    direction = up;

if (Key == Down)

    direction = down;

if (Key == Right)

    direction = right

 

Now if I hold right, and then press down, the direction will remain right since it's always the last statement. The keys that are on top will be able to use the directions under them (for example left can change to all directions, while right can't change at all) but not the other way around.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Moving without using diagonal movement.
« Reply #4 on: February 18, 2016, 09:15:48 pm »
If you use events, there will most likely not be two key presses in the same frame. So the one pressed later overwrites the former.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

TXRSTXN

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Moving without using diagonal movement.
« Reply #5 on: February 18, 2016, 09:40:52 pm »
I want to be able to have two keys to be pressed at the same time but to only use the later one. Using events the same problem remains. If I hold two keys at the same time, only the key with the later statement will be recognized. As I showed in my example   :(

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Moving without using diagonal movement.
« Reply #6 on: February 18, 2016, 09:44:38 pm »
No. Events don't tell you what keys are held. They tell you what keys have been pressed since you checked last. Event means state transition, not state. Important however: for this to be completely true, you have to disable key repetition.

The problem you describe only occurs when two keys are pressed in exactly the same frame. That's rather unlikely, and if the player isn't capable of better coordinating his hands, he deserves it.
« Last Edit: February 18, 2016, 09:50:32 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

ZeroZ30o

  • Newbie
  • *
  • Posts: 47
    • View Profile
Re: Moving without using diagonal movement.
« Reply #7 on: February 18, 2016, 10:40:47 pm »
You need to make a system that determines if a key is pressed or not. This is very easy:

EDIT: In case this is not clear, "rightKey" must be stored, and so must "movement"

On key press event:
rightKey = true;
On key released event:
rightKey = false;

After that, you can use the event to overwrite the data:
movement = "right";

And to ACTUALLY check movement, do:
if (movement == "right" && rightKey) { move(whatever,0); }

You also need to add a case in case the "movement" key is actually not pressed. This would happen when you press 2 keys but release the last one -you need to add code to revert "movement" to the previous key.
« Last Edit: February 18, 2016, 10:49:07 pm by ZeroZ30o »

TXRSTXN

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Moving without using diagonal movement.
« Reply #8 on: February 18, 2016, 11:53:24 pm »
You need to make a system that determines if a key is pressed or not. This is very easy:

EDIT: In case this is not clear, "rightKey" must be stored, and so must "movement"

On key press event:
rightKey = true;
On key released event:
rightKey = false;

After that, you can use the event to overwrite the data:
movement = "right";

And to ACTUALLY check movement, do:
if (movement == "right" && rightKey) { move(whatever,0); }

You also need to add a case in case the "movement" key is actually not pressed. This would happen when you press 2 keys but release the last one -you need to add code to revert "movement" to the previous key.

FINALLY!
I managed to figure it out. The events eventually worked (I had lines resetting the keys to false :-[). I used  numbers 1-4 for the directions and stored them in an array with two spaces. One to store the present direction, and one to store the "overwritten" direction, passing the values in between.

This has been bugging be for days.. what a relief.
Thank you both!  ;D

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Moving without using diagonal movement.
« Reply #9 on: February 19, 2016, 05:58:06 am »
You are deciding movement based on real-time state of your movement keys so I don't understand why you chose to use events for this.
In the events tutorial about KeyPressed and KeyReleased event, it clearly (in a big red box) shows how to do it but - more importantly - states that there is a (probably) better option for this (follow the final link that is inside that red box).
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Moving without using diagonal movement.
« Reply #10 on: February 19, 2016, 10:19:32 am »
You are deciding movement based on real-time state of your movement keys so I don't understand why you chose to use events for this.
I explained above why events are appropriate to know which key was pressed last. Real-time input can be used additionally to know which keys are held down at the moment.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything