#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2008
    Posts
    5
    Rep Power
    0

    [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:
    Code:
                highPunch = new UserControlledSprite(
                     Game.Content.Load<Texture2D>(@"Images/highPunch"),
                    Vector2.Zero, new Point(200, 200), 10, new Point(0, 900),
                    new Point(9, 1), new Vector2(6, 6));
    I am using this code for input:
    Code:
                if (Keyboard.GetState().IsKeyDown(Keys.F))
                {
                    inputAccept = true;
                    isHighPunching = true;
                }
    Then it should play the animation with this:
    Code:
    if (inputAccept == true)
                {
                    if (isHighPunching)
                    {
                        player = highPunch;
                    }
    }
    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.

    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.
  2. #2
  3. Null Pointer Exception
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2006
    Location
    america
    Posts
    3,355
    Rep Power
    1579
    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...

    Code:
    if (Keyboard.GetState().IsKeyDown(Keys.F) && !isHighPunching)
    //Can only punch if you are not currently punching...
                {
                    inputAccept = true;
                    isHighPunching = true;
                }
    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;
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2008
    Posts
    5
    Rep Power
    0
    Originally Posted by misterdanny
    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...

    Code:
    if (Keyboard.GetState().IsKeyDown(Keys.F) && !isHighPunching)
    //Can only punch if you are not currently punching...
                {
                    inputAccept = true;
                    isHighPunching = true;
                }
    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;
    This gave me the same result. My sprites do cycle through every frame, the button just has to be held.

    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.
  6. #4
  7. Null Pointer Exception
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2006
    Location
    america
    Posts
    3,355
    Rep Power
    1579
    It's hard to help you unless you post some code. I need to see where your writing everything.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2008
    Posts
    5
    Rep Power
    0
    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)

    Sprite class:
    Code:
            public Sprite(Texture2D textureImage, Vector2 position, Point frameSize,
            int collisionOffset, Point currentFrame, Point sheetSize, Vector2 speed,
            int millisecondsPerFrame)
            {
                this.textureImage = textureImage;
                this.position = position;
                this.frameSize = frameSize;
                this.collisionOffset = collisionOffset;
                this.currentFrame = currentFrame;
                this.sheetSize = sheetSize;
                this.speed = speed;
                this.millisecondsPerFrame = millisecondsPerFrame;
            }
    Draw:
    Code:
            public virtual void Draw(GameTime gameTime, SpriteBatch spriteBatch)
            {
                //Draw the sprite
                spriteBatch.Draw(textureImage,
                    position,
                    new Rectangle(currentFrame.X * frameSize.X,
                        currentFrame.Y * frameSize.Y,
                        frameSize.X, frameSize.Y),
                    Color.White, 0, Vector2.Zero,
                    1f, SpriteEffects.None, 0);
            }
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2008
    Location
    Fort Meade, MD
    Posts
    170
    Rep Power
    50
    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.

IMN logo majestic logo threadwatch logo seochat tools logo