5 Replies - 3561 Views - Last Post: 17 October 2011 - 11:52 AM

#1 ihs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 15-October 11

XNA 4.0 2D sprite animation while moving?

Posted 15 October 2011 - 04:29 PM

Hello all,

I have a very simple project I've created to get my head around XNA (and as bit of fun).

I am linking you to a small project where I'm trying to get the Ninja character to move from right to left while his walking animation happens.

There is also a smurf you will see who was from a help site.

2 problems:
1)I can't seem to slow ninja's walking animation down, even though I can with smurf (and they are same code) by changing FPS int value.

2)Walking animation just loops in one area, how do I get the ninja to move from right to left while his animation is happening. Note he must be an NPC enemy (so no keyboard input should be needed to move him).

I would REALLY like to keep the code as object oriented as possible, most examples I see put everything in Game1.cs, I do not want this.

Here is the project: https://rapidshare.c.../xna_test_2.zip

Thanks for any help.

Is This A Good Question/Topic? 0
  • +

Replies To: XNA 4.0 2D sprite animation while moving?

#2 ihs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 15-October 11

Re: XNA 4.0 2D sprite animation while moving?

Posted 15 October 2011 - 04:38 PM

Sorry, new link: https://rapidshare.c.../xna_test_2.zip

Also to start the game, press A, then B in following screen.

(sorry couldn't find edit post..)
Was This Post Helpful? 0
  • +
  • -

#3 ShadowsEdge19  Icon User is offline

  • D.I.C Addict

Reputation: 142
  • View blog
  • Posts: 664
  • Joined: 16-January 10

Re: XNA 4.0 2D sprite animation while moving?

Posted 15 October 2011 - 05:11 PM

Have you had a look at the MSDN Platformer Starter Kit? This has plenty of example uses of AI and user controlled movement animation on a side scrolling platformer.

This post has been edited by ShadowsEdge19: 15 October 2011 - 05:18 PM

Was This Post Helpful? 0
  • +
  • -

#4 ihs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 15-October 11

Re: XNA 4.0 2D sprite animation while moving?

Posted 15 October 2011 - 05:37 PM

View PostShadowsEdge19, on 15 October 2011 - 05:11 PM, said:

Have you had a look at the MSDN Platformer Starter Kit? This has plenty of example uses of AI and user controlled movement animation on a side scrolling platformer.

I have, but it's hard to take in all that code when you literally just began coding in XNA....However I guess I will take another look.

In the mean time, if anyone else can provide more specific help with the project I have included, please feel free.
Was This Post Helpful? 0
  • +
  • -

#5 ShadowsEdge19  Icon User is offline

  • D.I.C Addict

Reputation: 142
  • View blog
  • Posts: 664
  • Joined: 16-January 10

Re: XNA 4.0 2D sprite animation while moving?

Posted 16 October 2011 - 02:33 AM

The reason why the Ninja's animation is running faster than the Smurf is because in your Ninja tileset you have only 9 separate pieces for the animation whereas the Smurf tileset has 16 pieces. If you try and run the Ninja animation at the same speed as the Smurfs one then you will get a more noticeable refresh rate.

As to the other you might have better luck using floats for your animation position values rather than integers as you can get a more smoother motion with the right calculation.

Also at the moment you have the const START_POSITION_X and START_POSITION_Y which is plugged into the Draw() method, therefore you will ALWAYS start at that location and never move from that spot. You need to use those values as the base position and add the current mSpeed and mDirection to it to determine the correct position.

But you also need to update those 2 values, but at the moment I don't see you doing this, you are in fact resetting the values back to the same values each time the Update() is called.

mSpeed = Vector2.Zero;
mDirection = Vector2.Zero;

mSpeed.X = NINJA_SPEED;
mDirection.X = MOVE_LEFT;


Was This Post Helpful? 1
  • +
  • -

#6 ihs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 15-October 11

Re: XNA 4.0 2D sprite animation while moving?

Posted 17 October 2011 - 11:52 AM

View PostShadowsEdge19, on 16 October 2011 - 02:33 AM, said:

The reason why the Ninja's animation is running faster than the Smurf is because in your Ninja tileset you have only 9 separate pieces for the animation whereas the Smurf tileset has 16 pieces. If you try and run the Ninja animation at the same speed as the Smurfs one then you will get a more noticeable refresh rate.

As to the other you might have better luck using floats for your animation position values rather than integers as you can get a more smoother motion with the right calculation.

Also at the moment you have the const START_POSITION_X and START_POSITION_Y which is plugged into the Draw() method, therefore you will ALWAYS start at that location and never move from that spot. You need to use those values as the base position and add the current mSpeed and mDirection to it to determine the correct position.

But you also need to update those 2 values, but at the moment I don't see you doing this, you are in fact resetting the values back to the same values each time the Update() is called.

mSpeed = Vector2.Zero;
mDirection = Vector2.Zero;

mSpeed.X = NINJA_SPEED;
mDirection.X = MOVE_LEFT;


Your post helped me in my way of understanding the structure of it, and so I am now using another library for animation (and it works while moving).
Thanks for the pointers.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1