4 Replies - 1749 Views - Last Post: 03 November 2011 - 03:28 PM Rate Topic: -----

#1 Kirby8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-November 11

Making an arcade game, trying to use the right/left arrow keys to move

Posted 03 November 2011 - 08:09 AM

Hi guys, I've been re-making a retro arcade game and want to use the left and right arrow keys to move my spaceship left and right respectively. I've been searching on-line for quite some time and tried numerous things to get this to work. Unfortunately the spaceship won't move (the arrow keys aren't detected.
From further reading I see others have had the same problem but I can't find an exact solution. My two separate codes are below, both of which don't move the spaceship. (the space bar works however)

 this.KeyPreview = true;
            this.KeyDown += new KeyEventHandler(frmGame_KeyDown);


code 1
//Private void to fire the missile with the missile timer
        private void fireMissile()
        {
            tmrBirds.Interval = 1;
            tmrMissile.Enabled = true;
        }
        //check for user input/key presses
        void frmGame_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Space)
            {
                fireMissile();
            }
            if (e.KeyCode == Keys.Left)
            {
                if (!(SpaceShip.Left + 50 > this.ClientSize.Width))
                    SpaceShip.Left += 10;
            }
            if (e.KeyCode == Keys.Right)
            {
                if (!(SpaceShip.Left < 0))
                    SpaceShip.Left -= 10;
            }
            
            
               
            
        }




code attempt 2
//check for user input/key presses
        void frmGame_KeyDown(object sender, KeyEventArgs e)
        {
                          
            switch (e.KeyCode)
            {
                case Keys.Space: //if the space bar is pressed, call the fireMissile subroutine
                    fireMissile();
                    break;
                case Keys.Left: //if the left arrow key is pressed, move the spaceship left
                    if (!(SpaceShip.Left + 50 > this.ClientSize.Width))
                        SpaceShip.Left += 10;
                    break;
                case Keys.Right: //if the right arrow key is pressed, move the spaceship right
                    if (!(SpaceShip.Left < 0))
                        SpaceShip.Left -= 10;
                    break;
            }
        }



Any help would be appreciated =)

Is This A Good Question/Topic? 0
  • +

Replies To: Making an arcade game, trying to use the right/left arrow keys to move

#2 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6507
  • View blog
  • Posts: 14,377
  • Joined: 02-June 10

Re: Making an arcade game, trying to use the right/left arrow keys to move

Posted 03 November 2011 - 09:08 AM

In designer check the form properties.
YOu need KeyPreview to be on

I see you are going that programmatically. Good job.

SO you're saying that if you put a breakpoint at line 10 of your first code block it is never hit?
Was This Post Helpful? 0
  • +
  • -

#3 Kirby8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-November 11

Re: Making an arcade game, trying to use the right/left arrow keys to move

Posted 03 November 2011 - 10:01 AM

Well currently I am trying to solve this with the first code, however are u speaking of line 10 on the second code? I have changed the form properties so that keypreview = true and the first code I posted still refused to move the spaceship, I'll put a breakpoint on line 14
Was This Post Helpful? 0
  • +
  • -

#4 Kirby8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-November 11

Re: Making an arcade game, trying to use the right/left arrow keys to move

Posted 03 November 2011 - 10:17 AM

changed keypreview to true on both versions and it has not been successful. I have also placed a breakpoint on line 10 as you said and nothing has happended. It's just strange how the space bar key is detected on both versions and yet the arrow keys aren't. Have you got any ideas?
Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5101
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Making an arcade game, trying to use the right/left arrow keys to move

Posted 03 November 2011 - 03:28 PM

It's because Arrow keys are not sent via KeyPreview.

http://connect.micro...custom-controls

Quote

to handle arrow keys in your user control, you should override ProcessDialogKey in the UserControl.
Protected Overrides Function ProcessDialogKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean

If (keyData = Keys.Down Or keyData = Keys.Up Or keyData = Keys.Left Or keyData = Keys.Right) Then
'this means that the key won't be passed down to the control.
'Do whatever you want here...
Return True
End If
Return MyBase.ProcessDialogKey(keyData)

End Function


This is VB.NET code, but the idea is the same.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1