Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

how to make one looping animation


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 mastergame   Members   -  Reputation: 106

Like
0Likes
Like

Posted 24 October 2012 - 08:53 AM

hello every one..
i have problem in one times looping animation..
i have the one fighter
i want that doing one times kick animation when i press one keyboard button..
but i only can make the fighter doing kick animation when i hold the one keyboard button..
if not hold, it can not kick animation..
this is my code ..
character.cs page
[source lang="java"]using Microsoft.Xna.Framework;using Microsoft.Xna.Framework.Graphics;using Microsoft.Xna.Framework.Input;namespace fighting{ class Character { //animating the character //public Animation charanimation; Texture2D fighttexture; float timer = 0f; float interval = 100f; int currentframe = 0; int fightwidth = 0; int fightheight = 0; int fightspeed = 2; Rectangle sourcerect; Vector2 position; Vector2 origin; float scale; int framecount; int frametime; public bool Looping; int currentImageIndex; public Vector2 Position { get { return position; } set { position = value; } } public Vector2 Origin { get { return origin; } set { origin = value; } } public Texture2D Texture { get { return fighttexture; } set { fighttexture = value; } } public Rectangle SourceRect { get { return sourcerect; } set { sourcerect = value; } } public Character(Texture2D texture, int currentFrame, int fightWidth, int fightHeight) { fighttexture = texture; currentframe = currentFrame; fightwidth = fightWidth; fightheight = fightHeight; } KeyboardState currentKBState; KeyboardState previousKBState; public void HandleFightMovement(GameTime gameTime) { previousKBState = currentKBState; currentKBState = Keyboard.GetState(); sourcerect = new Rectangle(currentframe * fightwidth, 0, fightwidth, fightheight); if (currentKBState.GetPressedKeys().Length == 0) { if (currentframe > 0 && currentframe < 7) { currentframe = 0; } } if(currentKBState.IsKeyDown(Keys.A)) { if (position.X >20) { position.X -= fightspeed ; } } if (currentKBState.IsKeyDown(Keys.D)) { if (position.X < 780) { position.X += fightspeed; } } if (currentKBState.IsKeyDown(Keys.RightShift)) { animatekick(gameTime); } if(currentKBState.IsKeyDown(Keys.L)) { oncekick(gameTime); } } private void oncekick(GameTime gt) { timer += (float)gt.ElapsedGameTime.TotalMilliseconds; if (timer > interval) { currentframe++; loopOnce(1, 5); if (currentframe > 6) { currentframe = 0; } timer = 0f; } } public void animatekick(GameTime gt) { if(currentKBState !=previousKBState) { currentframe=0; } timer += (float)gt.ElapsedGameTime.TotalMilliseconds; if (timer > interval) { currentframe++; if (currentframe > 6) { currentframe = 0; } timer = 0f; } } private void loopOnce(int min,int max) { if (currentframe > max || currentframe < min) //Checks to see if index out of range of current animation currentframe = min; //Starts at the beginning of the animation for (int i = min; i < max; i++) //Uses the range to determine when to stop { currentframe++; } //Increments index each iteration that passes } }}[/source]



the kick animation contain 7 frames..
when i click L button, it only doing frame 5
how to make it can doing frame 1,2,3,4,5 and 6 in only press button L once?
thx..

Sponsor:

#2 SimonForsman   Members   -  Reputation: 3714

Like
0Likes
Like

Posted 24 October 2012 - 09:48 AM

don't run the animation from the input handler, set a flag in the input handler, run the animation in the gameloop if the flag is set and clear the flag when the animation has reached its final frame.
I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

#3 Khatharr   Members   -  Reputation: 1574

Like
0Likes
Like

Posted 24 October 2012 - 11:33 PM

Gah - posted in wrong thread. Sry.

Edited by Khatharr, 24 October 2012 - 11:35 PM.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

#4 mastergame   Members   -  Reputation: 106

Like
0Likes
Like

Posted 26 October 2012 - 10:23 AM

don't run the animation from the input handler, set a flag in the input handler, run the animation in the gameloop if the flag is set and clear the flag when the animation has reached its final frame.

do u mean set some breakpoint?

I have change the code in onekick funtion like this:
[source lang="java"] private void oncekick(GameTime gt) { timer += (float)gt.ElapsedGameTime.TotalMilliseconds; if (timer > interval) { for (int i = 1; i < 6; i++) { timer = 0f; currentframe++; if (currentframe > 6) { currentframe = 0; } } }[/source]
now it can kick from frame 1-6 in one button click, but too fast..
what should i add the code for delaying time between one frame with others?
so can see the kick animation more slow motion..

#5 Mekuri   Members   -  Reputation: 265

Like
0Likes
Like

Posted 26 October 2012 - 11:48 AM

Basically, every time you hit the kick button, set a boolean to true. And while this is true the animation will be performed. When the animation is done, set the bool to false.

Your oncekick method
Don't use that for loop inside your "timer" if. The way you do it there it will play all the animations everytime the interval is reached. Simply remove the for loop, and keep what's inside it.
Thus the logic will be: Every time timer has reached interval, it resets the timer to zero, increases current frame by one and inside your "currentframe" if, you should set the boolean that determines whether you are kicking or not to false.

Check out the game I am making here - http://www.youtube.com/user/NasarethMekuri


#6 Valgor   Members   -  Reputation: 126

Like
0Likes
Like

Posted 26 October 2012 - 06:46 PM

[source lang="csharp"]bool kicking = false;// Assuming keyboard input is already given in another function// which then sets the kicking boolean to true;private void oncekick(GameTime gametime){ timer += (float)gametime.ElapsedGameTime.TotalMilliseconds; if (timer > interval) { if ( kicking ) { currentframe++; if (currentframe > 6) { currentframe = 0; kicking = false; } } timer = 0f; }}[/source]

This is my idea on how to do it. The kicking boolean is set in the class definition. Every game update call will update the current frame. If it's still too fast you can increase the interval time.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS