Platformer.Player.Update C# (CSharp) Method

Update() public method

Handles input, performs physics, and animates the player sprite.
We pass in all of the input states so that our game is only polling the hardware once per frame. We also pass the game's orientation because when using the accelerometer, we need to reverse our motion when the orientation is in the LandscapeRight orientation.
public Update ( GameTime gameTime, KeyboardState keyboardState, GamePadState gamePadState, TouchCollection touchState, AccelerometerState accelState, DisplayOrientation orientation, InputManager inputManager ) : void
gameTime Microsoft.Xna.Framework.GameTime
keyboardState Microsoft.Xna.Framework.Input.KeyboardState
gamePadState Microsoft.Xna.Framework.Input.GamePadState
touchState TouchCollection
accelState AccelerometerState
orientation DisplayOrientation
inputManager InputManager
return void
        public void Update(
            GameTime gameTime, 
            KeyboardState keyboardState, 
            GamePadState gamePadState, 
            TouchCollection touchState, 
            AccelerometerState accelState,
            DisplayOrientation orientation,
            InputManager inputManager)
        {
            // Hook for InputManager
            inputManager.Update();

            if (IsAlive)
            {
                GetInput(keyboardState, gamePadState, touchState, accelState, orientation, inputManager);
            }

            ApplyPhysics(gameTime);

            if (IsAlive)
            {
                if (isOnGround)
                {
                    if (Math.Abs(Velocity.X) - 0.02f > 0)
                    {
                        if (Velocity.X > 0)
                        {
                            sprite.PlayAnimation(runRightAnimation);
                            isRight = true;
                        }
                        else
                        {
                            sprite.PlayAnimation(runLeftAnimation);
                            isRight = false;
                        }
                    }
                    else
                    {
                        idle();
                    }
                }
                else if (isClimbing)
                {
                    if (Velocity.Y > 0.02f)
                    {
                        sprite.PlayAnimation(ladderDownAnimation);
                    }
                    else if (Velocity.Y < 0.02f)
                    {
                        sprite.PlayAnimation(ladderUpAnimation);
                    }

                    if (Math.Abs(Velocity.Y) <= 0.02f)
                    {
                        sprite.Pause();
                    }
                }
                else if (!isJumping)
                {
                    if (Velocity.X > 0)
                    {
                        sprite.PlayAnimation(fallRightAnimation);
                    }
                    else if (Velocity.X < 0)
                    {
                        sprite.PlayAnimation(fallLeftAnimation);
                    }
                    else
                    {
                        sprite.PlayAnimation(isRight ? fallRightAnimation : fallLeftAnimation);
                    }
                }
            }

            // Check for fall damage
            checkFallDamage();

            // Clear input.
            movement = Vector2.Zero;
            wasClimbing = isClimbing;
            isClimbing = false;
            isJumping = false;
        }

Usage Example

        public override void Update(ContentManager Content, GameTime gameTime)
        {
            switch (level)
            {
            case Level.W1_L1:
                map = Content.Load <TiledMap>("Level_1");
                break;

            case Level.W1_L2:
                map = Content.Load <TiledMap>("Level_2");
                break;

            case Level.W1_L3:
                map = Content.Load <TiledMap>("Level_3");
                break;
            }

            if (isLoaded == false)
            {
                isLoaded    = true;
                ventureFont = Content.Load <SpriteFont>("3Dventure");
                psFont      = Content.Load <SpriteFont>("ps2p");
                //bgm = Content.Load<Song>("sacrifice");
                heart = Content.Load <Texture2D>("heart_x16");

                var viewportAdapter = new BoxingViewportAdapter(game.Window, game.GraphicsDevice, ScreenWidth, ScreenHeight);
                camera          = new Camera2D(viewportAdapter);
                camera.Position = new Vector2(0, ScreenHeight);

                foreach (TiledTileLayer layer in map.TileLayers)
                {
                    if (layer.Name == "collision")
                    {
                        collisionLayer = layer;
                    }
                }

                foreach (TiledObjectGroup group in map.ObjectGroups)
                {
                    if (group.Name == "player_spawn")
                    {
                        foreach (TiledObject obj in group.Objects)
                        {
                            player = new Player(this);
                            player.Load(Content);
                            player.Position = new Vector2(obj.X, obj.Y);
                        }
                    }

                    if (group.Name == "enemy_spawn")
                    {
                        foreach (TiledObject obj in group.Objects)
                        {
                            Enemy enemy = new Enemy(this);
                            enemy.Load(Content);
                            enemy.Position = new Vector2(obj.X, obj.Y);
                            enemies.Add(enemy);
                        }
                    }

                    if (group.Name == "goal")
                    {
                        foreach (TiledObject obj in group.Objects)
                        {
                            goal = new Goal(this);
                            goal.Load(Content);
                            goal.Position = new Vector2(obj.X, obj.Y);
                        }
                    }

                    if (group.Name == "key")
                    {
                        foreach (TiledObject obj in group.Objects)
                        {
                            key = new Key(this);
                            key.Load(Content);
                            key.Position = new Vector2(obj.X, obj.Y);
                        }
                    }

                    if (group.Name == "lock")
                    {
                        foreach (TiledObject obj in group.Objects)
                        {
                            LockedWall lockedWall = new LockedWall(this);
                            lockedWall.Load(Content);
                            lockedWall.Position = new Vector2(obj.X, obj.Y);
                            lockedWalls.Add(lockedWall);
                        }
                    }

                    if (group.Name == "extralife")
                    {
                        foreach (TiledObject obj in group.Objects)
                        {
                            extralife = new ExtraLife(this);
                            extralife.Load(Content);
                            extralife.Position = new Vector2(obj.X, obj.Y);
                        }
                    }
                }
            }

            if (musicLoad == false)
            {
                musicLoad = true;
                //MediaPlayer.Play(bgm);
            }

            keyS      = Content.Load <SoundEffect>("keys");
            splat     = Content.Load <SoundEffect>("splat");
            keyInst   = keyS.CreateInstance();
            splatInst = splat.CreateInstance();

            float deltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds;

            camera.Position = player.Position - new Vector2(ScreenWidth / 2, ScreenHeight / 1.5f);
            CameraMechanics();

            timer -= deltaTime;

            CheckCollisions();
            goal.Update(deltaTime);
            player.Update(deltaTime);
            key.Update(deltaTime);
            extralife.Update(deltaTime);
            foreach (Enemy e in enemies)
            {
                e.Update(deltaTime);
                e.ParticleUpdate(gameTime);
            }
            foreach (LockedWall lw in lockedWalls)
            {
                lw.Update(deltaTime);
            }

            if (game.AllowInput && game.AllowMenu == true)
            {
                if (Keyboard.GetState().IsKeyDown(Keys.Enter))
                {
                    AIE.StateManager.PushState("PauseState");
                    game.ResetInputTimer();
                }
            }

            if (game.AllowMenu == false)
            {
                if (Keyboard.GetState().IsKeyUp(Keys.Enter))
                {
                    game.AllowMenu = true;
                }
            }
        }