Author Topic: C# help  (Read 1246 times)

Offline DuckRA2

  • *
  • Posts: 1006
  • Rep: 1
    • View Profile
    • Awards
C# help
« on: September 27, 2011, 08:06:19 PM »
I am trying to do some basic tests in XNA which is for C#. I have done some tutorials like drawing sprites and displaying a 3d model. I usually like experimenting with code outside the tutorials. It has been pissing the heck off of me that I can't figure out variables! I think I understand that Variables can be initialized someplace but then are hidden everywhere else. Maybe I am just too used to scripting in game maker, it made stuff like that easy. But it's time to move off game maker and do something real. Basically all I want to do is create a variable, assign it a value, and then add it to the camera's x position. In visual basic adding variables was as simple as +thevariable I believe. But here it is different? Anyways here is my code.







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;


namespace _3dtest
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;


        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            int poohead = 12;    <- here is my variable created and assigned a value of 12
        }


        /// <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>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here


            base.Initialize();
        }
        Model xnatestmodel;
        float aspectratio;
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);


            xnatestmodel = Content.Load<Model>("xnatestmodel");
            aspectratio = graphics.GraphicsDevice.Viewport.AspectRatio;


            // TODO: use this.Content to load your game content here
        }


        /// <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 (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();


            // TODO: Add your update logic here


            base.Update(gameTime);
        }
        Vector3 modelPosition = Vector3.Zero;
        Vector3 cameraPosition = new Vector3(0.0f+poohead, 14.0f, 122.0f);  <-- here is where I try to add it to the camera's x




        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Gray);


            foreach (ModelMesh mesh in xnatestmodel.Meshes)
                 foreach (BasicEffect effect in mesh.Effects)
        {
            effect.EnableDefaultLighting();
            effect.View = Matrix.CreateLookAt(cameraPosition, Vector3.Zero, Vector3.Up);
            effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f),
            aspectratio, 1.0f, 10000.0f);
            mesh.Draw();
        }


            base.Draw(gameTime);
        }
    }
}






help will be greatly appreciated.


EDIT: I think my issue has something to do with those vectors...
« Last Edit: September 28, 2011, 12:35:38 AM by DuckRA2 »

Offline Serge

  • *
  • Posts: 1530
  • Rep: 13
    • View Profile
    • http://www.q3k.org/
    • Awards
Re: C# help
« Reply #1 on: September 28, 2011, 06:07:09 AM »
a) use code tags. Please.
b) If you want your variable to be accessible from other methods of the class, make it a member variable (place its declaration underneath SpriteBatch spriteBatch), set its value in the constructor, and use it in other methods.
home | twitter | yt | gmf de/compiler | component freedom | xmpp: q3k@q3k.org | email: q3k@q3k.org

Offline DuckRA2

  • *
  • Posts: 1006
  • Rep: 1
    • View Profile
    • Awards
Re: C# help
« Reply #2 on: October 05, 2011, 07:49:19 PM »
Thanks. I made some progress from that advice on a little project I am making.