Ace Caserya
Ace Caserya

Reputation: 2925

gameTime of Microsoft.Xna.Framework not working in C#

I'm using C# 2010 .net 4.0 on XP.

I'm getting an error on using gameTime, it is as follows: The name 'gameTime' does not exist in the current context

I've downloaded (xnafx40_redist.msi) and added the Microsoft.Xna.Framework from the reference and used it on my code:

    using System;
    using System.Windows.Forms;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Audio;
    using Microsoft.Xna.Framework.Content;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;

    namespace WindowsFormsApplication1
    {

        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {

                int counter = 1;
                int limit = 50;
                float countDuration = 2f; 
                float currentTime = 0f;

                //Error on gameTime
                currentTime += (float)gameTime.ElapsedGameTime.TotalSeconds; 

                if (currentTime >= countDuration)
                {
                    counter++;
                    currentTime -= countDuration; 
                }
                if (counter >= limit)
                {
                    counter = 0;
                }
            }
        }
    }

I also tried to declare GameTime to variable but it is also out of scope. Is it something to do with XNA framework i copied? or XP? Or .net version? Does the code require a object or something? I'm new in XNA solutions.


Additional info: it seems the 'Game' when i declare Microsoft.Xna.Framework.Game using other code is also not working. It is saying The type or namespace 'Game' does not exist.

Upvotes: 0

Views: 728

Answers (2)

jrbeverly
jrbeverly

Reputation: 1621

I am not exactly sure where you are trying to retrieve your gameTime variable from?

If you are trying to retrieve it from a [Microsoft.Xna.Framework.Game][1] instance? Or do you have a special control you have added to your Form that includes the Game object?

I am not sure I am able to help but these resources might be of some assistance:

Upvotes: 1

rutter
rutter

Reputation: 11452

You're probably used to accessing the gameTime parameter that's passed to XNA's Game.Update() method. You don't appear to be in that method. Perhaps you can instead cache some timing information during each game update, then reference that cached information in your button callback?

If you're not at all sure what I'm talking about, find an example project and comb through it a bit.

As an aside, you might want to use a tutorial for combining Windows Forms with XNA. From what I gather, it's possible but not necessarily straightforward.

Upvotes: 1

Related Questions