August 23rd, 2009, 04:33 PM
[XNA] Sprite Play Through ?
Hello, I am pretty new to programming C# and I have got my code to work to a point but I cannot seem to get this to work to my knowledge without using a timer (which would not be consistent for every sprite I want to play)
What is happening is, the sprite only plays WHILE the button is being held down. I want all of the sprite frames to play through once and then stop, when the button is pressed.
All of this code works, but just looping the sprite.
I am using this to load the sprite:
I am using this code for input:
highPunch = new UserControlledSprite(
Vector2.Zero, new Point(200, 200), 10, new Point(0, 900),
new Point(9, 1), new Vector2(6, 6));
Then it should play the animation with this:
inputAccept = true;
isHighPunching = true;
I am aware that it is probably due to the way that the inputs are being accepted, or the if statement only being true while the button is held down.
if (inputAccept == true)
player = highPunch;
But I cannot find anything on it, I have checked a ton of resources and they all teach this same way. But none of them show you how to play through all the frames of a sprite after a button is pressed.
Can someone please show me where to go from here, or how to set it up to run all the way through and not just while the button is held?
Any help is appreciated.
August 23rd, 2009, 11:37 PM
I take it this is some sort of fighting game like mortal combat?
I'll give you a few pointers, you should be able to figure out the rest...
Then on the code that animates the player punching, you want the last "part" of the animation (after hes done punching) to set isHighPunching=false;
if (Keyboard.GetState().IsKeyDown(Keys.F) && !isHighPunching)
//Can only punch if you are not currently punching...
inputAccept = true;
isHighPunching = true;
August 24th, 2009, 01:25 AM
This gave me the same result. My sprites do cycle through every frame, the button just has to be held.
Originally Posted by misterdanny
I was thinking about making a timer that would allow that animation to play through for a certain amount of time (because the boolean would be true until it ended), but I don't want to do that for every animation.
(And it would be messed up if I wanted to change the speed later)
My code just doesn't know to play all frames, only while the button is held down.
I am used to Actionscript where this problem doesn't really exist.
August 24th, 2009, 09:02 AM
It's hard to help you unless you post some code. I need to see where your writing everything.
August 24th, 2009, 03:38 PM
I didn't want to overload this with code at first.
Thank you for helping me.
I have everything setup to run through all of the frames, but I am not sure how to get it to actually run through the frames automatically after a single button press (and not holding it down)
public Sprite(Texture2D textureImage, Vector2 position, Point frameSize,
int collisionOffset, Point currentFrame, Point sheetSize, Vector2 speed,
this.textureImage = textureImage;
this.position = position;
this.frameSize = frameSize;
this.collisionOffset = collisionOffset;
this.currentFrame = currentFrame;
this.sheetSize = sheetSize;
this.speed = speed;
this.millisecondsPerFrame = millisecondsPerFrame;
public virtual void Draw(GameTime gameTime, SpriteBatch spriteBatch)
//Draw the sprite
new Rectangle(currentFrame.X * frameSize.X,
currentFrame.Y * frameSize.Y,
Color.White, 0, Vector2.Zero,
1f, SpriteEffects.None, 0);
August 24th, 2009, 05:14 PM
I've never used C#, so forgive me if this post is nonsense, but it looks from the code you posted, that pressing the button merely sets a boolean to true - doesn't actually draw anything. The two additional methods/functions you posted don't provide anything helpful about why the animation only draws while the button is being pressed. I think what we need to see is where the 'Draw' method/function is being called from.
What happens when you release the button - does it stop in the middle of the animation on the current frame, or does it go back to some default frame? This will help narrow down where the problem is coming from.
It seems to me that the 'Draw' method/function should be called from a "main game loop" thread which doesn't care whether a button is being pressed or not - it should just check the boolean(s) to determine what needs to be drawn, and change the boolean(s) when the end of an animation has been reached, as misterdanny suggested.