Jump to content

Animation


Kotov72rus
 Share

Recommended Posts

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?

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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;
       }

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 Share

×
×
  • Create New...