Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Reading values from window-form to XNA

Reading values from window-form to XNA

Scheduled Pinned Locked Moved C#
csharpgraphicsgame-devtutorialquestion
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    larsp777
    wrote on last edited by
    #1

    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
    
    R 1 Reply Last reply
    0
    • L larsp777

      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
      
      R Offline
      R Offline
      Ron Beyer
      wrote on last edited by
      #2

      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.

      L 1 Reply Last reply
      0
      • R Ron Beyer

        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.

        L Offline
        L Offline
        larsp777
        wrote on last edited by
        #3

        Great, thanks! The first method works fine but can you explain what you do in the second?

        R 1 Reply Last reply
        0
        • L larsp777

          Great, thanks! The first method works fine but can you explain what you do in the second?

          R Offline
          R Offline
          Ron Beyer
          wrote on last edited by
          #4

          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.

          L 2 Replies Last reply
          0
          • R Ron Beyer

            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.

            L Offline
            L Offline
            larsp777
            wrote on last edited by
            #5

            Ok, thanks!

            1 Reply Last reply
            0
            • R Ron Beyer

              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.

              L Offline
              L Offline
              larsp777
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups