2 Replies - 2955 Views - Last Post: 23 February 2013 - 10:37 AM

#1 DaT3dst3r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 22-February 13

New Class Won't Work

Posted 22 February 2013 - 11:23 PM

Hello, I've been making a game before I've stumbled upon an error. This error will not let me move my player as I want it to move. I've looked all over the internet for some help, but none of the resources helped.

This is my class
using System.Text;
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;

namespace TacoSauce
{
    class Player
    {
        Vector2 Position;
        Texture2D Texture;
        int StageWidth, StageHeight;

        public Player(Texture2D texture, Vector2 position, int stagewidth, int stageheight)
        {
            Position = position;
            Texture = texture;

            StageWidth = stagewidth;
            StageHeight = stageheight;
        }

        public void Update()
        {
            if (Keyboard.GetState().IsKeyDown(Keys.Right))
            {
                Position.X += 1;
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Begin();
            spriteBatch.Draw(Texture, Position, Color.White);
            spriteBatch.End();
        }
    }
}



I want the following code to make the player move right, but the player won't move at all. I tried making it
 Update(GameTime gametime)
and
 Update (Spritebatch spritebatch)
but none of it will make the player move
public void Update()
        {
            if (Keyboard.GetState().IsKeyDown(Keys.Right))
            {
                Position.X += 1;
            }
        }


I don't know if this code will help you, but this is my Game1.cs.
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;

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

        Player player;

        public Main()
        {
            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

            player = new Player(this.Content.Load<Texture2D>("Sprites/Player/Player"), new Vector2(100, 100), graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight);

            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
        }

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

            
        }

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

            player.Draw(spriteBatch);

            base.Draw(gameTime);
        }
    }
}


Can you please help me?
Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: New Class Won't Work

#2 LiberLogic969  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 34
  • View blog
  • Posts: 85
  • Joined: 03-September 12

Re: New Class Won't Work

Posted 23 February 2013 - 12:08 AM

You need to call the players Update() method in the Game1 classes Update(GameTime gameTime) method :

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

                 player.Update(); // <----- This

             // TODO: Add your update logic here  
         }  



Was This Post Helpful? 0
  • +
  • -

#3 DaT3dst3r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 22-February 13

Re: New Class Won't Work

Posted 23 February 2013 - 10:37 AM

Thank you LiberLogic969, it actually works. Now I can continue my game I'm making!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1