Results 1 to 17 of 17

Thread: move a shape with arrows

  1. #1

    Thread Starter
    Banned
    Join Date
    Nov 2001
    Posts
    59

    move a shape with arrows

    how do i make a shape move to the right and the left when u press the arrows on the keyboard? THANKS!

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Set the KeyPreview property of the Form to True and use code simular to the following in the KeyDown event of the form.
    VB Code:
    1. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    2.     Select Case KeyCode
    3.         Case vbKeyDown
    4.             Shape1.Top = Shape1.Top + 45 '3 pixels
    5.         Case vbKeyUp
    6.             Shape1.Top = Shape1.Top - 45 '3 pixels
    7.         Case vbKeyLeft
    8.             Shape1.Left = Shape1.Left - 45
    9.         Case vbKeyRight
    10.             Shape1.Left = Shape1.Left + 45
    11.     End Select
    12. End Sub
    Best regards

  3. #3
    Rather than using 45 for 3 pixels, I suggest (with all respect to Joacim ):
    VB Code:
    1. Screen.TwipsPerPixelX * 3
    2.  
    3. ' and
    4.  
    5. Screen.TwipsPerPixelY * 3
    Twips are dumb.

  4. #4
    Frenzied Member Mega_Man's Avatar
    Join Date
    Mar 2001
    Location
    North of England, South-East of Iceland
    Posts
    1,067
    The picture would flicker like mad though. would it not?
    How would you resolve that?
    "If at first you don't succeed, then skydiving is not for you"

  5. #5
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Try setting the autoredraw property to true on the form. Just one way, as there are a few others..

  6. #6
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Originally posted by filburt1
    Rather than using 45 for 3 pixels, I suggest (with all respect to Joacim ):
    VB Code:
    1. Screen.TwipsPerPixelX * 3
    2. ' and
    3. Screen.TwipsPerPixelY * 3
    Twips are dumb.
    I agree, i'm just drunk and lazy right now

  7. #7
    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:
    1. LockWindowUpdate Me.hwnd
    2. ' move stuff
    3. LockWindowUpdate 0 ' I think

  8. #8
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    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

  9. #9
    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
    I've still got over 100, many based on Filburt: http://www.turtletips.com/avatars/

    And she's too hot to get rid of.

  10. #10
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    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.
    I know that she's hot because I'm sleeping with her (I wish )

  11. #11
    Frenzied Member Mega_Man's Avatar
    Join Date
    Mar 2001
    Location
    North of England, South-East of Iceland
    Posts
    1,067
    You sleep Joacim??
    When?.. you're always on here.
    "If at first you don't succeed, then skydiving is not for you"

  12. #12
    Originally posted by Joacim Andersson
    I know that she's hot because I'm sleeping with her (I wish )
    You are so drunk tonight.

  13. #13
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Originally posted by Mega_Man
    You sleep Joacim??
    When?.. you're always on here.
    I don't need any sleep I've got Whiskey!
    Originally posted by filburt1
    You are so drunk tonight
    You are so right
    I've actually got a party going on right now and you're all invited

  14. #14
    DaoK
    Guest
    Here is a old project I have made just to show you how to move a shape.

  15. #15

    Thread Starter
    Banned
    Join Date
    Nov 2001
    Posts
    59

    I tried!~

    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

  16. #16
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    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

  17. #17

    Thread Starter
    Banned
    Join Date
    Nov 2001
    Posts
    59

    thanks

    thanks, now it works

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width