Skip to main content
GameDev.net gamedev.net
🔒 Locked 🤖 Godot

how to make one looping animation

Started by mastergame Oct 24, 2012 at 2:53 PM 4 replies 1.4k views
Original Post
mastergame
mastergame
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..
SimonForsman
SimonForsman
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.
[size="1"]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!
Khatharr
Khatharr
Gah - posted in wrong thread. Sry.
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.
mastergame
mastergame

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..
Mekuri
Mekuri
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.
Valgor
Valgor
[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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.