3 Replies - 1380 Views - Last Post: 15 February 2012 - 01:25 PM

#1 sambobastud10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 31-August 09

C# xna My character/sprite wont jump...

Posted 15 February 2012 - 04:34 AM

Hi ive been at this for hours, I was trying to figure out what was wrong with the code I try to put my Updatejump and jump methods in my player class but it gave me an error: object does not contain a definition for Update... so i put them in my game1 class and it runs but my player wont jump... here is my player class
using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace zombieGame
{
    class Player
    {
        

        // Animation representing the player
        public Texture2D PlayerTexture;

        // Position of the Player relative to the upper left side of the screen
        public Vector2 Position;
        public Vector2 Direction = Vector2.Zero;
        public Vector2 Speed = Vector2.Zero;
        // State of the player
        public bool Active;

        // Amount of hit points that player has
        public int Health;

        // Get the width of the player
        public int Width
        {
            get { return PlayerTexture.Width; }
        }

        // Get the height of the player
        public int Height
        {
            get { return PlayerTexture.Height; }
        }

      
        public void Initialize(Texture2D texture, Vector2 position)
        {
            PlayerTexture = texture;

            // Set the starting position of the player around the middle of the screen and to the back
            Position = position;

            // Set the player to be active
            Active = true;

            // Set the player health
            Health = 100;
        }


        public void Update()
        {
           
        }


        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(PlayerTexture, Position, null, Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);
        }
    }
}

and here is my game1 class
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.Storage;

namespace zombieGame
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        //used for jump
        const int MOVE_UP = -1;
        const int MOVE_DOWN = 1;
        const int PLAYER_SPEED = 160;
        // Represents the player 
        Player player;

        enum State
        {
            Walking,
            Jumping
        }
        State CurrentState = State.Walking;

        Vector2 StartingPosition = Vector2.Zero;

        // Keyboard states used to determine key presses
        KeyboardState currentKeyboardState;
        KeyboardState previousKeyboardState;

        // A movement speed for the player
        float playerMoveSpeed;

        // Image used to display the static background
        Texture2D mainBackground;

        // Parallaxing Layers
        Background bgLayer1;
        Background bgLayer2;
        Background bgLayer3;

        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()
        {
            // Initialize the player class
            player = new Player();

            // Set a constant player move speed
            playerMoveSpeed = 8.0f;

            // Initialize our background
            bgLayer1 = new Background();
            bgLayer2 = new Background();
            bgLayer3 = new Background();
            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);

            // Load the player resources 
            Vector2 playerPosition = new Vector2(GraphicsDevice.Viewport.TitleSafeArea.X, GraphicsDevice.Viewport.TitleSafeArea.Y + GraphicsDevice.Viewport.TitleSafeArea.Height / 2);
            player.Initialize(Content.Load<Texture2D>("Player"), playerPosition);

            // Load the parallaxing background after the initialize player
            bgLayer1.Initialize(Content, "cloud2", GraphicsDevice.Viewport.Width, -1);
            bgLayer2.Initialize(Content, "cloud3", GraphicsDevice.Viewport.Width, -2);
            bgLayer3.Initialize(Content, "Trees", GraphicsDevice.Viewport.Width, -4);
            
            // the main background
            mainBackground = Content.Load<Texture2D>("CloudAndGrass");
            // TODO: use this.Content to load your game content here
        }

        /// <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();

            // Read the current state of the keyboard and store it
            currentKeyboardState = Keyboard.GetState();
           
            //Update the player jump
            UpdateJump(currentKeyboardState);

            // Save the previous state of the keyboard  so we can determinesingle key/button presses
            previousKeyboardState = currentKeyboardState;

            // Update the parallaxing background AFTER THE UPDATE PLAYER
            bgLayer1.Update();
            bgLayer2.Update();
            bgLayer3.Update();

            base.Update(gameTime);
        }

        private void UpdatePlayer(GameTime gameTime)
        {

        }


        private void UpdateJump(KeyboardState currentKeyboardState)
        {
            if (CurrentState == State.Walking)
            {
                if (currentKeyboardState.IsKeyDown(Keys.Space) == true && previousKeyboardState.IsKeyDown(Keys.Space) == false)
                {
                    Jump();
                }
            }

            if (CurrentState == State.Jumping)
            {
                if (StartingPosition.Y - player.Position.Y > 150)
                {
                    player.Direction.Y = MOVE_DOWN;
                }

                if (player.Position.Y > StartingPosition.Y)
                {
                    player.Position.Y = StartingPosition.Y;
                    CurrentState = State.Walking;
                    player.Direction = Vector2.Zero;
                }
            }
        }

        private void Jump()
        {
            if (CurrentState != State.Jumping)
            {
                CurrentState = State.Jumping;
                StartingPosition = player.Position;
                player.Direction.Y = MOVE_UP;
                player.Speed = new Vector2(PLAYER_SPEED, PLAYER_SPEED);
            }
        }

        /// <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);

            spriteBatch.Begin();

            spriteBatch.Draw(mainBackground, Vector2.Zero, Color.White);

            // Draw the Player
            player.Draw(spriteBatch);
            // Draw the moving background
            bgLayer1.Draw(spriteBatch);
            bgLayer2.Draw(spriteBatch);
            bgLayer3.Draw(spriteBatch);
            
          
            spriteBatch.End();

            
            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: C# xna My character/sprite wont jump...

#2 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1356
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: C# xna My character/sprite wont jump...

Posted 15 February 2012 - 05:20 AM

I'll move this to XNA where it belongs.

As for the problem you're having, you have MOVE_UP set to -1. In the Jump() method, you're setting the player's position.Y to MOVE_UP, which is the same as saying you are setting the Y position of the player to -1, rather than subtracting from the current position.Y. Try changing that around so that you're adding or subtracting from the position.Y such as player.Position.Y += MOVE_UP;. Also, make sure you do the same for the part in your code where the player's position comes back down.
Was This Post Helpful? 0
  • +
  • -

#3 sambobastud10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 31-August 09

Re: C# xna My character/sprite wont jump...

Posted 15 February 2012 - 12:45 PM

o sorry for posting in wrong forum... so yea i put in the code as so for the UpdateJump()
if (CurrentState == State.Jumping)
            {
                if (StartingPosition.Y - player.Position.Y > 150)
                {
                    player.Direction.Y += MOVE_DOWN;
                }

and on the jump()
if (CurrentState != State.Jumping)
            {
                CurrentState = State.Jumping;
                StartingPosition = player.Position;
                player.Direction.Y -= MOVE_UP;
                player.Speed = new Vector2(PLAYER_SPEED, PLAYER_SPEED);
            }

and still the player wont jump...
Was This Post Helpful? 0
  • +
  • -

#4 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1356
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: C# xna My character/sprite wont jump...

Posted 15 February 2012 - 01:25 PM

MOVE_DOWN is a positive value, which means you're adding the value to the current player.position.Y, which means you're moving the character closer to the bottom of the screen rather than the other way.

Also consider that you're only changing the value by 1, which translates to 1 pixel. Try setting the value considerably higher at first just to make sure that the position is actually changing.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1