LoginLogin

Need help with directional sprite movement/animation

Root / Programming Questions / [.]

thatguyCreated:
This is a little bit complicated to explain but it comes down to if I for example hold right on the dpad and then press or hold up on the dpad the the sprite changes rapidly from up and down what I want it to do is that the sprite walks to the right with the animation of walking right and when I press or hold up it still uses the animation of walking to the right only it also goes up. Where the problem is in this, is I also want to be able to do the same for up and then pressing or holding right but then using the animation of walking up. I also want it to work with the joystick and a combination of the dpad and joystick.(I have vsync 2 if that makes a diffrence.)

I think the way I would implement this is to have two variables, one for the direction on the frame previous to the current one (say PREVDIR%), one for the direction on the current frame (say CURRDIR%). For initialization, set CURRDIR% to zero, indicating the sprite is stationary. In the superloop, have two lines like
PREVDIR%=CURRDIR%
CURRDIR%=BUTTON() AND (#LEFT OR #RIGHT OR #UP OR #DOWN)
Now, if CURRDIR% is one of the four values #LEFT, #RIGHT, #UP, or #DOWN, then your sprite is moving in a cardinal direction and the sprite's appearance needs to correspond to that. If CURRDIR% is nonzero, but not a cardinal direction, then it is a diagonal direction. There are two cases to consider when the sprite is moving diagonally. First, if PREVDIR% is nonzero, then you are in a situation like, the user has been holding 'down', then just started holding 'right' at the same time. In this case, do not change the appearance of the sprite. Second, if PREVDIR% is zero, then the user went from stationary directly to moving diagonally. You have to choose which cardinal direction you'd prefer for the appearance of your sprite. I think I would choose left/right over up/down, since the screen is wider than it is tall, but it is your choice.

Could you give a example code because I don't understand it anymore by the currdir% is non zero or an explaination so a dummy like I can understand it :p

This code is not tested.
CURRDIR%=0
ANIMATIONDIR%=0
WHILE #TRUE
 VSYNC
 PREVDIR%=CURRDIR%
 CURRDIR%=BUTTON() AND (#LEFT OR #RIGHT OR #UP OR #DOWN)
 IF CURRDIR%==0 THEN
  ' Stop the animation
  ANIMATIONDIR%=0
 ELSEIF (CURRDIR%==#LEFT) || (PREVDIR%==0 && (CURRDIR% AND #LEFT)) THEN
  IF ANIMATIONDIR%!=#LEFT THEN
   ' Start left-facing animation
  ENDIF
  ANIMATIONDIR%=#LEFT
 ELSEIF (CURRDIR%==#RIGHT) || (PREVDIR%==0 && (CURRDIR% AND #RIGHT)) THEN
  ' ... right
 ELSEIF (CURRDIR%==#UP) || (PREVDIR%==0 && (CURRDIR% AND #UP)) THEN
  ' ... up
 ELSEIF (CURRDIR%==#DOWN) || (PREVDIR%==0 && (CURRDIR% AND #DOWN)) THEN
  ' ... down
 ENDIF
 ' Everything else in your superloop
WEND

Alright thanks for your help!

Actually, the last two guards can be
 ELSEIF CURRDIR%==#UP THEN
  ' ... up
 ELSEIF CURRDIR%==#DOWN THEN
  ' ... down
since the stationary-to-diagonal cases are covered by the previous guards.

I know i am kinda asking much but I have been playing around with the code and I can't figure out how to use it. If you have time or someone els I would highly appreciate it if you post the full code or a key. Again sorry for being so big of a noob 🙃

'How to use it' is, type it in, and fill in details I have left out. Can you be more specific about what you can't figure out?

For example i want to keep the animation status stored I have that done wit a variable and the spchr 0,old,0,0,0 old is the variable I use to declare the state where the animation has to stop if press one of the cardinal directions. But the thing I don't understand is if I press for example up while holding left the the sprite simply stops and doesn't go up even if don't use the variable and just do what you have explained. So I probaly am typing something wrong that is why I am asking for a full example code so that I just simply can swap the sprites out with mine and use the code without adding additional code because this stuff is past my understanding.

I am asking for a full example code so that I just simply can swap the sprites out with mine and use the code without adding additional code because this stuff is past my understanding.
That's unlikely to happen. No-one will make the effort of writing your game, so you can call it yours after they have done the work. And if it remains past your understanding, you will not be able to debug, tweak, or add to the game, without bugging someone again. So, the best approach is to extend your understanding. From the symptoms you describe, I imagine some of your code is like
IF BUTTON()==#UP THEN
 ' Move up
ENDIF
If, instead, you use
IF BUTTON()&&#UP THEN
 ' Move up
ENDIF
then diagonal motion should work.

That did the trick thanks!

IF BUTTON()&&#UP THEN
 ' Move up
ENDIF
You want BUTTON() AND #UP, not BUTTON() && #UP. The latter will be true if you press any button, not just #UP, because && is boolean AND rather than bitwise AND, and only checks that both sides aren't 0. In other words, these are all equivalent:
BUTTON() && #UP
(BUTTON() != 0) && (#UP != 0)
(BUTTON() != 0) && (1 != 0)
(BUTTON() != 0) && TRUE
BUTTON() != 0
BUTTON()

Thank you niconii, you're right that they are all equivalent, when used as a guard for an IF. I apologize for posting code I hadn't tested, or thought through enough.