locked
XNA 2.0 current context error RRS feed

  • Question

  • Hi,

     im new to the whole xna game platform, and yes im using 2.0(my comp couldn't run 4.0), and i have the code;     

    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

    Ultimage_King_Shooter

    {

     

    /// <summary>

     

     

    /// This is the main type for your game
     

     

    /// </summary>
     

     

    public class Game1 : Microsoft.Xna.Framework.Game
     

    {

     

    GraphicsDeviceManager graphics;

     

    SpriteBatch MainSpriteBatch;

     

    Texture2D Background;

     

    Texture2D AvatarImages;

     

    public Game1()

    {

    graphics =

    new GraphicsDeviceManager(this);

    Content.RootDirectory =

    "Content";

    }

     

    /// <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

     

    this.graphics.PreferredBackBufferHeight = 800;

     

    this.graphics.PreferredBackBufferWidth = 600;

     

    this.Window.Title = "Ultimage King Shooter";

     

    int BorderTop = 62;

     

    int BorderLeft = 5;

     

    int BorderBottom = 548;

     

    int BorderRight = 750;

     

     

    //walk animation

     

    int iWalkAnimationFrame = 0;

     

    //the current frame of animation

     

    int iWalkAnimationFrameCount = 4;

     

    //the total number of frames in out animation

     

    int iWalkAnimationFrameoffset = 60;

     

    //the number of pixels to offset when pulling the main character animation

     

    int iWalkAnimationRow = 0;

     

    //the row of the animation to find, indicating the direction of the player

     

    int iWalkSpeed = 8;

     

    base.Initialize();

    }

     

    /// <summary>

     

    /// LoadContent will be called once per game and is the place to load

     

    /// all of your content.

     

    /// </summary>

     

    protected override void LoadContent()

    {

     

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

    MainSpriteBatch =

    new SpriteBatch(GraphicsDevice);

     

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

    }

     

    protected override void LoadGraphicsContent(bool loadAllContent)

    {

     

    if (loadAllContent)

    {

     

    this.MainSpriteBatch = new SpriteBatch(graphics.GraphicsDevice);

     

    this.Background = Content.Load<Texture2D> ("Backround2");

     

     

    //player location

     

    Rectangle recPlayerLocation;

    }

     

     

    //TODO: Load any ResourceManagementMode.Manual content

    }

     

    private void SpriteBatch()

    {

     

    throw new Exception("The method or operation is not implemented.");

    }

     

    /// <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();

     

    //button press delay

     

    float fKeyPressCheckDelay = 0.25f;

     

    float fTotalElaspedTime = 0;

     

     

    // TODO: Add your update logic here

     

    float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;

    fTotalElaspedTime += elapsed;

    ksKeyboardState =

    Keyboard.GetState();

     

    //process if enough time has elapsed

     

    if (fTotalElaspedTime >= fKeyPressCheckDelay)

    {

     

    if(ksKeyBoardState.IsKeyDown(Keys.Up))

    {

    iWalkAnimationFrame += 1;

    iWalkAnimationRow = 0;

     

    if (iWalkAnimationFrame >= WalkAnimationFrameCount) ;

    {

    iWalkAnimationFrame = 0;

    }

     

    if (recPlayerLocation.Y - iWalkSpeed > borderTop) ;

    {

    recPlayerLocation.Y = iWalkSpeed;

    }

    fTotalElaspedTime = 0.0f;

    }

     

    else if(ksKeyBoardState.IsKeyDown(Keys.Left));

    }

     

     

    base.Update(gameTime);

    }

     

    /// <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);

     

    this.MainSpriteBatch.Begin(SpriteBlendMode.AlphaBlend);

     

    Vector2 position = new Vector2(0, 0);

     

    this.MainSpriteBatch.Draw(Background, position, Color.White);

    MainSpriteBatch.End();

     

     

    // TODO: Add your drawing code here

     

    base.Draw(gameTime);

    }

    }

    }

    im using the tutorial at http://joshua-w-wise78.livejournal.com/ and i keep getting the error; The name 'ksKeyBoardState' does not 
    exist in the current context. if anyone could solve my problem, thank you vey much, btw idk why my fonts blue lol. 

    thats what i have so far and and i have 13 errors, stated below.....

     Error 15 The name 'ksKeyboardState' does not exist in the current context C:\Documents and Settings\Brandon\Desktop\Ultimage King Shooter\Ultimage King Shooter\Game1.cs 127 15 Ultimage King Shooter
    Error 16 The name 'ksKeyBoardState' does not exist in the current context C:\Documents and Settings\Brandon\Desktop\Ultimage King Shooter\Ultimage King Shooter\Game1.cs 132 22 Ultimage King Shooter
    Error 17 The name 'iWalkAnimationFrame' does not exist in the current context C:\Documents and Settings\Brandon\Desktop\Ultimage King Shooter\Ultimage King Shooter\Game1.cs 134 23 Ultimage King Shooter
    Error 18 The name 'iWalkAnimationRow' does not exist in the current context C:\Documents and Settings\Brandon\Desktop\Ultimage King Shooter\Ultimage King Shooter\Game1.cs 135 23 Ultimage King Shooter
    Error 19 The name 'iWalkAnimationFrame' does not exist in the current context C:\Documents and Settings\Brandon\Desktop\Ultimage King Shooter\Ultimage King Shooter\Game1.cs 136 27 Ultimage King Shooter
    Error 20 The name 'WalkAnimationFrameCount' does not exist in the current context C:\Documents and Settings\Brandon\Desktop\Ultimage King Shooter\Ultimage King Shooter\Game1.cs 136 50 Ultimage King Shooter
    Error 21 The name 'iWalkAnimationFrame' does not exist in the current context C:\Documents and Settings\Brandon\Desktop\Ultimage King Shooter\Ultimage King Shooter\Game1.cs 138 27 Ultimage King Shooter
    Error 22 The name 'recPlayerLocation' does not exist in the current context C:\Documents and Settings\Brandon\Desktop\Ultimage King Shooter\Ultimage King Shooter\Game1.cs 140 27 Ultimage King Shooter
    Error 23 The name 'iWalkSpeed' does not exist in the current context C:\Documents and Settings\Brandon\Desktop\Ultimage King Shooter\Ultimage King Shooter\Game1.cs 140 49 Ultimage King Shooter
    Error 24 The name 'borderTop' does not exist in the current context C:\Documents and Settings\Brandon\Desktop\Ultimage King Shooter\Ultimage King Shooter\Game1.cs 140 62 Ultimage King Shooter
    Error 25 The name 'recPlayerLocation' does not exist in the current context C:\Documents and Settings\Brandon\Desktop\Ultimage King Shooter\Ultimage King Shooter\Game1.cs 142 27 Ultimage King Shooter
    Error 26 The name 'iWalkSpeed' does not exist in the current context C:\Documents and Settings\Brandon\Desktop\Ultimage King Shooter\Ultimage King Shooter\Game1.cs 142 49 Ultimage King Shooter
    Error 27 The name 'ksKeyBoardState' does not exist in the current context C:\Documents and Settings\Brandon\Desktop\Ultimage King Shooter\Ultimage King Shooter\Game1.cs 146 27 Ultimage King Shooter

    • Edited by person2 Sunday, January 22, 2012 4:45 PM
    Sunday, January 22, 2012 4:18 PM

Answers

  • You need to have the variables declared somewhere. ksKeyBoardState does not seem to be declared anywhere. Some of the other variables (e.g. the iWalk... ones) are declared in one method but also used in other methods. You cannot do that; declaring them in a method makes them local to the method. So either declare them at class level, or declare them in each method.
    Regards David R
    ---------------------------------------------------------------
    Every program eventually becomes rococo, and then rubble. - Alan Perlis
    The only valid measurement of code quality: WTFs/minute.
    Sunday, January 22, 2012 5:49 PM

All replies

  • You need to have the variables declared somewhere. ksKeyBoardState does not seem to be declared anywhere. Some of the other variables (e.g. the iWalk... ones) are declared in one method but also used in other methods. You cannot do that; declaring them in a method makes them local to the method. So either declare them at class level, or declare them in each method.
    Regards David R
    ---------------------------------------------------------------
    Every program eventually becomes rococo, and then rubble. - Alan Perlis
    The only valid measurement of code quality: WTFs/minute.
    Sunday, January 22, 2012 5:49 PM
  • Hi person2,

    Welcome to the MSDN forum!

    Please declare the ksKeyBoardState before using it. The following statement :
    ksKeyboardState = Keyboard.GetState();  

    may be: KeyboardState  ksKeyboardState = Keyboard.GetState();

    Please refer to the following link:

    Detecting a Key Press (Windows, Windows Phone, Xbox 360)
    http://msdn.microsoft.com/en-us/library/bb203902.aspx 

     

    Meanwhile, for XNA question, you may try the dedicated XNA forums:

    http://forums.create.msdn.com/forums/

    Hope it helps.

    Have a nice day!

    yoyo


    Yoyo Jiang[MSFT]
    MSDN Community Support | Feedback to us
    Tuesday, January 24, 2012 5:23 AM
    Moderator