Jump to content

Recommended Posts

Posted

Hi. Do not understand why this code does not work?.

 

This is for the test smile.png

if (KeyDown(KEY_W)){sequence = 2;} else sequence = 1;
if (KeyDown(KEY_S)){sequence = 2;} else sequence = 1;
if (KeyDown(KEY_A)){sequence = 2;} else sequence = 1;
if (KeyDown(KEY_D)){sequence = 2;} else sequence = 1;

 

switch (sequence) {
case 1:
 framebegin=0.0;
frameend=69.0;
break;
case 2:
framebegin=70.0;
frameend=130.0;
break;
case 3:
framebegin=131.0;
frameend=171.0;
break;
}

 

frame=AppTime()/15.0;
frame=fmodf(frame,frameend-framebegin)+framebegin;
Animate(PlayerMesh,frame,0.5,0,true);

 

Animation works only when you press 'D'.

Why is this?

Posted

Because KeyDown() means you are holding pressed key.

 

Let assume you are holding key "A".

 

After line

if (KeyDown(KEY_A)){sequence = 2;} else sequence = 1;

variable "sequence" will have value 2.

 

But after line

if (KeyDown(KEY_D)){sequence = 2;} else sequence = 1;

variable "sequence" will have value 1.

 

Because you are not holding "D" key pressed.

  • Upvote 1
Posted

You can do it that way:

if (KeyDown(KEY_W))
{
sequence = 2;
}
else if (KeyDown(KEY_A))
{
sequence = 2;
}
else if (KeyDown(KEY_S))
{
sequence = 2;
}
else if (KeyDown(KEY_D))
{
sequence = 2;
}
else
{
sequence = 1;
}

 

Or even that way (if W, A, S, D all should do the same)

 

if ((KeyDown(KEY_W) || KeyDown(KEY_A) || KeyDown(KEY_S) || KeyDown(KEY_D))
{
Sequence = 2;
}
else
{
Sequence = 1;
}

  • Upvote 1

(Win7 64bit) && (i7 3770K @ 3,5ghz) && (16gb DDR3 @ 1600mhz) && (Geforce660TI)

Posted

Or even shorter:

 

sequence=1+KeyDown(KEY_W) or KeyDown(KEY_A) or KeyDown(KEY_S) or KeyDown(KEY_D);

  • Upvote 2

Ryzen 9 RX 6800M ■ 16GB XF8 Windows 11 ■
Ultra ■ LE 2.53DWS 5.6  Reaper ■ C/C++ C# ■ Fortran 2008 ■ Story ■
■ Homepage: https://canardia.com ■

Guest Red Ocktober
Posted

just curious... which code snippet did you decide on using...

 

--Mike

Posted

I used this code:

   	 //Animation
       //Jumping
       jump = 0.0;
       isJump = false;
       if(KeyHit(KEY_SPACE))
       {
           if (!ControllerAirborne(Controller)) {
               jump=8.0;
               isJump = true;
           }
       }

       if(KeyDown(KEY_W))
       {
           sequence = 2;
           Angle = -180;
       } else if(KeyDown(KEY_S))
       {
           sequence = 2;
           Angle = -0;
       } else if(KeyDown(KEY_A))
       {
           sequence = 2;
           Angle = -90;
       } else if(KeyDown(KEY_D))
       {
           sequence = 2;
           Angle = -260;
       } else
       {
           sequence = 1;
       }

       //
       if(KeyDown(KEY_W) && KeyDown(KEY_A))
       {
           Angle = -140;
       } else if(KeyDown(KEY_W) && KeyDown(KEY_D))
       {
           Angle = 140;
       } else if(KeyDown(KEY_S) && KeyDown(KEY_A))
       {
           Angle = -40;
       } else if(KeyDown(KEY_S) && KeyDown(KEY_D))
       {
           Angle = 40;
       }

       //Run
       if(KeyDown(KEY_W) && KeyDown(KEY_LSHIFT) || KeyDown(KEY_RSHIFT))
       {
           sequence = 3;
           speed = 6;
       } else if(KeyDown(KEY_S) && KeyDown(KEY_LSHIFT) || KeyDown(KEY_RSHIFT))
       {
           sequence = 3;
           speed = 6;
       } else if(KeyDown(KEY_A) && KeyDown(KEY_LSHIFT) || KeyDown(KEY_RSHIFT))
       {
           sequence = 3;
           speed = 6;
       } else if(KeyDown(KEY_D) && KeyDown(KEY_LSHIFT) || KeyDown(KEY_RSHIFT))
       {
           sequence = 3;
           speed = 6;
       } else
       {
           speed = 4;
       }

Guest Red Ocktober
Posted

no judgement coming from this end of the code monkey universe biggrin.png

 

--Mike

Posted

Just a small tip:

Instead of multiple calls to KeyDown(KEY_A) you could save the result in a variable and use that.

bool bKeyDownA = KeyDown(KEY_A);
....
if (bKeyDownA)
{
....
}

 

That saves some time, because it only has to Call KeyDown(KEY_A) once.

Not that it gives you much performance, but things like that add up easily, as your program evolves.

 

Oh and there is no judgement involved, just a simple tip wink.png

  • Upvote 1

(Win7 64bit) && (i7 3770K @ 3,5ghz) && (16gb DDR3 @ 1600mhz) && (Geforce660TI)

Posted

Or even shorter:

 

sequence=1+KeyDown(KEY_W) or KeyDown(KEY_A) or KeyDown(KEY_S) or KeyDown(KEY_D);

 

Damnit! I have some obnoxius switch-case statement here. Love the elegancy of 1 line of code.

Posted

Just a small tip:

Instead of multiple calls to KeyDown(KEY_A) you could save the result in a variable and use that.

bool bKeyDownA = KeyDown(KEY_A);
....
if (bKeyDownA)
{
....
}

 

That saves some time, because it only has to Call KeyDown(KEY_A) once.

Not that it gives you much performance, but things like that add up easily, as your program evolves.

 

Oh and there is no judgement involved, just a simple tip wink.png

 

Thank you, will use it

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...