| View previous topic :: View next topic |
| Author |
Message |
RenRu Sakamari Newbie
Joined: 04 Aug 2005 Posts: 20 Location: Spokane, WA
|
Posted: Aug 6th, 2005 11:51 AM Post subject: Moving Something With Arrow Keys |
|
|
Hi, I'm having problems, and I don't even know if you can do this with VB.NET but I still want to find out.
Ok, I am making a game where you move a 2d... maybe a dot for example around the screen by using the arrow keys, but I don't know how to make it controllable by the arrow keys, could someone helpp me or give me a tip or the code to do that? Thanks.
Cameron (RenRu) _________________ BLA<>,<> |
|
| Back to top |
|
feng(tree) Newbie

Joined: 05 Aug 2005 Posts: 8 Location: bathurst australia
|
Posted: Aug 7th, 2005 12:22 PM Post subject: |
|
|
im not sure how to do it with the arrow keys,
but im using the letters to move my charachter around.
you should do this in the mean time.
use the Keyscii integer to determine which button they've pressed
then use an if statement to continue depending on what they pressed.
every letter has a keyascii number, L is 108 and i think A is 65. you have to use these in the if statements not the letter it self. im not sure if the arrow keys have keyascii though. _________________ ""but then again, whos to say whats right and and wrong, what, with all our modern ideas, and products!!!"
an inspiring quote by Homer j Simpson |
|
| Back to top |
|
RenRu Sakamari Newbie
Joined: 04 Aug 2005 Posts: 20 Location: Spokane, WA
|
Posted: Aug 8th, 2005 07:59 PM Post subject: |
|
|
Okay... I still don't know how to move it... How would the code be fore the keyascii statement stuff? And also, how do you fu\ind out the numbers for the letters? _________________ BLA<>,<> |
|
| Back to top |
|
jmcilhinney Freshman
Joined: 06 Jul 2005 Posts: 25 Location: Sydney, Australia
|
Posted: Aug 9th, 2005 12:54 AM Post subject: |
|
|
Set the KeyPreview property of the form to True. Use the KeyUp event handler to test whether e.KeyCode is equal to Keys.Up, Keys.Down, Keys.Left or Keys.Right. You have to use the KeyUp event because the arrows do not raise the KeyDown or KeyPress events.
| Code: | Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
Select Case e.KeyCode
Case Keys.Up
'Move object up.
Case Keys.Down
'Move object down.
Case Keys.Left
'Move object left.
Case Keys.Right
'Move object right.
Case Else
'Do nothing.
End Select
End Sub |
|
|
| Back to top |
|
RenRu Sakamari Newbie
Joined: 04 Aug 2005 Posts: 20 Location: Spokane, WA
|
Posted: Aug 9th, 2005 09:35 PM Post subject: |
|
|
Ok, is there a certain way you have to set a picture to be able to be movedby the key pressing? _________________ BLA<>,<> |
|
| Back to top |
|
jmcilhinney Freshman
Joined: 06 Jul 2005 Posts: 25 Location: Sydney, Australia
|
Posted: Aug 9th, 2005 09:53 PM Post subject: |
|
|
It depends what it is that you are trying to move. If it is a control then you simply need to set its Top or Left property, depending on whether you are moving it vertically or horizontally. If it is something that you have drawn on the form yourself using GDI+, then you would need to refresh the form so that it gets redrawn and then draw the object again in the new position. All GDI+ drawing should be done on the form's Paint event, so you would simply alter some form-level variables that control the drawing. Here's an example of moving a circle around a form: | Code: | Private x As Integer = 200
Private y As Integer = 200
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim g As Graphics = Graphics.FromHwnd(Me.Handle)
g.DrawEllipse(New Pen(Color.Red), x, y, 100, 100)
End Sub
Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
Select Case e.KeyCode
Case Keys.Up
Me.y -= 10
Case Keys.Down
Me.y += 10
Case Keys.Left
Me.x -= 10
Case Keys.Right
Me.x += 10
Case Else
'Do nothing.
End Select
Me.Refresh()
End Sub |
|
|
| Back to top |
|
RenRu Sakamari Newbie
Joined: 04 Aug 2005 Posts: 20 Location: Spokane, WA
|
Posted: Aug 10th, 2005 11:20 AM Post subject: |
|
|
Yeah... Thanks, but I still need some help with it... Could you send me an example code if you were just wanting to make a picturebox move? Sorry if I'm asking of too much work _________________ BLA<>,<> |
|
| Back to top |
|
jmcilhinney Freshman
Joined: 06 Jul 2005 Posts: 25 Location: Sydney, Australia
|
Posted: Aug 10th, 2005 05:37 PM Post subject: |
|
|
| Moving a control is even easier. When the user presses the Up arrow you just decrement the control's Top property by the desired value. Likewise you increment the Top property to move it down. The Left property similarly determines the controls horizontal position. I'm going to leave the actual code to you. All you have to do is change the code within the Select Case statement. You can remove the call to Refresh, as well as the class-level variables and the Paint event handler. |
|
| Back to top |
|
RenRu Sakamari Newbie
Joined: 04 Aug 2005 Posts: 20 Location: Spokane, WA
|
Posted: Aug 10th, 2005 06:27 PM Post subject: |
|
|
Umm... How do I do that? Sorry I'm really new to VB and I only really know how to use buttons and such, sorry....
I'm trying to make it so it doesn't move unless you use the arrow keys, and I had NO idea what you just said, sorry. _________________ BLA<>,<> |
|
| Back to top |
|
jmcilhinney Freshman
Joined: 06 Jul 2005 Posts: 25 Location: Sydney, Australia
|
Posted: Aug 10th, 2005 06:45 PM Post subject: |
|
|
| Code: | Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
Select Case e.KeyCode
Case Keys.Up
Me.PictureBox1.Top -= 10
Case Keys.Down
Me.PictureBox1.Top += 10
Case Keys.Left
Me.PictureBox1.Left -= 10
Case Keys.Right
Me.PictureBox1.Left += 10
Case Else
'Do nothing.
End Select
End Sub | I'm not sure if you have good reason to be moving that PictureBox, but I'd suggest that you get familiar with the basics, like how to set a property in code, before trying more advanced things. Just a suggestion. |
|
| Back to top |
|
RenRu Sakamari Newbie
Joined: 04 Aug 2005 Posts: 20 Location: Spokane, WA
|
Posted: Aug 10th, 2005 06:49 PM Post subject: |
|
|
Yeah, I have basic knowledge of this program, but I really suck at it _________________ BLA<>,<> |
|
| Back to top |
|
RenRu Sakamari Newbie
Joined: 04 Aug 2005 Posts: 20 Location: Spokane, WA
|
Posted: Aug 10th, 2005 07:00 PM Post subject: |
|
|
Argh... It says there are errors... It says C:\Documents and Settings\Quality Customer\My Documents\Visual Studio Projects\WindowsApplication15\Form1.vb(59): 'KeyCode' is not a member of 'System.EventArgs'.
How do I make that fixed? I'm going to try to fix it myself, but help would be nice.
THANK YOU! _________________ BLA<>,<> |
|
| Back to top |
|
jmcilhinney Freshman
Joined: 06 Jul 2005 Posts: 25 Location: Sydney, Australia
|
Posted: Aug 10th, 2005 07:08 PM Post subject: |
|
|
| What event are you using? Is it the KeyUp event? To create an event handler for a particular event, select the object for which you want to handle an event from the drop-down list at the top-left of the code window, in this case it is the form itself, and then select the event from the drop-down list at the top-right. This will create an empty event handler and you just put the code you want to execute into it. |
|
| Back to top |
|
RenRu Sakamari Newbie
Joined: 04 Aug 2005 Posts: 20 Location: Spokane, WA
|
Posted: Aug 10th, 2005 07:09 PM Post subject: |
|
|
Oh, duh... I knew that, sorry... Anyway, thank you so much for your help! _________________ BLA<>,<> |
|
| Back to top |
|
Meadow Newbie
Joined: 06 Sep 2005 Posts: 1
|
Posted: Sep 6th, 2005 10:32 AM Post subject: |
|
|
I am so glad I have found this topic I've been searching all over the web for something like this so I've registered as soon as I've found this thankyou so much.  _________________ [link] - My Site that sucks... |
|
| Back to top |
|
|