Reading values from window-form to XNA
-
I´m working on a game using XNA and C#. It´s a kind of two (or more) uboat-game where you place an uboat somewhere and then the opponent are going to find the uboat. The game is maybe going to be an online-game or maybe between two computers. I also need chat but that can be a separat program. Anyway, right now I have a form which contains a picturebox where the actual game are. I have a button in the form and would like to be able to affect the sprites using that button. Right now the uboat is placed in the upper left corner. Then I want to move the uboat to destination of my choice. However I can't affect hte uboat (enemy) with the button once the game has started. Is it even possible? I'm quite new to XNA and I have used this a tutorial: Here´s my code: Program:
using System;
namespace WindowsGame1
{
#if WINDOWS || XBOX
static class Program
{
/// /// The main entry point for the application.
///
static void Main(string[] args)
{
Form1 form = new Form1();
form.Show();
Game1 game = new Game1(form.getDrawSurface());
//game.buttonHit(400,500, true);
game.Run();
}
}
#endif
}Form1:
public partial class Form1 : Form { public Form1() { InitializeComponent(); } Game1 game1 = new Game1(); int counter = 1; public IntPtr getDrawSurface() { return pctSurface.Handle; } private void button1\_Click(object sender, EventArgs e) { game1.buttonHit(350, 450, true); counter++; label1.Text = counter.ToString(); } }
}
Game1
public class Game1 : Microsoft.Xna.Framework.Game { private GraphicsDeviceManager graphics; // Används för att hantera grafik private SpriteBatch spriteBatch; // Används för att rita bilder private Player player; // Spelarens objekt private Player player2; // Spelarens objekt private Enemy enemy; private Texture2D backgroundTexture; int screenWidth; int screenHeight; GraphicsDevice device; Rectangle textBox; private IntPtr drawSurface; int x; int y; bool isActiv
-
I´m working on a game using XNA and C#. It´s a kind of two (or more) uboat-game where you place an uboat somewhere and then the opponent are going to find the uboat. The game is maybe going to be an online-game or maybe between two computers. I also need chat but that can be a separat program. Anyway, right now I have a form which contains a picturebox where the actual game are. I have a button in the form and would like to be able to affect the sprites using that button. Right now the uboat is placed in the upper left corner. Then I want to move the uboat to destination of my choice. However I can't affect hte uboat (enemy) with the button once the game has started. Is it even possible? I'm quite new to XNA and I have used this a tutorial: Here´s my code: Program:
using System;
namespace WindowsGame1
{
#if WINDOWS || XBOX
static class Program
{
/// /// The main entry point for the application.
///
static void Main(string[] args)
{
Form1 form = new Form1();
form.Show();
Game1 game = new Game1(form.getDrawSurface());
//game.buttonHit(400,500, true);
game.Run();
}
}
#endif
}Form1:
public partial class Form1 : Form { public Form1() { InitializeComponent(); } Game1 game1 = new Game1(); int counter = 1; public IntPtr getDrawSurface() { return pctSurface.Handle; } private void button1\_Click(object sender, EventArgs e) { game1.buttonHit(350, 450, true); counter++; label1.Text = counter.ToString(); } }
}
Game1
public class Game1 : Microsoft.Xna.Framework.Game { private GraphicsDeviceManager graphics; // Används för att hantera grafik private SpriteBatch spriteBatch; // Används för att rita bilder private Player player; // Spelarens objekt private Player player2; // Spelarens objekt private Enemy enemy; private Texture2D backgroundTexture; int screenWidth; int screenHeight; GraphicsDevice device; Rectangle textBox; private IntPtr drawSurface; int x; int y; bool isActiv
Change Form1 to this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}Game1 game1; int counter = 1; public Game1 Game { get { return game1; } set { game1 = value; } } public IntPtr getDrawSurface() { return pctSurface.Handle; } private void button1\_Click(object sender, EventArgs e) { game1.buttonHit(350, 450, true); counter++; label1.Text = counter.ToString(); } }
}
Then change Program.cs to:
static void Main(string[] args)
{
Form1 form = new Form1();Game1 game = new Game1(form.getDrawSurface()); form.Game = game; form.Show(); //game.buttonHit(400,500, true); game.Run(); }
Can you see why? You create a game in the Program.cs file, and you create another one in the Form1.cs file. Even though the names are the same, they are not the same game, essentially you have two Game1 objects created. The one you are talking to in Form1 is not the one you created when the program starts. Somehow you need to give Form1 a reference to the game you created. This is one way, there are others, for example, you could also do this: Form1.cs:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}int counter = 1; public IntPtr getDrawSurface() { return pctSurface.Handle; } private void button1\_Click(object sender, EventArgs e) { Program.game.buttonHit(350, 450, true); counter++; label1.Text = counter.ToString(); } }
}
Then change Program.cs to:
static Game1 game;
static void Main(string[] args)
{
Form1 form = new Form1();
form.Show();
game = new Game1(form.getDrawSurface());
//game.buttonHit(400,500, true);
game.Run();
}There are many ways to skin a cat, but these are two of the easiest.
-
Change Form1 to this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}Game1 game1; int counter = 1; public Game1 Game { get { return game1; } set { game1 = value; } } public IntPtr getDrawSurface() { return pctSurface.Handle; } private void button1\_Click(object sender, EventArgs e) { game1.buttonHit(350, 450, true); counter++; label1.Text = counter.ToString(); } }
}
Then change Program.cs to:
static void Main(string[] args)
{
Form1 form = new Form1();Game1 game = new Game1(form.getDrawSurface()); form.Game = game; form.Show(); //game.buttonHit(400,500, true); game.Run(); }
Can you see why? You create a game in the Program.cs file, and you create another one in the Form1.cs file. Even though the names are the same, they are not the same game, essentially you have two Game1 objects created. The one you are talking to in Form1 is not the one you created when the program starts. Somehow you need to give Form1 a reference to the game you created. This is one way, there are others, for example, you could also do this: Form1.cs:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}int counter = 1; public IntPtr getDrawSurface() { return pctSurface.Handle; } private void button1\_Click(object sender, EventArgs e) { Program.game.buttonHit(350, 450, true); counter++; label1.Text = counter.ToString(); } }
}
Then change Program.cs to:
static Game1 game;
static void Main(string[] args)
{
Form1 form = new Form1();
form.Show();
game = new Game1(form.getDrawSurface());
//game.buttonHit(400,500, true);
game.Run();
}There are many ways to skin a cat, but these are two of the easiest.
-
Great, thanks! The first method works fine but can you explain what you do in the second?
Program is a static class, meaning it is available to the entire application. By adding a static variable inside this class, you also make it available to the entire application. You initialize the game, then create the form. The form, or any other form for that matter can access Program.Game to get an instance of the running game and manipulate it. Its the same as the first one, except it changes where the reference to the game is contained.
-
Program is a static class, meaning it is available to the entire application. By adding a static variable inside this class, you also make it available to the entire application. You initialize the game, then create the form. The form, or any other form for that matter can access Program.Game to get an instance of the running game and manipulate it. Its the same as the first one, except it changes where the reference to the game is contained.
-
Program is a static class, meaning it is available to the entire application. By adding a static variable inside this class, you also make it available to the entire application. You initialize the game, then create the form. The form, or any other form for that matter can access Program.Game to get an instance of the running game and manipulate it. Its the same as the first one, except it changes where the reference to the game is contained.
Hi again. I can now change values in the winforms that affects the game, so thank you very much for that. How can I do the opposite (if possible) affect the winform from my game. Let say I want to make a button enabled or disabled when something in my game happends.