Reputation: 366
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.
Upvotes: 1
Views: 1446
Reputation: 111
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:
public Snowman(Game game) : base(game)
{
//Whatever goes here!
}
"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):
public Snowman(Game game, Model snowman) : base(game)
{
this.snowman = snowman;
}
And in your Game1.cs, in your LoadContent function or wherever you're currently calling "snowMan = Content.Load ( "Models\snowman");", try this instead:
snowMan = new Snowman(this, Content.Load<Model>("Models\\snowman");
This will create a new Snowman instance with the "Model snowman" field initialized.
Upvotes: 2
Reputation: 4280
So... if you can't find the class, make your own!
Anywho, I guess DrawableGameObject was supposed to make it easy to update and draw different types of objects in the game engine, but if you're using it only for the snowman (which I would guess since you're not mentioning other code not working..) you might as well not have it!
So remove the inheritance for that class, and remove the override from those methods. Hide the DrawSnowMan method (i.e. make that private), and make the LoadContent and Draw public, and call them from respective methods in your Game class!
Also, I would suggest building in a mechanism to draw your snowman on different locations, or if you want to have multiple instances of the SnowMan class, move the Model somewhere else (they are gonna share it, so you could save some memory by not duplicating it unnecessarily).
Hope I helped some, although if you were looking for an answer that involved finding the DrawableGameObject class then sorry.
Upvotes: 0