Original Post
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..
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..