Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Player Animation on a keypress

This thread is locked; no one can reply to it. rss feed Print
 1   2 
Player Animation on a keypress
Dizzy Egg
Member #10,824
March 2009
avatar

I imagine there's some confusery in your event keydown if/else type code! If you like you can post a whole bunch of code here, it seems we're getting on ok! I can do no more tonight as I am inebriated, but tomorrow I will be able to quest on with you, we'll figure it out bro 8-)

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

Lasaqus7
Member #15,950
May 2015

My key events are as follows:-

#SelectExpand
1ALLEGRO_EVENT ev; 2 al_wait_for_event(event_queue, &ev); 3 4 if (ev.type == ALLEGRO_EVENT_TIMER) 5 { 6 render = true; 7 8 //UPDATE=========================================== 9 if (keys[UP] || keys[W]) 10 MoveHumanUp(Human); 11 else if (keys[DOWN] || keys[S]) 12 MoveHumanDown(Human); 13 else if (keys[LEFT] || keys[A]) 14 MoveHumanLeft(Human); 15 else if (keys[RIGHT] || keys[D]) 16 MoveHumanRight(Human); 17 else if (keys[SPACE]) 18 { 19 20 } 21if (state == MENU) 22 { 23 } 24 else if (state == PLAYING) 25 { 26 } 27 else if (state == COMBATSCREEN) 28 { 29 } 30 else if (state == COMBATIDLE) 31 { 32 if (threshold > CheckDistance(Human.x, Human.y, Orc.x, Orc.y)) 33 { 34 ChangeState(state, COMBATCHASING); 35 } 36 37 if (keys[KEY_1]) 38 { 39 Human.Attacking = true; 40 41 } 42 43 if (Human.Attacking) 44 PlayerAttack(Human); 45 46 } 47else if (ev.type == ALLEGRO_EVENT_KEY_DOWN) 48 { 49 switch (ev.keyboard.keycode) 50 { 51 case ALLEGRO_KEY_ESCAPE: 52 keys[ESCAPE] = true; 53 break; 54 case ALLEGRO_KEY_SPACE: 55 if (canPressKey = true) 56 { 57 keys[SPACE] = true; 58 canPressKey = false; 59 60 keyPressed = !keyPressed; 61 } 62 break; 63 case ALLEGRO_KEY_UP: 64 keys[UP] = true; 65 break; 66 case ALLEGRO_KEY_DOWN: 67 keys[DOWN] = true; 68 break; 69 case ALLEGRO_KEY_LEFT: 70 keys[LEFT] = true; 71 break; 72 case ALLEGRO_KEY_RIGHT: 73 keys[RIGHT] = true; 74 break; 75 case ALLEGRO_KEY_W: 76 keys[W] = true; 77 break; 78 case ALLEGRO_KEY_S: 79 keys[S] = true; 80 break; 81 case ALLEGRO_KEY_A: 82 keys[A] = true; 83 break; 84 case ALLEGRO_KEY_D: 85 keys[D] = true; 86 break; 87 case ALLEGRO_KEY_C: 88 if (canPressKeyC = true) 89 { 90 keys[C] = true; 91 canPressKeyC = false; 92 93 ckeyPressed = !ckeyPressed; 94 } 95 break; 96 case ALLEGRO_KEY_1: 97 keys[KEY_1] = true; 98 break; 99 } 100 } 101 else if (ev.type == ALLEGRO_EVENT_KEY_UP) 102 { 103 switch (ev.keyboard.keycode) 104 { 105 case ALLEGRO_KEY_ESCAPE: 106 done = true; 107 break; 108 case ALLEGRO_KEY_SPACE: 109 canPressKey = true; 110 keys[SPACE] = false; 111 break; 112 case ALLEGRO_KEY_UP: 113 keys[UP] = false; 114 break; 115 case ALLEGRO_KEY_DOWN: 116 keys[DOWN] = false; 117 break; 118 case ALLEGRO_KEY_LEFT: 119 keys[LEFT] = false; 120 break; 121 case ALLEGRO_KEY_RIGHT: 122 keys[RIGHT] = false; 123 break; 124 case ALLEGRO_KEY_W: 125 keys[W] = false; 126 break; 127 case ALLEGRO_KEY_S: 128 keys[S] = false; 129 break; 130 case ALLEGRO_KEY_A: 131 keys[A] = false; 132 break; 133 case ALLEGRO_KEY_D: 134 keys[D] = false; 135 break; 136 case ALLEGRO_KEY_C: 137 keys[C] = false; 138 break; 139 case ALLEGRO_KEY_1: 140 keys[KEY_1] = false; 141 break; 142 } 143 }

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Lasaqus7 said:

#SelectExpand
1 case ALLEGRO_KEY_SPACE: 2 if (canPressKey = true) { 3 keys[SPACE] = true; 4 canPressKey = false; 5 keyPressed = !keyPressed; 6 } 7 break; 8//... 9 case ALLEGRO_KEY_C: 10 if (canPressKeyC = true) { 11 keys[C] = true; 12 canPressKeyC = false; 13 ckeyPressed = !ckeyPressed; 14 } 15 break;

You are checking against true in both cases, which will always evaluate to true.

See my response to you in an earlier thread :
https://www.allegro.cc/forums/thread/615373/1013171#target

For example, here you should have used this :
if (true == canPressKeyC) {
or this :
if (canPressKeyC) {
instead of this :
if (true = canPressKeyC) {(which would have given you an error)
or this :
if (canPressKeyC = true) {(which always performs an assignment and evaluates to true)

Lasaqus7
Member #15,950
May 2015

So a == rather than a single =

Ok i'll get that changed now. I did read that, didn't fully understand what was being discussed, I was happy that it started working and then started working on another area.

Thanks Edgar

l j
Member #10,584
January 2009
avatar

See

taron  said:

Animation::IDLE would be the correct syntax.

Also you'd have to add int animationState or something inside the Animation struct to track which animation is being played, I forgot to add that.

So you can/could do
animationState = Animation::IDLE; ...

switch (animationState)
{
   case Animation::IDLE:
        ...
   break;
   case Animation::MOVE:
        ...
   break;
}
// or
if (animationState == Animation::IDLE)
{
   ...
}

Enums are actually quite simple.
Much like structs, unions and classes you can also give them a name instead of making them anonymous.

#SelectExpand
1enum Numbers 2{ 3 ZERO, // enums start at 0 4 ONE, 5 TWO 6}; 7 8Numbers number = Numbers::ONE; 9std::cout << number << '\n'; // Will print '1' 10int i = number; // Implicit cast to integer is allowed 11std::cout << i << '\n'; 12 13enum Values 14{ 15 A = 5, // You can assign values explicitly if you so desire 16 B = 2 17}; 18 19std::cout << Values::A << ' ' << Values::B << '\n'; 20// enums aren't contained in their own scope 21std::cout << A << '\n'; // Same as Values::A 22 // Which is why I made it an anonymous enum inside of the struct. 23// C++11 adds a solution for this (Supported in VS 2013 iirc) 24enum class NewEnum 25{ 26 C, 27 D 28}; 29 30std::cout << (int)NewEnum::C << std::endl; // Have to cast explicitly to an int now 31// std::cout << NewEnum::C << std::endl; // Won't compile, can't find overloaded operator << for type of NewEnum 32// std::cout << C << std::endl; // Won't be able to find identifier 'C', scope is explicit now. 33NewEnum newEnum = NewEnum::D;

If you want to make use of enums in new code, you should probably default to using enum class instead of simply enum.

Lasaqus7
Member #15,950
May 2015

@Edgar

Edgar said:

For example, here you should have used this :
<if (true == canPressKeyC) {
or this :
if (canPressKeyC) {

This didn't work for me after I made the change, so i reverted back to the original as that is working as intended, not sure why but it is working.

@Taron
What I think I will do rather than trying to re-work my code at this stage, is that once I'm done, I'll start a fresh project only using code for the enum/structs for the animation and play about with it there, I would like to try and get that working.

 1   2 


Go to: