XNA Creators Club Online
Page 1 of 1 (9 items)
Sort Posts: Previous Next

Doing the Going Beyond tutorials and ran into some trouble

Last post 9/2/2008 3:47 AM by MeatBundle. 8 replies.
  • 5/31/2008 9:24 PM

    Doing the Going Beyond tutorials and ran into some trouble

    Ok, Im completly new to C# and XNA, Ive pretty much mastered Visual Basic then moved onto this. Im doing the Going Beyond tutorials where you make the asteroids game, but Im having a problem with this line.:

    Ship ship = new ship();

    I dont know how to fix it, Ive read over the code tons of times and have no clue. I have also changed the code to take keyboard input rateher than a 360 controller which I got to work well. The error Im getting is this, Error 1 The type or namespace name 'Ship' could not be found (are you missing a using directive or an assembly reference?) C:\Users\Scott\Documents\Visual Studio 2008\Projects\WindowsGame1\WindowsGame1\Game1.cs 37 9 WindowsGame1

    Heres my code incase you want to check it over.

    Game1.cs:

    using System;

     

    using System.Collections.Generic;

     

    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.Net;

     

    using Microsoft.Xna.Framework.Storage;

     

    namespace WindowsGame1

    {

    /// <summary>

     

    /// This is the main type for your game

     

    /// </summary>

     

    public class Game1 : Microsoft.Xna.Framework.Game

     

    {

    GraphicsDeviceManager graphics;

    SpriteBatch spriteBatch;

    KeyboardState oldState;

    //Camera/View information

     

    Vector3 cameraPosition = new Vector3(0.0f, 0.0f, -5000.0f);

    float aspectRatio;

    Matrix projectionMatrix;

    Matrix viewMatrix;

    //Audio components

     

    AudioEngine audioEngine;

    WaveBank waveBank;

    SoundBank soundBank;

    //Cue so we can hang onto the soun of the engine

     

    Cue engineSound = null;

    //Visual components

     

    Ship ship = new Ship();

    public Game1()

    {

    graphics = new GraphicsDeviceManager(this);

    Content.RootDirectory = "Content";

    aspectRatio = (float)GraphicsDeviceManager.DefaultBackBufferWidth / GraphicsDeviceManager.DefaultBackBufferHeight;

    }

    protected override void Initialize()

    {

    audioEngine = new AudioEngine("Content\\Audio\\MyGameAudio.xgs");

    waveBank = new WaveBank(audioEngine, "Content\\Audio\\Wave Bank.xwb");

    soundBank = new SoundBank(audioEngine, "Content\\Audio\\Sound Bank.xsb");

     

    projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f),

    aspectRatio, 1.0f, 10000.0f);

    viewMatrix = Matrix.CreateLookAt(cameraPosition, Vector3.Zero, Vector3.Up);

    base.Initialize();

    oldState = Keyboard.GetState();

    }

    private Matrix[ SetupEffectDefaults(Model myModel)

    {

    Matrix[ absoluteTransforms = new Matrix[myModel.Bones.Count];

    myModel.CopyAbsoluteBoneTransformsTo(absoluteTransforms);

    foreach (ModelMesh mesh in myModel.Meshes)

    {

    Effect.EnableDefaultLighting();

    Effect.Projection = projectionMatrix;

    Effect.View = viewMatrix;

    }

    return absoluteTransforms;

    }

     

     

    protected override void LoadContent()

    {

    // Create a new SpriteBatch, which can be used to draw textures.

     

    spriteBatch = new SpriteBatch(GraphicsDevice);

    ship.Model = Content.Load<Model>("Models/p1_wedge");

    ship.Transforms = SetupEffectDefaults(ship.Model);

    }

    protected override void UnloadContent()

    {

    }

    protected override void Update(GameTime gameTime)

    {

    // Allows the game to exit

     

    if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)

    this.Exit();

    // Get some input.

     

    UpdateInput();

    // Update audioEngine.

     

    audioEngine.Update();

    // Add velocity to the current position.

     

    ship.Position += ship.Velocity;

    // Bleed off velocity over time.

     

    ship.Velocity *= 0.95f;

    base.Update(gameTime);

    }

     

     

     

    protected void UpdateInput()

    {

    //Sets the keyboard as controller.

     

    KeyboardState newState = Keyboard.GetState();

    {

    ship.Update(currentState);

    //Rotate the model using the W key.

     

    if (newState.IsKeyDown(Keys.A))

    {

    if (!oldState.IsKeyDown(Keys.A))

    {

    modelRotation = modelRotation + 0.10f;

    }

    else if (oldState.IsKeyDown(Keys.A))

    {

    engineSound.Pause();

    }

    }

    if (newState.IsKeyDown(Keys.D))

    {

    modelRotation = modelRotation - 0.10f;

    }

    else if (oldState.IsKeyDown(Keys.D))

    {

    engineSound.Pause();

    }

    if (newState.IsKeyDown(Keys.W))

    {

    if (!oldState.IsKeyDown(Keys.W))

    {

    Vector3 modelVelocityAdd = Vector3.Zero;

    modelVelocityAdd.X = -(float)Math.Sin(modelRotation);

    modelVelocityAdd.Z = -(float)Math.Cos(modelRotation);

    modelVelocity += modelVelocityAdd * 2;

    }

    if (engineSound == null)

    {

    engineSound = soundBank.GetCue("engine_2");

    engineSound.Play();

    }

    else if (engineSound.IsPaused)

    {

    engineSound.Resume();

    }

    else

     

    {

    if (engineSound != null && engineSound.IsPlaying)

    {

    engineSound.Pause();

    }

    }

    }

    else if (oldState.IsKeyDown(Keys.W))

    {

    engineSound.Pause();

    }

     

    if (newState.IsKeyDown(Keys.S))

    {

    if (!oldState.IsKeyDown(Keys.S))

    {

    Vector3 modelVelocityAdd = Vector3.Zero;

    modelVelocityAdd.X = +(float)Math.Sin(modelRotation);

    modelVelocityAdd.Z = +(float)Math.Cos(modelRotation);

    modelVelocity += modelVelocityAdd * 2;

    }

    else if (oldState.IsKeyDown(Keys.S))

    {

    engineSound.Pause();

    }

    }

     

    if (newState.IsKeyDown(Keys.Space))

    {

    if (!oldState.IsKeyDown(Keys.Space))

    {

    modelPosition = Vector3.Zero;

    modelVelocity = Vector3.Zero;

    modelRotation = 0.0f;

    //Make a sound when we warp.

     

    soundBank.PlayCue("hyperspace_activate");

    if (engineSound.IsPlaying)

    {

    engineSound.Pause();

    }

    }

    else if (oldState.IsKeyDown(Keys.Space))

    {

    }

     

    }

    }}

    private object currentState(Keys keys)

    {

    throw new NotImplementedException();

    }

    protected override void Draw(GameTime gameTime)

    {

    graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

    Matrix shipTransformMatrix = ship.RotationMatrix

    * Matrix.CreateTranslation(ship.Position);

    DrawModel(ship.Model, shipTransformMatrix, ship.Transforms);

    base.Draw(gameTime);

    }

    public void DrawModel(Model model, Matrix modelTransform, Matrix[ absoluteBoneTransforms)

    {

    //Draw the model, a model can have multiple meshes, so loop

     

    foreach (ModelMesh mesh in model.Meshes)

    {

    //This is where the mesh orientation is set

     

    foreach (BasicEffect effect in mesh.Effects)

    {

    effect.World = absoluteBoneTransforms[mesh.ParentBone.Index] * modelTransform;

    }

    //Draw the mesh, will use the effects set above.

     

    mesh.Draw();

    }

    }

    }

    }

     

    Ship.cs:

    using System;

     

    using System.Collections.Generic;

     

    using System.Text;

     

    using Microsoft.Xna.Framework;

     

    using Microsoft.Xna.Framework.Content;

     

    using Microsoft.Xna.Framework.Graphics;

     

    using Microsoft.Xna.Framework.Input;

     

     

    namespace GoingBeyond4_Tutorial

    {

    class Ship

     

    {

    public Model Model;

    public Matrix[ Transforms;

    //Position of the model in world space.

     

    public Vector3 Position = Vector3.Zero;

    //Velocity of the model, applied each frame to the model's position

     

    public Vector3 Velocity = Vector3.Zero;

    public Matrix RotationMatrix = Matrix.Identity;

    private float rotation = 0.0f;

    public float Rotation

    {

    get { return rotation; }

    set

     

    {

    float newVal = value;

    while (newVal >= MathHelper.TwoPi)

    {

    newVal -= MathHelper.TwoPi;

    }

    while (newVal < 0)

    {

    newVal += MathHelper.TwoPi;

    }

    if (rotation != newVal)

    {

    rotation = newVal;

    RotationMatrix = Matrix.CreateRotationY(rotation);

    }

    }

    }

    public void Update(GamePadState controllerState)

    {

    //Rotate the model using the A key and scale it down.

     

    Rotation -= controllerState.ThumbSticks.Left.X * 0.10f;

    //Finally, add this vector to our velocity.

     

    Velocity += RotationMatrix.Forward * 1.0f * controllerState.Triggers.Right;

    }

    }

    }

     

     

    Thanks for any help!

  • 5/31/2008 9:40 PM In reply to

    Re: Doing the Going Beyond tutorials and ran into some trouble

    The ship class is in a GoingBeyond4_Tutorial namespace.

    You can do one of the following:

    1) Alter your game1.cs namespace to GoingBeyond4_Tutorial

    2) Add #using GoingBeyond4_Tutorial to your game1.cs

    3) Use GoingBeyond4_Tutorial.Ship i.e. the fully qualified name of the ship class in the game1.cs

    Ant.
  • 5/31/2008 10:56 PM In reply to

    Re: Doing the Going Beyond tutorials and ran into some trouble

    Antony Kancidrowski:

    The ship class is in a GoingBeyond4_Tutorial namespace.

    You can do one of the following:

    1) Alter your game1.cs namespace to GoingBeyond4_Tutorial

    2) Add #using GoingBeyond4_Tutorial to your game1.cs

    3) Use GoingBeyond4_Tutorial.Ship i.e. the fully qualified name of the ship class in the game1.cs

    None seem to work, but I could just be doing this wrong

  • 6/1/2008 5:13 PM In reply to

    Re: Doing the Going Beyond tutorials and ran into some trouble

    From your post I assumed that ship.cs is in the same project as the game1.cs. If it isn't then you will also need to add the reference of the DLL to the game1.cs project.

    What error are you getting when you make the change?

    Ant.
  • 6/1/2008 8:34 PM In reply to

    Re: Doing the Going Beyond tutorials and ran into some trouble

    Antony Kancidrowski:

    From your post I assumed that ship.cs is in the same project as the game1.cs. If it isn't then you will also need to add the reference of the DLL to the game1.cs project.

    What error are you getting when you make the change?

    When I make the changed I get around 20 errors for each.

  • 6/1/2008 11:25 PM In reply to

    Re: Doing the Going Beyond tutorials and ran into some trouble

    OK, lets start with the first one and work from there. If you get numerous errors first one can cause others. Generally the error messages are quite useful and state exactly what the problem is.

    Without knowing the error(s) I can not help you effictively.

    Ant.
  • 6/5/2008 12:29 AM In reply to

    Re: Doing the Going Beyond tutorials and ran into some trouble

    I'm in the same boat as the topic creator. I was using the tutorials, don't have an XBox controller, so I tried to code in a keyboard. It worked fine until I tried to implement the ship class document. The original error was the same, the

     

    Ship ship = new Ship();

    line warned me I was lacking a reference. Based on your advice, I checked the .cs file, and changed the namespace from the default tutorial to _3dGame, which is the name of my solution / project (I'm still hazy on what a solution is within a project). Anyhow, this introduced a bunch of new errors. Let me print my code:

     Game1.cs

    using System;
    using System.Collections.Generic;
    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.Net;
    using Microsoft.Xna.Framework.Storage;

    namespace _3dgame
    {
        /// <summary>
        /// This is the main type for your game
        /// </summary>
        public class Game1 : Microsoft.Xna.Framework.Game
        {
            GraphicsDeviceManager graphics;
            KeyboardState oldState;
            SpriteBatch spriteBatch;
            Color backColor = Color.CornflowerBlue;

            //Camera/View information
            Vector3 cameraPosition = new Vector3(0.0f, 0.0f, 25000.0f);
            float aspectRatio;
            Matrix projectionMatrix;
            Matrix viewMatrix;

            //Audio components
            AudioEngine audioEngine;
            WaveBank waveBank;
            SoundBank soundBank;
            // Cue so we can hang on to the sound of the engine.
            Cue engineSound = null;

            //Visual components
            Ship ship = new Ship();

            public Game1()
            {
                graphics = new GraphicsDeviceManager(this);
                Content.RootDirectory = "Content";

                aspectRatio = (float)GraphicsDeviceManager.DefaultBackBufferWidth / GraphicsDeviceManager.DefaultBackBufferHeight;
            }

            protected override void Initialize()
            {
                audioEngine = new AudioEngine("Content\\Audio\\MyGameAudio.xgs");
                waveBank = new WaveBank(audioEngine, "Content\\Audio\\Wave Bank.xwb");
                soundBank = new SoundBank(audioEngine, "Content\\Audio\\Sound Bank.xsb");

                projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f),
                    aspectRatio, 20000.0f, 30000.0f);
                viewMatrix = Matrix.CreateLookAt(cameraPosition, Vector3.Zero, Vector3.Up);

                base.Initialize();
            }

            private Matrix[ SetupEffectDefaults(Model myModel)
            {
                Matrix[ absoluteTransforms = new Matrix[myModel.Bones.Count];
                myModel.CopyAbsoluteBoneTransformsTo(absoluteTransforms);

                foreach (ModelMesh mesh in myModel.Meshes)
                {
                    foreach (BasicEffect effect in mesh.Effects)
                    {
                        effect.EnableDefaultLighting();
                        effect.Projection = projectionMatrix;
                        effect.View = viewMatrix;
                    }
                }
                return absoluteTransforms;
            }

            protected override void LoadContent()
            {
                // Create a new SpriteBatch, which can be used to draw textures.
                spriteBatch = new SpriteBatch(GraphicsDevice);

                ship.Model = Content.Load<Model>("Models/p1_wedge");
                ship.Transforms = SetupEffectDefaults(ship.Model);
            }


            /// <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 (newState.IsKeyPressed(Keys.Escape))
                    this.Exit();

                // Get some input.
                UpdateInput();

                // Update audioEngine.
                audioEngine.Update();

                // Add velocity to the current position.
                modelPosition += modelVelocity;

                // Bleed off velocity over time.
                modelVelocity *= 0.95f;

                base.Update(gameTime);
            }

            protected void UpdateInput()
            {
                // Define the keyboard state.
                KeyboardState currentState = currentState;
                {
                    ship.Update(currentState);

                    // Set some audio based on whether we're pressing a trigger.
                    if (currentState.IsKeyDown(Keys.Up))
                    {
                        if (engineSound == null)
                        {
                            engineSound = soundBank.GetCue("engine_2");
                            engineSound.Play();
                        }

                        else if (engineSound.IsPaused)
                        {
                            engineSound.Resume();
                        }
                    }
                    else
                    {
                        if (engineSound != null && engineSound.IsPlaying)
                        {
                            engineSound.Pause();
                        }
                    }

                    // In case you get lost, press Space to warp back to the center.
                    if (currentState.IsKeyDown(Keys.Space))
                    {
                        ship.Position = Vector3.Zero;
                        ship.Velocity = Vector3.Zero;
                        ship.Rotation = 0.0f;

                        // Make a sound when we warp.
                        soundBank.PlayCue("hyperspace_activate");
                    }
                }
            }

            /// <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)
            {
                graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

                Matrix shipTransformMatrix = ship.RotationMatrix
                * Matrix.CreateTranslation(ship.Position);
                DrawModel(ship.Model, shipTransformMatrix, ship.Transforms);
                base.Draw(gameTime);


                base.Draw(gameTime);
            }

            public void DrawModel(Model model, Matrix modelTransform, Matrix[ absoluteBoneTransforms)
            {
                //Draw the model, a model can have multiple meshes, so loop
                foreach (ModelMesh mesh in model.Meshes)
                {
                    //This is where the mesh orientation is set
                    foreach (BasicEffect effect in mesh.Effects)
                    {
                        effect.World = absoluteBoneTransforms[mesh.ParentBone.Index] * modelTransform;
                    }
                    //Draw the mesh, will use the effects set above.
                    mesh.Draw();
                }
            }
        }
    }

     ship.cs

    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Content;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;

    namespace _3dgame
    {
        class Ship
        {
            public Model Model;
            public Matrix[ Transforms;

            //Position of the model in world space
            public Vector3 Position = Vector3.Zero;

            //Velocity of the model, applied each frame to the model's position
            public Vector3 Velocity = Vector3.Zero;

            public Matrix RotationMatrix = Matrix.Identity;
            private float rotation = 0.0f;
            public float Rotation
            {
                get { return rotation; }
                set
                {
                    float newVal = value;
                    while (newVal >= MathHelper.TwoPi)
                    {
                        newVal -= MathHelper.TwoPi;
                    }
                    while (newVal < 0)
                    {
                        newVal += MathHelper.TwoPi;
                    }

                    if (rotation != newVal)
                    {
                        rotation = newVal;
                        RotationMatrix = Matrix.CreateRotationY(rotation);
                    }

                }
            }

            public void Update(GamePadState controllerState)
            {
                // Rotate the model using the left thumbstick, and scale it down.
                Rotation -= controllerState.ThumbSticks.Left.X * 0.10f;

                // Finally, add this vector to our velocity.
                Velocity += RotationMatrix.Forward * 1.0f * controllerState.Triggers.Right;
            }
        }
    }

     

     End Code.

    All my errors exist within the Game1.cs file. The first is the line within the Update section, highlighted blue. "The name 'newState' does not exist in the current context"

      protected override void Update(GameTime gameTime)
            {
                // Allows the game to exit
                if (newState.IsKeyPressed(Keys.Escape))
                    this.Exit();

     I've attempted to map the Esc key to quit the program, which did not work the way I wanted it to. I suspect that I pulled the same help file as the above user, which explained mapping keyboard keys through a process involving newState and oldState to create an if/else statement that would determine when the key was released or pressed. This statement was lost while deleting content to accommodate the ship.cs document.

    A few lines below that, the same error, except for the 'modelPosition' and 'modelVelocity' variables

     

    // Add velocity to the current position.
                modelPosition += modelVelocity;

                // Bleed off velocity over time.
                modelVelocity *= 0.95f;

    I have no idea why these are in error. I think these are defined in the ship.cs file, but without 'model' before the name.

     The last two errors and warning all fall in these lines of code:

    protected void UpdateInput()
            {
                // Define the keyboard state.
                KeyboardState currentState = currentState;
                {
                    ship.Update(currentState);

    The warning just tells me the keyboardState definition is redundant. Before, I was defining the keyboard state to replace the oldState with the newState each cycle, but now I'm not sure that needs to happen. I'm still fuzzy on the simplest way to map keyboard controls.

    Last two errors are complicated. I've copied them here.

    Error    6    The best overloaded method match for '_3dgame.Ship.Update(Microsoft.Xna.Framework.Input.GamePadState)' has some invalid arguments    C:\Documents and Settings\Brian\My Documents\Visual Studio 2008\Projects\3dgame\3dgame\Game1.cs    130    17    3dgame

    Error    7    Argument '1': cannot convert from 'Microsoft.Xna.Framework.Input.KeyboardState' to 'Microsoft.Xna.Framework.Input.GamePadState'    C:\Documents and Settings\Brian\My Documents\Visual Studio 2008\Projects\3dgame\3dgame\Game1.cs    130    29    3dgame

    It seems to me that all the errors but the position and velocity are probably tied to me using the keyboard instead of a controller. I'm curious to hear your comments regarding this, if any. In the meantime, I did successfully manage to control the ship in the previous tutorial, so I'm not totally off base.

     

    Thanks in advance for any assistance you may provide.

     -SirNiko

  • 6/5/2008 1:13 AM In reply to

    Re: Doing the Going Beyond tutorials and ran into some trouble

    My advice would be to become more proficient in C# before continuing, or you are likely to get very frustrated.  These are basic errors which should be obvious to fix.

    'newState' doesn't appear to be a variable in your Update method, nor in the Game1 class, so I'm not sure what you expect it to be referencing.

    Same for modelPosition and modelVelocity... they aren't defined anywhere.

  • 9/2/2008 3:47 AM In reply to

    Re: Doing the Going Beyond tutorials and ran into some trouble

    My guess is that when you changed replacing the oldState w/ the newState each cycle to the way that you are doing it now, you deleted the variable definition, i.e. KeyboardState newState should go at the beginning of the function.

    Same thing with modelVelocity/modelPosition...you have a ship.Velocity and ship.Position, is that what you should be using there instead?

    Error 6 means that the function call ship.Update(currentState) is being passed an invalid argument, meaning the "currentState" variable is wrong. The line above, you are setting KeyboardState currentState = currentState; not sure if that works or not. Like you said, prolly because of switching between the gamepad and keyboard. The function is probably looking for gamepad input (a GamePad.GetState variable not a KeyboardState variable). Would probably have to rewrite the ship.Update function to accept a KeyboardState variable and process it correctly.

Page 1 of 1 (9 items) Previous Next