954,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Moving a shape

Hi, I want to move a shape (label) when I press WASD around a maze. I got what I have so far off the net, but I cant figure out whats wrong with it.

Original code

Public x 1 'This is whats wrong - it says "End of statement expected" when mouse over 
    Public y 1 'the "1"


Private Sub Form_KeyDown(ByVal KeyCode As Integer, ByVal Shift As Integer)
        ' if keypressed is right move the shape three more
        ' spaces to the right
        If KeyCode = 68 Then
            x = x + 3
            shape1.Move(x, y)
        End If
        ' if keypressed is left move the shape three more
        ' spaces to the left
        If KeyCode = 65 Then
            x = x - 3
            shape1.Move(x, y)
        End If

        If KeyCode = 83 Then
            y = y + 3
            shape1.Move(x, y)
        End If

        If KeyCode = 86 Then
            y = y - 3
            shape1.Move(x, y)
        End If
    End Sub


Note Im completely new to this so keep it simple please!
Thanks!

Amadman114
Newbie Poster
6 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 
Public x = 1 'This is whats wrong - it says "End of statement expected" when mouse over 
Public y = 1 'the "1"
Jx_Man
Nearly a Senior Poster
3,329 posts since Nov 2007
Reputation Points: 1,372
Solved Threads: 444
 

When I do this,

shape1.Move

Is underlined in blue and I still can't run it
Sorry for the slow reply!

Amadman114
Newbie Poster
6 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 


Move isn't a method its an event.

Just set the location of the label

'Instead of shape1.move(x,y)
shape1.Location = new Point(x,y)
Unhnd_Exception
Posting Pro
570 posts since Nov 2010
Reputation Points: 249
Solved Threads: 201
 

But then how do you not just set the location but make the location change depending on where the shape already is?

Im trying to make a shape move around the maze with WASD which seems to be harder than I thought it would be :s

Amadman114
Newbie Poster
6 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 


This will move the label around with the WASD keys or the Up,Left,Down, or Right arrow keys. From its current Position.

Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        Select Case e.KeyCode
            Case Keys.A, Keys.Left
                shape1.Location += New Size(-3, 0)
            Case Keys.D, Keys.Right
                shape1.Location += New Size(3, 0)
            Case Keys.W, Keys.Up
                shape1.Location += New Size(0, -3)
            Case Keys.S, Keys.Down
                shape1.Location += New Size(0, 3)
        End Select
    End Sub
Unhnd_Exception
Posting Pro
570 posts since Nov 2010
Reputation Points: 249
Solved Threads: 201
 

Ha ha! Thank you Exception :)

Would still appreciate if there was a way if for example if you pressed A+S then it would move diagonally down.

I tried adding:

Case Keys.A And Keys.S 'also tried using "+" instead of "and"
                shape1.Location += New Size(-3, 3)
Amadman114
Newbie Poster
6 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

Ha ha! Thank you Exception :)

Would still appreciate if there was a way if for example if you pressed A+S then it would move diagonally down.

I tried adding:

Case Keys.A And Keys.S 'also tried using "+" instead of "and"
                shape1.Location += New Size(-3, 3)
Amadman114
Newbie Poster
6 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: