Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I press down 'S' on the keyboard, and the character still sits at the same previous position. Why is this?

 

#region Using Clauses
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;  
using Microsoft.DirectX; 
using Microsoft.DirectX.DirectInput; 
#endregion

namespace MDX_Application
{
   public partial class Form1 : Form
   {
       private Device device = null;  // DirectInput device
       private bool running = true;   // Game loop control variable.
       Common PC;

       public Form1()
       {
           InitializeComponent();

           // Make a simple player object.
           PC = new Common(50, 50);
           PC.img = Image.FromFile("C1_Stand.png"); 

           // Set the window style.
           this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
       }

       protected override void OnClosed(EventArgs e)
       {
           running = false;
       }

       protected override void OnPaint(PaintEventArgs e)
       {
           Graphics g = e.Graphics;

           // Draw Player to the Screen
           g.DrawImage(PC.img, PC.x, PC.y); 

           this.Invalidate();
       }

       public void InitializeInput()
       {
           device = new Device(SystemGuid.Keyboard);
           device.SetCooperativeLevel(this, CooperativeLevelFlags.Background |
               CooperativeLevelFlags.NonExclusive);
           device.Acquire();

           while (running)
           {
               UpdateInputState();
               Application.DoEvents(); 
           }
       }

       private void UpdateInputState()
       {  
           foreach (Key k in device.GetPressedKeys())
           {
               if (k == Key.S) { PC.y += 10; }
           }  
       }
   }


   class Common
   {
       public Image img = null;
       public int x, y;

       public Common(int x, int y)
       {
           this.x = x;
           this.y = y;
       }
   }
}

Posted
You might want to see this thread http://www.xtremedotnettalk.com/showthread.php?t=93639. I know that code works because I am using it in a project I'm working on. It gets the input in a different way. I don't know which is more effecient, but that example works.

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

Posted
You might want to see this thread http://www.xtremedotnettalk.com/showthread.php?t=93639. I know that code works because I am using it in a project I'm working on. It gets the input in a different way. I don't know which is more effecient' date=' but that example works.[/quote']

 

Thanks for the input; however, I dramatically changed my code after learning how to apply 3d to 2d. For the input, I was able to do this...

 

       // Call this before showing form. 
       public void InitializeInput()
       {
           // Create our keyboard device
           deviceInput = new Microsoft.DirectX.DirectInput.Device(SystemGuid.Keyboard);
           deviceInput.SetCooperativeLevel(this, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive);
           deviceInput.Acquire(); 
       }

       // Game loop method
       private void UpdateInputState()
       {
           KeyboardState state = deviceInput.GetCurrentKeyboardState();

           if (state[Key.Escape])
               this.Close();
           if (state[Key.S])
               PC.Y += 10;
           if (state[Key.W])
               PC.Y -= 10;
           if (state[Key.A])
               PC.X -= 10;
           if (state[Key.D])
               PC.X += 10; 
       }

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...