views:

79

answers:

2

I'm sorry if this is insanely stupid, but I'm a total newbie when it comes to C# and especially the XNA Framework. I got the error message from above; why in the heck does it work for Platformer1's Player code but not for me?!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace WindowsGame1
{
    class Cell
    {
            public bool alive;
            public Texture2D CSprite;
            public int x
            {
                get { return x; }
                set { x = value; }
            }
            public int y
            {
                get { return y; }
                set { y = value; }
            }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {

        }

        public void Update(GameTime gameTime)
        {

        }

        protected override void LoadContent()
        {
            //spriteBatch = new SpriteBatch(GraphicsDevice);
            CSprite = new ContentManager.Load<Texture2D>("10by10tile"); // TODO: This does NOT work for some reason.
        }
    }
}

+2  A: 

ContentManager.Load is a method. You don't need the "new" statement. Try:

protected override void LoadContent()
{
       CSprite = ContentManager.Load<Texture2D>("10by10tile");
}
Reed Copsey
Pretty sure this will give you a null objectReference since ContentManager is a class - not an instance of the class.
Tim
@Tim: Not sure - he's overriding a base class member, in which case, the base class may have a "ContentManager" property. The compiler recognized it as a method, so I'm assuming the object exists...
Reed Copsey
@Tim: But - its difficult to tell. The (badly formatted) code suggests this is in a class that isn't subclassing anything (except Object) - in which case the override makes no sense...
Reed Copsey
I originally had it 'public void LoadContent()' or so. I later made it 'protected override void' because I don't know what I'm doing.
Anonymous
+1  A: 

I'm thinking that you may want to pass the instance of contentmanager - (this.Content from your Game class) - to your load content function.

protected override void LoadContent(ContentManager contentManager)
        {
            //spriteBatch = new SpriteBatch(GraphicsDevice);
            CSprite = contentManager.Load<Texture2D>("10by10tile");
        }

Edit: Not to palm you off or anything - but this is a simple - but useful tutorial to push you in the right direction with XNA, which should be able to show you all the basics you need.

Tim
So, uh, what, do I do this:
Anonymous
... class Cell { contentManager = this.Content; public bool alive;...
Anonymous
@Anon - You should be passing this.Content from your Game class, into your cell class - and then using it to load the content.
Tim
@Tim Alright, thanks. I'm not quite sure how I'll do that but I guess I'll try.
Anonymous