XNA Creators Club Online
Page 1 of 1 (6 items)
Sort Posts: Previous Next

Help with changing animated texture on key press.

Last post 05-01-2008 2:50 PM by Warren. 5 replies.
  • 04-28-2008 7:19 PM

    Help with changing animated texture on key press.

    Hi, im currently messing around with XNA 2.0 and have been attempting to make some 2d games.

    I am having trouble making my sprite move only on key press (by that i mean, his animation, not physically moving).

    Based on the How To: Animate a Sprite tutorial, i have succeeded in making his animation only walk when im pressing the down key, though when i try to change my animations, they get stuck on frame 0. The odd thing is, if i press right left or up, and let go, then press down again, the right, left or up sprites animate but move physically down.

    Am i going about this the right way? or is there a different way of going about this because im sure that what im doing is quite long winded for making a 2d character run around the viewport :P

    Can anyone point me in the right direction???

    flash.

  • 04-29-2008 4:54 AM In reply to

    Re: Help with changing animated texture on key press.

    Hey, I found that using the key input is great for just moving the character around and not doing the animation, and having an animation logic method for choosing which animation to use. The initialization method is only called once so you don't keep reseting your current frame to 0 (might be the reason why your animation keeps getting stuck). So.. here's to give you an idea:

    //Key Input
    public void Input()
    {
    if (Keyboard.GetState().IsKeyDown(Keys.D))
    {
    this.Position += new Vector2(3, 0);
    }
    if (Keyboard.GetState().IsKeyDown(Keys.A) )
    {
    this.Position += new Vector2(-3, 0);
    }
    }

    //Animation Logic
    public void AnimationCheck()
    {
    if (this.position.X < oldPosition.X)
    {
    if (this.TextureUsed != 1) //if he isn't walking
    {
    InitializeWalk(); //make him walk
    }
    if (this.SpriteEffects != SpriteEffects.FlipHorizontally) //Is he facing left?
    {
    this.SpriteEffects = SpriteEffects.FlipHorizontally; //face left
    }
    }
    else if (this.position.X > oldPosition.X)
    {
    if (this.TextureUsed != 1) //if he isn't walking
    {
    InitializeWalk(); //make him walk
    }
    if (this.SpriteEffects != SpriteEffects.None) //Is he facing right?
    {
    this.SpriteEffects = SpriteEffects.None; //face right
    }
    }
    else if (this.position.X == oldPosition.X)
    {
    if (this.TextureUsed != 0 && this.TextureUsed == 1 || this.TextureUsed == 3) //if he isn't walking
    {
    InitializeStop(); //make him stop
    }
    else if (this.TextureUsed != 4 && this.IsStarted == false) //if he isnt waiting and once hes done stopping
    {
    InitializeWait(); // make him wait
    }
    }
    }

    //Initializing Animations
    private void InitializeStop()
    {
    this.TextureUsed
    = 0;
    this.LastFrame
    = 13;
    this.ResetAnimation();
    this.IsStarted
    = true;
    this.IsLooped
    = false;
    }


    This is some of the code straight out of my current project. It has a bit more to it but I tried to simplify it as much as possible. I didnt put the InitializeWait and InitializeWalk methods because it's really just the same thing with a couple different values (ex: Walk's TextureUsed is 1).  The SpriteEffects are used to flip the animation. The character I am using is naturally facing right in the images I am using, so with a SpriteEffect.None - he will be facing right.

    Hopefully this helps!



    ------------------------------
    their != there != they're
    ------------------------------
  • 04-29-2008 8:28 PM In reply to

    Re: Help with changing animated texture on key press.

    Thats helps quite abit, but what would i do with regards to my up and down sprites, as my project is a top down game like Zelda: A link to the past. im wondering how id get my sprites to change and still animate. should i make a different class? 
  • 04-30-2008 3:07 AM In reply to

    Re: Help with changing animated texture on key press.

    You're talking about the same concept - just with the up and down animations? Just use the same logic and add the checks for Position.Y and oldPosition.Y

    So instead, it would be like...
    //Refer to previous code under AnimationCheck() Method
    //
    Right after the X comparisons... change the if (this.position.X == oldPosition.X) to
    else if (this.position.X == oldPosition.X && this.position.Y == oldPosition.Y)
    {
    if (this.TextureUsed !
    = blah blah blah) //if he isn't walking at all
    {
    InitializeStop(); //make him stop
    }
    else if (this.TextureUsed != blah blah blah) //if he isn't waiting and done stopping
    {
    InitializeWait(); // make him wait
    }
    }
    else if (this.position.Y > oldPosition.Y)
    {
    if (this.TextureUsed != blah blah blah) //if he isn't walking Down
    {
    InitializeWalkDown(); //make him walk down
    }
    //You probably wont have to mess with SpriteEffects on these because
    //you'll probably have 2 different textures for up and down animations.

    }
    else if (this.position.Y < oldPosition.Y)
    {
    if (this.TextureUsed != blah blah blah) //if he isn't walking up
    {
    InitializeWalkUp(); //make him walk up
    }
    //Same deal as before on SpriteEffects
    }



    I havent used the up and down animation code, but that should give you an idea on the logic of it. It's really the same thing as the animations for left and right.. just add the top and bottom ones as well... and checking for different textures that are used.

    Let me know if this is what you wanted. Hopefully it helps!
    ------------------------------
    their != there != they're
    ------------------------------
  • 05-01-2008 4:14 AM In reply to

    Re: Help with changing animated texture on key press.

    Ok, there are a couple of concepts i dont understand, i understand the idea of how this will all work. but i dont understand what i do to set values like oldPosition.X. i also dont know what effect InitializeWait(); will have. I think if i undertand these last concepts i should be set :) Thanks alot forall your help.
  • 05-01-2008 2:50 PM In reply to

    Re: Help with changing animated texture on key press.

    Ok, well, for my game, I had an animation that had my character stopping and an animation for him standing idle.. that's why there are two there. I'm not sure if you have two, if not, just use Wait or Stop instead of both.

    The oldPosition and Position should be a Vector2 (so it will have both X and Y stored into it).

    At the end of each update, make sure you do:

    oldPosition = Position;

    And you should be set!

    Goodluck!
    ------------------------------
    their != there != they're
    ------------------------------
Page 1 of 1 (6 items) Previous Next
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/
DCSIMG