10 Replies - 2098 Views - Last Post: 26 May 2011 - 09:47 AM

#1 Gonzo0193  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 49
  • Joined: 16-December 09

Help: XNA Error

Posted 26 May 2011 - 06:57 AM

Main.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 TestGameOOP
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Main : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        //Player image
        static string PLAYER_IMAGE = "lucas";

        //Player texture
        Texture2D playerTexture;

        //Player coords
        static int PLAYER_X = 57;
        static int PLAYER_Y = 337;
        static int PLAYER_WIDTH = 40;
        static int PLAYER_HEIGHT = 48;

        //Frame defaults
        static int DEFAULT_FRAME_W = 40;
        static int DEFAULT_FRAME_H = 48;

        //Initialise class variables
        Player player = new Player(PLAYER_X, PLAYER_Y, PLAYER_WIDTH, PLAYER_HEIGHT, DEFAULT_FRAME_W, DEFAULT_FRAME_H);
        Keypresses keypresses = new Keypresses();

        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

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

            playerTexture = this.Content.Load<Texture2D>(PLAYER_IMAGE);
            player.loadedPlayerTexture(playerTexture);
            // 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)
        {
            // TODO: Add your update logic here

            //Assign the variable keyState to the KeyboardState object
            KeyboardState keyState = Keyboard.GetState();

            // Allows the game to exit
            if (Keypresses.checkExitKey(keyState) == true)
                this.Exit();

            

            //Handle keyboard input
            Keypresses.checkPresses(keyState, player);

            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
            player.draw();

            base.Draw(gameTime);
        }
    }
}



Player.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace TestGameOOP
{
    public class Player
    {
        string playerState;
        string playerMove;
        Texture2D playerImage;
        Rectangle playerBounds;
        Rectangle srcBounds;

        public Player(int gPlayerX, int gPlayerY, int gPlayerWidth, int gPlayerHeight, int gDefaultFrameW, int gDefaultFrameH)
        {
            playerBounds = new Rectangle(gPlayerX, gPlayerY, gPlayerWidth, gPlayerHeight);
            srcBounds = new Rectangle(0, 0, gDefaultFrameW, gDefaultFrameH);
        }

        public void gMove(string direction)
        {
            playerMove = direction;
        }

        public void move()
        {
            
        }

        public void jump()
        {

        }

        void shoot()
        {

        }

        void die()
        {

        }

        public void loadedPlayerTexture(Texture2D gPlayerImage)
        {
            playerImage = gPlayerImage;
        }

        public void draw()
        {
            SpriteBatch.Draw(playerImage, playerBounds, srcBounds, Color.White);
        }

        public void getPlayerState(string gPlayerState)
        {
            playerState = gPlayerState;
        }

        public string returnPlayerState()
        {
            return playerState;
        }
    }
}



Keypresses
using System;
using System.Collections.Generic;
using System.Linq;
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;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace TestGameOOP
{
    public class Keypresses
    {


        public Keypresses()
        {
            
        }

        public static void checkPresses(KeyboardState keyState, Player player)
        {
            string direction = "stand";

            if(keyState.IsKeyDown(Keys.Left))
                direction = "left";


            if(keyState.IsKeyDown(Keys.Right))
                direction = "right";


            if(keyState.IsKeyDown(Keys.Up))
                direction = "up";


            if(keyState.IsKeyDown(Keys.Down))
                direction = "down";

            player.gMove(direction);
            player.getPlayerState(direction);
        }

        public static bool checkExitKey(KeyboardState keyState)
        {
            if (keyState.IsKeyDown(Keys.Escape))
            {
                return true;
            }
            
            return false;
        }
    }
}



Error
Error	1	An object reference is required for the non-static field, method, or property 'Microsoft.Xna.Framework.Graphics.SpriteBatch.Draw(Microsoft.Xna.Framework.Graphics.Texture2D, Microsoft.Xna.Framework.Rectangle, Microsoft.Xna.Framework.Rectangle?, Microsoft.Xna.Framework.Color)'	c:\users\gonzo\documents\visual studio 2010\Projects\TestGameOOP\TestGameOOP\TestGameOOP\Player.cs	56



I have checked my variables and none of them are null. I'm really stuck with this error. Any help would be great :)

Edit: Just realised i posted in the wrong place, reported it to be moved :)

This post has been edited by Gonzo0193: 26 May 2011 - 06:58 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Help: XNA Error

#2 bonyjoe  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 175
  • View blog
  • Posts: 548
  • Joined: 08-September 10

Re: Help: XNA Error

Posted 26 May 2011 - 07:44 AM

You need to have a spritebatch.begin() call and a spritebatch.end() call surrounding your draw code.
Was This Post Helpful? 1
  • +
  • -

#3 Gonzo0193  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 49
  • Joined: 16-December 09

Re: Help: XNA Error

Posted 26 May 2011 - 07:51 AM

After doing that i get:
Error	1	The name 'spriteBatch' does not exist in the current context	c:\users\gonzo\documents\visual studio 2010\Projects\TestGameOOP\TestGameOOP\TestGameOOP\Player.cs	56


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: Help: XNA Error

Posted 26 May 2011 - 07:54 AM

On line 56 of your player class, you try to call the draw method using "SpriteBatch". This is done incorrectly, since you do not have "SpriteBatch" declared in this class. Also, using "SpriteBatch" with the S capitalized means that you're trying to invoke the draw method of the SpriteBatch class without using an instance of the spriteBatch class. The capital S at the beginning of spriteBatch will always result in Visual Studio thinking you're trying to use the class rather than an instance of the class. This will always result in an error since you can only invoke methods of a class through an instance.

Declare a spriteBatch in your player class:
SpriteBatch spriteBatch;

Then invoke the draw method of the SpriteBatch class using the instance you've created:
spriteBatch.Begin();
spriteBatch.Draw(blah, blah, blah, blah, blah);
spriteBatch.End();

And see how that works for you.
Was This Post Helpful? 1
  • +
  • -

#5 Gonzo0193  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 49
  • Joined: 16-December 09

Re: Help: XNA Error

Posted 26 May 2011 - 08:03 AM

That has helped get rid of my original error, but now i get:

Error	1	'Microsoft.Xna.Framework.Graphics.SpriteBatch' does not contain a definition for 'begin' and no extension method 'begin' accepting a first argument of type 'Microsoft.Xna.Framework.Graphics.SpriteBatch' could be found (are you missing a using directive or an assembly reference?)	c:\users\gonzo\documents\visual studio 2010\Projects\TestGameOOP\TestGameOOP\TestGameOOP\Player.cs	58



and

Error	2	'Microsoft.Xna.Framework.Graphics.SpriteBatch' does not contain a definition for 'end' and no extension method 'end' accepting a first argument of type 'Microsoft.Xna.Framework.Graphics.SpriteBatch' could be found (are you missing a using directive or an assembly reference?)	c:\users\gonzo\documents\visual studio 2010\Projects\TestGameOOP\TestGameOOP\TestGameOOP\Player.cs	60



EDIT: I missed the capital letters, Thanks for both your help!

EDIT: I now get:
An unhandled exception of type 'System.NullReferenceException' occurred in TestGameOOP.exe

Additional information: Object reference not set to an instance of an object.


On the spriteBatch.Begin()

This post has been edited by Gonzo0193: 26 May 2011 - 08:05 AM

Was This Post Helpful? 0
  • +
  • -

#6 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



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

Re: Help: XNA Error

Posted 26 May 2011 - 08:07 AM

Just remember that capitalization is VERY important. There is a big difference, according to most development environments between 'A' and 'a'.

Forgot to mention that when creating an instance of the SpriteBatch class, you need to use the 'new' keyword:
SpriteBatch spriteBatch = new SpriteBatch(GraphicsDevice);

This post has been edited by Kilorn: 26 May 2011 - 08:08 AM

Was This Post Helpful? 1
  • +
  • -

#7 Gonzo0193  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 49
  • Joined: 16-December 09

Re: Help: XNA Error

Posted 26 May 2011 - 08:14 AM

Thanks, but doing that caused this ^^

Error	1	'Microsoft.Xna.Framework.Graphics.GraphicsDevice' is a 'type' but is used like a 'variable'	c:\users\gonzo\documents\visual studio 2010\Projects\TestGameOOP\TestGameOOP\TestGameOOP\Player.cs	18



Sorry to seem like such a nag. But thankyou for all of your help :)
Was This Post Helpful? 0
  • +
  • -

#8 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



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

Re: Help: XNA Error

Posted 26 May 2011 - 08:23 AM

Actually, scratch what I said earlier about the SpriteBatch declaration. Change your Draw method to look like:
public void Draw(SpriteBatch spriteBatch)
{
    SpriteBatch.Draw(playerImage, playerBounds, srcBounds, Color.White);
}



This will force you to pass in a SpriteBatch object to any calls to this method, so in your game1 class, when it comes time to draw the player, you'll do something like:
player.Draw(spriteBatch);



You'll already have the SpriteBatch declared in the game1 class as well as the Graphics Device. Having it declared elsewhere is unnecessary for this particular project.
Was This Post Helpful? 0
  • +
  • -

#9 Gonzo0193  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 49
  • Joined: 16-December 09

Re: Help: XNA Error

Posted 26 May 2011 - 08:28 AM

That brings back the first error again ^^.
Was This Post Helpful? 0
  • +
  • -

#10 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



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

Re: Help: XNA Error

Posted 26 May 2011 - 08:31 AM

Yea, I made the mistake of capitalizing SpriteBatch in the Draw method of the player class. That'll teach me to try to rush through things when I'm supposed to be working. Change that to 'spriteBatch' and it should work.

EDIT:
public void Draw(SpriteBatch spriteBatch)
{
    spriteBatch.Draw(playerImage, playerBounds, srcBounds, Color.White);
}



This post has been edited by Kilorn: 26 May 2011 - 08:32 AM

Was This Post Helpful? 1
  • +
  • -

#11 Gonzo0193  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 49
  • Joined: 16-December 09

Re: Help: XNA Error

Posted 26 May 2011 - 09:47 AM

View PostKilorn, on 26 May 2011 - 09:31 AM, said:

Yea, I made the mistake of capitalizing SpriteBatch in the Draw method of the player class. That'll teach me to try to rush through things when I'm supposed to be working. Change that to 'spriteBatch' and it should work.

EDIT:
public void Draw(SpriteBatch spriteBatch)
{
    spriteBatch.Draw(playerImage, playerBounds, srcBounds, Color.White);
}




Thank you so much, If i could click the + more than once i would :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1