none
"An object reference is required for the non-static field, method, or property"

    Question

  • Hi all! I'm learning C# in combination with XNA, but I'm having some trouble. I get these errors in my simple pong project:

    Error	1	An object reference is required for the non-static field, method, or property 'Pong2.Game1.ball'	c:\users\robin\documents\visual studio 2010\Projects\Pong2\Pong2\Pong2\BatEnemy.cs	20	8	Pong2
    
    
    Error	2	An object reference is required for the non-static field, method, or property 'Pong2.Game1.ball'	c:\users\robin\documents\visual studio 2010\Projects\Pong2\Pong2\Pong2\BatEnemy.cs	24	8	Pong2



    This happens when I want to request the y-position of instance ball.
    My Game1.cs file:

    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 Pong2
    {
    	/// <summary>
    	/// This is the main type for your game
    	/// </summary>
    	public class Game1 : Microsoft.Xna.Framework.Game
    	{
    		GraphicsDeviceManager graphics;
    		SpriteBatch spriteBatch;
    		public Bat playerbat = new Bat();
    		public BatEnemy enemybat = new BatEnemy();
    		public Ball ball = new Ball();
    		Texture2D spr_bat;
    		Texture2D spr_ball;
    
    		
    
    		public Game1()
    		{
    			graphics = new GraphicsDeviceManager(this);
    			Content.RootDirectory = "Content";
    		}
    
    		// Init:
    		protected override void Initialize()
    		{
    			//dingen initaliseren
    			
    			
    
    			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()
    		{
    			spr_bat	 = Content.Load<Texture2D>("bat");
    			spr_ball = Content.Load<Texture2D>("ball");
    			// 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();
    
    			// Update
    
    			playerbat.Update();
    			enemybat.Update();
    			ball.Update();
    
    			////
    
    			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);
    
    
    			spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
    
    			// Draw alle sprites:
    
    			spriteBatch.Draw(spr_bat, playerbat.position, null, Color.White, 0, new Vector2(0,0) , 1, SpriteEffects.None, 0); // Playerbat tekenen
    			spriteBatch.Draw(spr_bat, enemybat.position, null, Color.White, 0, new Vector2(0, 0), 1, SpriteEffects.None, 0); // Playerbat tekenen
    			spriteBatch.Draw(spr_ball, ball.position, null, Color.White, 0, new Vector2(0, 0), 1, SpriteEffects.None, 0); // Playerbat tekenen
    
    			////
    			spriteBatch.End();
    
    		
    
    			base.Draw(gameTime);
    		}
    
    		public void DrawSprite(Texture2D Sprite, Vector2 Position, Color BlendColor, float Rotation, float Scale, Vector2 Origin)
    		{
    		// DrawSprite(Texture2D sprite, Vector2 position, Color blendcolor, Float rotation, Float scale, Vector2 origin);
    		}
    	}
    }
    
    



    My BatEnemy.cs file

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Design;
    using Microsoft.Xna.Framework.Input;
    //using Pong2;
    
    namespace Pong2
    {
    	public class BatEnemy
    	{
    		public int y=0;
    		public Vector2 position = new Vector2(800-16,0);
    		public void Update()
    		{
    			if (Game1.ball.y<y && y>0)
    			{
    				y -= 5; // Omhoog gaan als de bal boven het batje is
    			}
    			if (Game1.ball.y > y && y < 480 - 64)
    			{
    				y += 5; // Omlaag gaan als de bal onder het batje is
    			}
    			position.Y = y; // Variabel position (vector2) updaten.
    			
    		}
    	}
    }
    


    Where it goes wrong in BatEnemy.cs
    if (Game1.ball.y<y && y>0)
    // and
    if (Game1.ball.y > y && y < 480 - 64)
    


    I've already googled, but the results I've found can't really help me... I'm quite new, so please bear with me. Also, sorry for my english, I'm dutch.

    Thanks for reading and eventually helping me :)
    Gmfreaky
    Sunday, December 26, 2010 9:11 PM

Answers

  • The problem is that you're accessing the y property as if it was a static member and it is not. I think what you actually are intending to do is maybe pass the Ball instance as a parameter to the BatEnemy constructor.

    So, in your Game1 class, change your declarations like so:

    // not 100% sure you can do this here. You may need to instantiate these in the constructor
    // but try it this way first if you like
    public Ball ball = new Ball();
    public BatEnemy enemybat = new BatEnemy(ball);
    
    

    Then, your BatEnemy class would need to be changed like so (note that you would need to add a constructor):

    public class BatEnemy
    {
      public int y=0;
      public Vector2 position = new Vector2(800-16,0);
      private Ball ball;
    	
      public BatEnemy(Ball _ball)
      {
        ball = _ball;
      }
      public void Update()
      {
        if (ball.y<y && y>0)
        {
          y -= 5; // Omhoog gaan als de bal boven het batje is
        }
        if (ball.y > y && y < 480 - 64)
        {
          y += 5; // Omlaag gaan als de bal onder het batje is
        }
        position.Y = y; // Variabel position (vector2) updaten.
      }
    }
    
    

     


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    • Marked as answer by Bin-ze Zhao Wednesday, December 29, 2010 9:28 AM
    Sunday, December 26, 2010 9:48 PM