I’ve been working on a XNA project for a class I’m taking. I had an issue with a class that I was making for the “Bad guys” which are snowmen (the class is called Snowman.cs). I cannot get the program to compile and it’s driving me crazy. Someone helped me out with what I think is some solid code for the Snowman.cs class:
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;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using CameraViewer;
namespace MGH10_Skybox
{
public class Snowman : DrawableGameObject
{
private Camera cam = new Camera();
Model snowMan;
Matrix[] snowManMatrix;
protected override void LoadContent()
{
//snowMan = Content.Load<Model>( "Models\\snowman" );
snowManMatrix = new Matrix[snowMan.Bones.Count];
snowMan.CopyAbsoluteBoneTransformsTo(snowManMatrix);
}
public void DrawSnowMan(Model model, GameTime gameTime)
{
foreach (ModelMesh mesh in model.Meshes)
{
Matrix world, scale, translation;
scale = Matrix.CreateScale(0.02f, 0.02f, 0.02f);
translation = Matrix.CreateScale(0.0f, 0.7f, -4.0f);
world = scale * translation;
foreach (BasicEffect effect in mesh.Effects)
{
effect.World = snowManMatrix[mesh.ParentBone.Index] * world;
effect.View = cam.viewMatrix;
effect.Projection = cam.projectionMatrix;
effect.EnableDefaultLighting();
}
mesh.Draw();
}
}
protected override void Draw(GameTime gameTime)
{
DrawSnowMan(snowMan, gameTime);
}
}
}
My issue is that I keep getting a compiler error that says “The type or namespace name ‘DrawableGameObject’ could not be found (are you missing a using directive or assembly reference?)”.
I have tried doing all kinds of research on google and I get almost no results for DrawableGameObject, but anything that does talk about it (or at least says it in the page preview) usually ends up using DrawableGameComponent instead.
I’ve tried using DrawableGameComponent, but then I get even more compiler errors which lead me to believe that it truly is not what I’m looking for. Specifically, the error states that “DrawableGameComponent does not contain a constructor that takes ‘0’ arguments” (which goes against how my class and its calls were constructed).
The one gleam of hope that I had was that ‘DrawableGameObject’ is supposedly a .net 4.0 class, and I had been using XNA 3.1 w/ Visual C# 2008 (which does not support 4.0 apparently). So I downloaded XNA 4.0 and Visual C# Express 2010 with high hopes that crashed and burned when I went to change the target Framework only to find the option completely grayed out (even when starting a completely new project).
All help is GREATLY appreciated.
It seems as though you will actually want “DrawableGameComponent” instead, as I couldn’t find anything about a “DrawableGameObject” class. The error you’re getting when trying to inherit from DrawableGameComponent can be fixed by calling the base constructor with the appropriate parameter during your classes constructor, as shown below:
“game” in the base constructor is supposed to be the Game object that the DrawableGameComponent is attached to.
Edit:
Based on your latest comment, I’d recommend adding this constructor to your class (that is, replace what I previously suggested with this constructor):
And in your Game1.cs, in your LoadContent function or wherever you’re currently calling “snowMan = Content.Load ( “Models\snowman”);”, try this instead:
This will create a new Snowman instance with the “Model snowman” field initialized.