Is there anywhere good sources where I can go and read to find out how to start to code my game so if I click a key (eg 1) that my player with cycle through his attack animation and continue to move in his current direction and only be able to attack within a certain timeframe ?

I have tried to look at other people code in games I found where the code was available but since they code with classes, it gets me confused alot as I am still learning.

I currently have a CombatIdle state (Enemy sat idle), a Combat Chasing state (Enemy is chasing the player around) and a CombatRetreat state (Player out of range, enemy heading back to be idle). If I can put a state within a state, I'm guessing I could just put the state in the CombatChasing state as this is the one where the enemy would be in range.

I haven't posted code as I haven't began coding it. just looking for helpful resources on this before I come back and ask why it doesn't work

Ok cool! I guess basically it's going to come down to conditions every time your game loop starts again, so, did I press attack? No? Carry on then...or...yes? Is there an enemy next to me? No? just play my attack animation then....or....yes? Well, how many DP does it have?....etc etc etc

That way you can control when the frame count goes up by using the miniFrame counter, which can be a huge number - the only problem with this is that it wont run at the same speeds on different machines (probably), so not as precise as a timer, but it should slow things down enough to see the animation!

So what I believe is my code is actually performing an attack animation although it is too quick for me to see.

Shouldn't be the case, unless the whole animation happens in less than 16ms since it may end before the average screen can refresh.

while(keys[KEY_1]&& Human.curFrame != Human.maxFrame)//Does a loop as if 1 is held down{

This loop right here will do the whole attack in one frame without doing anything.

I wrote a pretty simple way to do animation based on your already existing code. (Not tested btw, just to give an idea of the logic you can possibly use)Fix and change as you see fit.

#SelectExpand

1 2// A struct to keep track of Animation data. 3struct Animation
4{ 5enum 6{ 7 IDLE,
8 MOVE,
9 ATTACK,
10 ...
11}; 12 13// Does the animation loop? 14bool loop; 15// Optionally add this if you want to be able to cancel animations. 16// You might for example not want to allow the player 17// to cancel a slide or attack but you do want to allow the player 18// to cancel the idle animation. 19// You could build a far more complex system with some animations 20// allowing to be canceled by a few others but not all. 21bool cancellable; 22// Makes it easier to track whether or not the animation is still playing. 23bool ended; 24 25int maxFrame; 26int frameCount; 27int animationRow; 28int animationColumns; 29int animationDirection; 30int frameDelay; 31int curFrame; 32} 33 34struct Player
35{ 36 ...
37// Hold the animation data; 38 Animation animation; 39 ...
40} 41 42// Always call this once per frame for each object with an animation. 43void UpdateAnimation(Animation &animation) 44{ 45++animation.frameCount; 46if(animation.frameCount > animation.frameDelay) 47{ 48++animation.curFrame; 49 animation.frameCount =0; 50if(animation.curFrame >= animation.maxFrame) 51{ 52if(animation.loop) 53{ 54// Go back to the first frame if it should loop. 55 animation.curFrame =0; 56} 57else 58{ 59// Freeze the animation on the last frame. 60// And flag it as ended. 61 animation.curFrame = animation.maxFrame; 62 animation.ended =true; 63// Once the animation has ended assume 64// that it may now be cancelled. 65 animation.cancellable =true; 66} 67} 68} 69} 70 71// When you want to change the animation being played. 72// newAnimation is passed as a reference to a const Animation 73// to avoid accidentally writing to newAnimation instead of the 74// target animation. Not necessary. 75void ChangeAnimation(Animation &animation, const Animation &newAnimation) 76{ 77// If the current animation can't be cancelled, do nothing. 78// You might possibly want to change this to an assert. 79// So the program terminates with an error instead of ignoring it. 80if(animation.cancellable) 81{ 82// Copy new animation here 83// Alternatively remove this whole function and just do this assignment inline. 84// But this might be more error-prone if you forget to check any important information 85// about the animation like cancellable. 86 animation = newAnimation; 87} 88} 89 90void PlayerAttack(Player &player) 91{ 92if(!player.animation.cancellable) 93{ 94// If the current animation can't be cancelled, do an early return 95// and don't do any attack stuff. 96// Might not want to allow the player to attack if he's been stunned for example. 97return; 98} 99// *else* 100// Create a new animation 101 Animation newAnimation; 102 103// Initialize the new animation here 104 newAnimation.curFrame =0; 105 ...
106 107// Once the new animation has been initialized the player's animation can be changed 108// to the new one. 109 ChangeAnimation(player.animation, newAnimation); 110 111// Do whatever is necessary for dealing damage and all that here. 112 113}

I'm working through your code taron, trying to understand it all, but I have ran into a few issues and questions.

#SelectExpand

1struct Animation
2{ 3enum 4{ 5 IDLE,
6 MOVE,
7 ATTACK,
8 ...
9};

How do I declare or reference these within my main code ? (I've tried animation.IDLE, animation[IDLE], Animation.animation[IDLE] but none work, unless as its a struct it works differently to the way I done my enum keys[].)I'm assuming these would be where I code my variables for different animations. I haven't done this or seen this before.

However I do get the following errors C2146: syntax error: missing ';' before identifier 'animation'C4430: missing type specifier - int assumed.Note C++ does not support default-intC2039: 'animation' : is not a member of 'Player'(previously all my Player struct worked) And my Player struct is in a objects.h file, if this makes any difference.

I can place this in my main body of code although this would stop PlayerAttack from working as it would no longer reference to player (I think).

I do like the way you wrote this aswell, I understand most of it can see how it works and how it does the animations

Generally this means it doesn't recognize the type. If you've put the Animation in its own header file, make sure to include that header in your Player header, otherwise the compiler will not be able to find the definition of the type.If you've defined the Animation in your main.cpp or whatever file, move it to a header file.So it would look something like this:

Try not to go too far down a road that is suggested on here if it seems alien; I used 1 .cpp file and hundreds of separate variables for my first 20 odd projects....LONG before I started delving into enums and structs....keep it simple, and as soon as something doesn't make sense, come on here and post some code...that's how I learned!

So trying to do it in a way I understand. Using Mike Geig's tutorial on his spaceshooter he has a comet going across his screen using no input. It move across without any keys being involved and his code for that is

So looking at the 2 codes, why is one allowing it to be cycled all the time and perform the animation across the screen, yet with my code only cycle through the animation when I actually hold down the key. From what I gather I have removed the keypress part by adding the PlayerAttacking, so my PlayerAttacking is now similar to If the comet is onscreen.

The only big difference I see isMy code

#SelectExpand

1if(keys[KEY_1]) 2 PlayerAttack(Human);

Mike Geig's code

#SelectExpand

1UpdateComet(comets, NUM_COMETS);

We both have our calls in the ALLEGRO_EVENT_TIMER section aswell.

EDIT_ OMG I got it to workOn the bad side is when I move after attacking it plays that animation again, but I got it to work