play the animation when a key is pressed

May 26, 2011 at 1:23 AM

Hi every on I need a simple codes to play two animation for example idle and run by usin marine.fbx. ( Im a begginer).

when I press the left thumbstic to the up i need to play the run animation and when the key is not pressed in need the idle animation . i try it before but all i have is (a hang animation ) the animation not playing , its stopped in first frame and not movig . when i presse the left to up all in have the first frame from run and when i release i have a first frame from idle animation ( there is no animation ) . this is my codes :

 using
System;
using
System.Collections.Generic;
using
System.Linq;
using
Microsoft.Xna.Framework;
using
Microsoft.Xna.Framework.Audio;
using
Microsoft.Xna.Framework.Content;
using
Microsoft.Xna.Framework.GamerServices;
using
Microsoft.Xna.Framework.Graphics;
using
Microsoft.Xna.Framework.Input;
using
Microsoft.Xna.Framework.Media;
using
Microsoft.Xna.Framework.Net;
using
Microsoft.Xna.Framework.Storage;
using
XNAnimation;
using
XNAnimation.Controllers;
using
XNAnimation.Effects;
namespace
PlayerMarine_xna3 
{
 
/// <summary>
 
/// This is the main type for your game 
 
/// </summary> 
 
public class Game1 : Microsoft.Xna.Framework.Game 
{
 
GraphicsDeviceManager graphics; 
 
SpriteBatch spriteBatch; 
 
Matrix cameraView; 
 
Matrix cameraProjection; 
 
SkinnedModel skinnedModel; 
 
AnimationController animationController; 
 
 
public Game1() 
{
graphics =
new GraphicsDeviceManager(this); 
Content.RootDirectory =
"Content"; 
}
 
/// <summary> 
 
/// Allows the game to perform any initialization it needs to before starting to run. 
 
/// This is where it can query for any required services and load any non-graphic 
 
/// related content. Calling base.Initialize will enumerate through any components 
 
/// and initialize them as well. 
 
/// </summary> 
 
protected override void Initialize() 
{
 
// TODO: Add your initialization logic here 
 
base.Initialize(); 
}
 
/// <summary> 
 
/// LoadContent will be called once per game and is the place to load 
 
/// all of your content. 
 
/// </summary> 
 
protected override void LoadContent() 
{
 
// Create a new SpriteBatch, which can be used to draw textures. 
spriteBatch =
new SpriteBatch(GraphicsDevice); 
 
// TODO: use this.Content to load your game content here 
cameraView =
Matrix.CreateLookAt(new Vector3(10, 3, 40), new Vector3(0, 10, 0), Vector3.Up); 
cameraProjection =
Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45f) 
,graphics.GraphicsDevice.Viewport.AspectRatio
, 1, 1000);
skinnedModel = Content.Load<
SkinnedModel>("Entity/PlayerMarine"); 
animationController =
new AnimationController(skinnedModel.SkeletonBones); 
animationController.StartClip(skinnedModel.AnimationClips[
"Idle"]); 
 
}
 
/// <summary> 
 
/// UnloadContent will be called once per game and is the place to unload 
 
/// all content. 
 
/// </summary> 
 
protected override void UnloadContent() 
{
 
// TODO: Unload any non ContentManager content here 
}
 
/// <summary> 
 
/// Allows the game to run logic such as updating the world, 
 
/// checking for collisions, gathering input, and playing audio. 
 
/// </summary> 
 
/// <param name="gameTime">Provides a snapshot of timing values.</param> 
 
protected override void Update(GameTime gameTime) 
{
 
// Allows the game to exit 
 
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) 
 
this.Exit(); 
 
// TODO: Add your update logic here 
animationController.Update(gameTime.ElapsedGameTime,
Matrix.Identity); 
 
GamePadState padState; 
padState =
GamePad.GetState(PlayerIndex.One); 
 
 
if (padState.ThumbSticks.Left.Y > 0.2f) 
{ animationController.StartClip(skinnedModel.AnimationClips[
"Run"]); } 
 
else { animationController.StartClip(skinnedModel.AnimationClips["Idle"]);} 
 
 
base.Update(gameTime); 
}
 
/// <summary> 
 
/// This is called when the game should draw itself. 
 
/// </summary> 
 
/// <param name="gameTime">Provides a snapshot of timing values.</param> 
 
protected override void Draw(GameTime gameTime) 
{
GraphicsDevice.Clear(
Color.CornflowerBlue); 
 
// TODO: Add your drawing code here 
 
foreach (ModelMesh modelMesh in skinnedModel.Model.Meshes) 
{
 
foreach (SkinnedModelBasicEffect effect in modelMesh.Effects) 
{
 
// Setup camera 
effect.View = cameraView;
effect.Projection = cameraProjection;
 
// Set the animated bones to the model 
effect.Bones = animationController.SkinnedBoneTransforms;
 
// OPTIONAL - Configure material 
effect.Material.DiffuseColor =
new Vector3(0.8f); 
effect.Material.SpecularColor =
new Vector3(0.3f); 
effect.Material.SpecularPower = 8;
 
// OPTIONAL - Configure lights 
effect.AmbientLightColor =
new Vector3(0.1f); 
effect.LightEnabled =
true; 
effect.EnabledLights =
EnabledLights.One; 
effect.PointLights[0].Color =
Vector3.One; 
effect.PointLights[0].Position =
new Vector3(100); 
}
 
// Draw a model mesh 
modelMesh.Draw();
}
 
 
base.Draw(gameTime); 
}
}
}

thanks

May 28, 2011 at 6:05 PM

ok in found it . i must put the speed code after the start clipe line.