I have downloaded several tutorials for XNA / C# and in almost every one, it is breaking on this line and telling me I am making an invalid method call.
I think I am using XNA Beta 2.
//Initialize the SpriteBatch object used to draw the sprites
mSpriteBatch = new SpriteBatch(graphics.GraphicsDevice);
Here are some details on the exception:
System.InvalidOperationException was unhandled
Message="The method call is invalid."
Source="Microsoft.Xna.Framework"
StackTrace:
at Microsoft.Xna.Framework.Graphics.VertexShader..ctor(GraphicsDevice graphicsDevice, Byte[] shaderCode)
at Microsoft.Xna.Framework.Graphics.SpriteBatch.ConstructPlatformData()
at Microsoft.Xna.Framework.Graphics.SpriteBatch..ctor(GraphicsDevice graphicsDevice)
at WindowsGameDELETE_ME.Game1.LoadGraphicsContent(Boolean loadAllContent) in C:\Documents and Settings\Thomas Knowlton\Desktop\WindowsGameDELETE_ME\WindowsGameDELETE_ME\Game1.cs:line 78
at Microsoft.Xna.Framework.Game.Initialize()
at WindowsGameDELETE_ME.Game1.Initialize() in C:\Documents and Settings\Thomas Knowlton\Desktop\WindowsGameDELETE_ME\WindowsGameDELETE_ME\Game1.cs:line 61
at Microsoft.Xna.Framework.Game.Run()
at WindowsGameDELETE_ME.Program.Main(String[] args) in C:\Documents and Settings\Thomas Knowlton\Desktop\WindowsGameDELETE_ME\WindowsGameDELETE_ME\Program.cs:line 14
Here is the code from the tutorial giving the exception:
#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion
namespace WindowsGameDELETE_ME
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
ContentManager content;
//The 2D textured to be rendered
Texture2D mTexture;
//The texture start position for the rectangle
int mTextureStartX = 0;
//The object to draw the sprite
SpriteBatch mSpriteBatch;
//The X and Y coordinates to position the Sprite
int mSpriteX = 0;
int mSpriteY = 0;
//The horizontal and vertical movement speeds for the sprite
int mHorizontalSpeed = 3;
int mVerticalSpeed = 3;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
content = new ContentManager(Services);
}
/// <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();
}
/// <summary>
/// Load your graphics content. If loadAllContent is true, you should
/// load content from both ResourceManagementMode pools. Otherwise, just
/// load ResourceManagementMode.Manual content.
/// </summary>
/// <param name="loadAllContent">Which type of content to load.</param>
protected override void LoadGraphicsContent(bool loadAllContent)
{
if (loadAllContent)
{
// TODO: Load any ResourceManagementMode.Automatic content
//Initialze the sprite batch object. This will be used to draw the sprite
mSpriteBatch = new SpriteBatch(this.graphics.GraphicsDevice);
//Load the image into the texture object
ContentManager aLoader = new ContentManager(Services);
mTexture = aLoader.Load<Texture2D>("JumpingJelly") as Texture2D;
}
// TODO: Load any ResourceManagementMode.Manual content
}
/// <summary>
/// Unload your graphics content. If unloadAllContent is true, you should
/// unload content from both ResourceManagementMode pools. Otherwise, just
/// unload ResourceManagementMode.Manual content. Manual content will get
/// Disposed by the GraphicsDevice during a Reset.
/// </summary>
/// <param name="unloadAllContent">Which type of content to unload.</param>
protected override void UnloadGraphicsContent(bool unloadAllContent)
{
if (unloadAllContent == true)
{
content.Unload();
}
}
/// <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 default game to exit on Xbox 360 and Windows
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
//Check for keyboard input
CheckKeyboardInput();
//Update the Sprite
UpdateSprite();
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);
// TODO: Add your drawing code here
//Begin drawing the scene. Using AlphaBlend as the blend mode will use the transparencies of the images drawn.
mSpriteBatch.Begin(SpriteBlendMode.AlphaBlend);
//Draw the sprite
mSpriteBatch.Draw(mTexture, new Rectangle(mSpriteX, mSpriteY, 50, 50), new Rectangle(mTextureStartX, 0, 256, 256), Color.White);
mSpriteBatch.End();
base.Draw(gameTime);
}
//Update the Sprite
void UpdateSprite()
{
//Auto-move the sprite by speed
mSpriteX += mHorizontalSpeed;
mSpriteY += mVerticalSpeed;
//Check if the sprite has collided with a boundary
if (CheckBoundaryCollision() == true)
{
//Move to the next frame of the sprite when a collision occurs
ChangeSpriteFrame();
}
}
//Check to see if the Sprite has collided with a boundary
protected Boolean CheckBoundaryCollision()
{
//Determine if a boundary collision occurred
Boolean aCollision = false;
//Set the max and min for the X boundaries of the area the sprite can move within
int aMaxX = Window.ClientBounds.Width - 50;
int aMinX = 0;
//Set the max and min for the Y boundaries of the area the sprite can move within
int aMaxY = Window.ClientBounds.Height - 50;
int aMinY = 0;
//Check to see if the sprite has moved out of the allowed area
if (mSpriteX > aMaxX)
{
mHorizontalSpeed *= -1;
mSpriteX = aMaxX;
aCollision = true;
}
else if (mSpriteX < aMinX)
{
mHorizontalSpeed *= -1;
mSpriteX = aMinX;
aCollision = true;
}
if (mSpriteY > aMaxY)
{
mVerticalSpeed *= -1;
mSpriteY = aMaxY;
aCollision = true;
}
else if (mSpriteY < aMinY)
{
mVerticalSpeed *= -1;
mSpriteY = aMinY;
aCollision = true;
}
return aCollision;
}
//Move the sprite to the next frame
protected void ChangeSpriteFrame()
{
if (mTextureStartX == 0)
{
mTextureStartX = 256;
}
else
{
mTextureStartX = 0;
}
}
//Check for keyboard input
protected void CheckKeyboardInput()
{
//Get the current state of the keyboard
KeyboardState aKeyboard = Keyboard.GetState();
//Get the current keys being pressed
Keys[] aCurrentKeys = aKeyboard.GetPressedKeys();
//Cycle through all of the keys being pressed and move the sprite accordingly
for (int aCurrentKey = 0; aCurrentKey < aCurrentKeys.Length; aCurrentKey++)
{
//Change the sprite's direction to the left and move it left
if (aCurrentKeys[aCurrentKey] == Keys.Left)
{
if (mHorizontalSpeed > 0)
{
mHorizontalSpeed *= -1;
}
mSpriteX += mHorizontalSpeed;
}
//Change the sprite's direction to the right and move it right
if (aCurrentKeys[aCurrentKey] == Keys.Right)
{
if (mHorizontalSpeed < 0)
{
mHorizontalSpeed *= -1;
}
mSpriteX += mHorizontalSpeed;
}
//Change the sprite's direction to up and move it up
if (aCurrentKeys[aCurrentKey] == Keys.Up)
{
if (mVerticalSpeed > 0)
{
mVerticalSpeed *= -1;
}
mSpriteY += mVerticalSpeed;
}
//Change the sprite's direction to down and move it down
if (aCurrentKeys[aCurrentKey] == Keys.Down)
{
if (mVerticalSpeed < 0)
{
mVerticalSpeed *= -1;
}
mSpriteY += mVerticalSpeed;
}
//Exit the game if the Escape key has been pressed
if (aCurrentKeys[aCurrentKey] == Keys.Escape)
{
this.Exit();
}
}
}
}
}