Getting Started with XNA Game Studio 3.0 to develop video games Part IX.
Uditha BandaraThis month we’ll discuss about code and steps to use VB.NET code in a XNA Game.
First you need to create new XNA project and VB.NET class library project.
Then add a 2D image to the content folder.

First in the VB.NET class library write this code.
Public Class TextureData
Public x As Integer = 120 'Set X
potition of the rectangle as 120
Public y As Integer = 120 'Set Y potition of the rectangle as 120
Public height As Integer = 120 'Set height
of the rectangle as 120
Public width As Integer = 120 'Set width of
the rectangle as 120
Then add that project reference to the main XNA project.
By selecting the VB.NET library.


Then in the XNA project write this code to complete the solution.
In the Game1.cs file initialize these variables.
Texture2D mytx; //Initilize Texture
variable
int val = 0; //Set val varible
to zero
Texture_data.TextureData
tdata = new TextureData();
// Get a projet refarence from VB.NET project.
Now in the LoadContent() method write code for
loading the assest
mytx
= Content.Load<Texture2D>("BurnoutParadise");
// Load the image
In the Draw () method you can see the Changes by drawing the images and models.
val = val + 1; //Increase the val
spriteBatch.Begin(); //Begin the spitebatch process
spriteBatch.Draw(mytx, new Rectangle(tdata.x + val, tdata.y, tdata.width,
tdata.height), Color.White);
//Load the rectangle data from VB class and set in the Draw
Methord
spriteBatch.End(); //End the spitebatch process
Now you can run the project by pressing F5 or by clicking the run button.

Final code of Game1.cs
/*
content
created by -Uditha Sampath Bandara
udithamail@yahoo.com
uditha.wordpress.com
*/
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;
using Texture_data;
namespace Using_VB_in_XNA
{
/// <summary>
/// This is the main type
for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D mytx;
//Initilize Texture variable
int val = 0; //Set val varible to zero
Texture_data.TextureData tdata = new
TextureData();
// Get a projet refarence from VB.NET project.
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()
{
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.
spriteBatch
= new SpriteBatch(GraphicsDevice);
mytx =
Content.Load<Texture2D>("BurnoutParadise"); // Load the image
}
/// <summary>
/// UnloadContent will be
called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
}
/// <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();
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)
{
GraphicsDevice.Clear(Color.Black);
val = val +
1; //Increase
the val
spriteBatch.Begin(); //Begin the
spitebatch process
spriteBatch.Draw(mytx, new Rectangle(tdata.x + val, tdata.y, tdata.width,
tdata.height), Color.White);
//Load the rectangle data from VB class and set in the Draw
Methord
spriteBatch.End(); //End the
spitebatch process
base.Draw(gameTime);
}
}
}
This is the end of the using VB.NET in a XNA game tutorial.
Also source project is attached with this article. (http://digit.lk/downloads/Dec09/UsingVBinXNA-dec09.rar)
Form the next tutorial you’ll learn about adding a Video to an XNA game.


