how do i make a shape move to the right and the left when u press the arrows on the keyboard? THANKS!
Printable View
how do i make a shape move to the right and the left when u press the arrows on the keyboard? THANKS!
Set the KeyPreview property of the Form to True and use code simular to the following in the KeyDown event of the form.Best regardsVB Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) Select Case KeyCode Case vbKeyDown Shape1.Top = Shape1.Top + 45 '3 pixels Case vbKeyUp Shape1.Top = Shape1.Top - 45 '3 pixels Case vbKeyLeft Shape1.Left = Shape1.Left - 45 Case vbKeyRight Shape1.Left = Shape1.Left + 45 End Select End Sub
Rather than using 45 for 3 pixels, I suggest (with all respect to Joacim :D):
Twips are dumb. :rolleyes:VB Code:
Screen.TwipsPerPixelX * 3 ' and Screen.TwipsPerPixelY * 3
The picture would flicker like mad though. would it not?
How would you resolve that?
Try setting the autoredraw property to true on the form. Just one way, as there are a few others..
I agree, i'm just drunk and lazy right now :)Quote:
Originally posted by filburt1
Rather than using 45 for 3 pixels, I suggest (with all respect to Joacim :D):
Twips are dumb. :rolleyes:VB Code:
Screen.TwipsPerPixelX * 3 ' and Screen.TwipsPerPixelY * 3
If you use either LockWindowUpdate Me.hwnd (see the API viewer for the declaration) or use BitBlt you can get rid of the flicker:
VB Code:
LockWindowUpdate Me.hwnd ' move stuff LockWindowUpdate 0 ' I think
Hey filbert1!
Why have you stoped using your turtles in your avitars?
I don’t really appreciate that you’re now using a picture of my girlfriend ;)
I've still got over 100, many based on Filburt: http://www.turtletips.com/avatars/ :)Quote:
Originally posted by Joacim Andersson
Hey filbert1!
Why have you stoped using your turtles in your avitars?
I don’t really appreciate that you’re now using a picture of my girlfriend ;)
And she's too hot to get rid of. :D :D
I know that she's hot because I'm sleeping with her (I wish :p)Quote:
Originally posted by filburt1
I've still got over 100, many based on Filburt: http://www.turtletips.com/avatars/ :)
And she's too hot to get rid of. :D :D
You sleep Joacim?? :)
When?.. you're always on here. :D
You are so drunk tonight. :DQuote:
Originally posted by Joacim Andersson
I know that she's hot because I'm sleeping with her (I wish :p)
I don't need any sleep I've got Whiskey!Quote:
Originally posted by Mega_Man
You sleep Joacim?? :)
When?.. you're always on here. :D
You are so right :)Quote:
Originally posted by filburt1
You are so drunk tonight
I've actually got a party going on right now and you're all invited :)
Here is a old project I have made just to show you how to move a shape.
I used this code and named my shape "player" but it doesnt move!
Private Sub player_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyRight Then
player.Left = player.Left + 30
End If
If KeyCode = vbKeyLeft Then
player.Left = player.Left - 30
End If
If KeyCode = vbKeyUp Then
If player.Top < Line2.Y1 Then Exit Sub
player.Top = player.Top - 30
End If
If KeyCode = vbKeyDown Then
If (player.Top + player.Height) > Line1.Y1 Then Exit Sub
player.Top = player.Top + 30
End If
End Sub
You should use the Form_KeyDown event and not the player_KeyDown event.
You also have to set the KeyPreview property of the Form to True.
Best regards
thanks, now it works :)