fout in script??

Status
Niet open voor verdere reacties.

diyatje

Nieuwe gebruiker
Lid geworden
23 jun 2010
Berichten
1
Hoi ik heb een vraagje, ik krijg steeds de volgende foutmelding:The GraphicsDevice property cannot be used before Initialize has been called, maar ik zie niet waar de fout ligt. Enig idee?

Code:
namespace Pacie
{
    public class Pacman: DrawableGameComponent
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Texture2D pacmanTexture;

        public Pacman(Game main, ContentManager contentManager):base(main)
        {
            pacmanTexture = contentManager.Load<Texture2D>("pacman");
           
        }
         /// <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>
        public 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);

        }

        /// <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>
        public override void Update(GameTime gameTime)
        {
            // Allows the game to exit
           // if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                //this.Exit();

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Draw(GameTime gameTime)
        {
            [COLOR="blue"]GraphicsDevice.Clear(Color.CornflowerBlue);[/COLOR] // hierbij krijg ik de foutmelding
            spriteBatch.Begin();
            spriteBatch.Draw(pacmanTexture,new Rectangle(0,0,40,40),Color.Blue);
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}

dankjewel alvast
 
Bovenaan in je code doe je het volgende:

[CPP]
GraphicsDeviceManager graphics;
[/CPP]

En vervolgens roep je dan in die void waar het fout goed lekker de grapics device aan, maar die is nergens geïnitialiseerd. Ik kan het nu even niet testen omdat ik XNA hier niet heb geinstalleerd maar probeer dit eens:

[CPP]
GraphicsDeviceManager graphics = new GraphicsDeviceManager();

//Ik ken de XNA library niet compleet uit mijn hoofd, maar dan zou het zo iets moeten worden
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
[/CPP]
 
Hey,

in de Draw methode moet je geen nieuwe GraphicDeviceManager maken..
Ik ken het probleem. De this.GraphicsDevice werkt niet, maar als je this.Game.GraphicsDevice doet werkt het wel ;)

Overigens hoef je de content manager niet mee te geven in de constructor.
met this.Game.Content.Load<Texture2D>("pacman"); lukt het ook.
En als tip, zet de code waarmee je content load in de methode "override LoadContent()".
Dat is de bedoeling van die methode en hiermee blijft je code geoptimaliseerd ;)

Succes en gr,
Mathijs
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan