Kotov72rus Posted December 11, 2012 Share Posted December 11, 2012 Hi. Do not understand why this code does not work?. This is for the test 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? Quote Link to comment Share on other sites More sharing options...
Daimour Posted December 11, 2012 Share Posted December 11, 2012 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. 1 Quote Link to comment Share on other sites More sharing options...
DaDonik Posted December 11, 2012 Share Posted December 11, 2012 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; } 1 Quote (Win7 64bit) && (i7 3770K @ 3,5ghz) && (16gb DDR3 @ 1600mhz) && (Geforce660TI) Link to comment Share on other sites More sharing options...
Kotov72rus Posted December 11, 2012 Author Share Posted December 11, 2012 Thank you so much, work. Quote Link to comment Share on other sites More sharing options...
Canardia Posted December 11, 2012 Share Posted December 11, 2012 Or even shorter: sequence=1+KeyDown(KEY_W) or KeyDown(KEY_A) or KeyDown(KEY_S) or KeyDown(KEY_D); 2 Quote ■ Ryzen 9 ■ RX 6800M ■ 16GB ■ XF8 ■ Windows 11 ■ ■ Ultra ■ LE 2.5 ■ 3DWS 5.6 ■ Reaper ■ C/C++ ■ C# ■ Fortran 2008 ■ Story ■ ■ Homepage: https://canardia.com ■ Link to comment Share on other sites More sharing options...
Kotov72rus Posted December 11, 2012 Author Share Posted December 11, 2012 Test Animation: http://youtu.be/fPZJuwmyNPI Quote Link to comment Share on other sites More sharing options...
Guest Red Ocktober Posted December 11, 2012 Share Posted December 11, 2012 just curious... which code snippet did you decide on using... --Mike Quote Link to comment Share on other sites More sharing options...
Kotov72rus Posted December 12, 2012 Author Share Posted December 12, 2012 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; } Quote Link to comment Share on other sites More sharing options...
Kotov72rus Posted December 12, 2012 Author Share Posted December 12, 2012 Do not judge me harshly , this is my first experience with C++ Quote Link to comment Share on other sites More sharing options...
Guest Red Ocktober Posted December 12, 2012 Share Posted December 12, 2012 no judgement coming from this end of the code monkey universe --Mike Quote Link to comment Share on other sites More sharing options...
DaDonik Posted December 12, 2012 Share Posted December 12, 2012 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 1 Quote (Win7 64bit) && (i7 3770K @ 3,5ghz) && (16gb DDR3 @ 1600mhz) && (Geforce660TI) Link to comment Share on other sites More sharing options...
Clackdor Posted December 12, 2012 Share Posted December 12, 2012 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. Quote Link to comment Share on other sites More sharing options...
Kotov72rus Posted December 12, 2012 Author Share Posted December 12, 2012 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 Thank you, will use it Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.