-
I am a beginner and I want to move a shape according to which arrow button I press. But I don't want to press it a lot of times, just once and that's it.
What I mean is , for example, if I press on the right arrow, the shape will move right by itself until it hits the extremity of the form.
Thanks in advance :)
Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 39 Then
Shape1.Left = Shape1.Left + 50
End If
If KeyCode = 38 Then
Shape1.Top = Shape1.Top - 50
End If
If KeyCode = 37 Then
Shape1.Left = Shape1.Left - 50
End If
If KeyCode = 40 Then
Shape1.Top = Shape1.Top + 50
End If
End Sub
The code shows what I did but it doesn't work like I want it to be. :mad:
-
Code:
if keycode = vbkeyup then
do until shape1.top < 0
shape1.top = shape1.top - 50
loop
ElseIf keycode = vbkeydown then
do until shape1.top > me.scaleheight
shape1.top = shape1.top + 50
loop
ElseIf keycode = vbkeyleft then
do until shape1.left < 0
shape1.left = shape1.left - 50
loop
ElseIf keycode = vbkeyright then
do until shape1.left > me.scalewidth
shape1.left = shape1.left + 50
loop
End If
-
Thanks, the code you wrote works. But now I would like to change the direction of the shape whenever I want. Because :
Code:
do until shape1.top > me.scaleheight
will make the shape go until the extremity before I can manage to change direction again.
Please help, thx :)
[Edited by HELP ! on 10-02-2000 at 09:54 PM]
-
Check out my animation demo or if that's too complex the DInput demo (really easy!).
-
Hi Fox,
I checked out your DInput prog, it doesn't work :(
-
Dude...
I don't think you are in need of help to move a shape wound on the screen. I think you need a few quick hints as to how the VB handles Events and which part of your code is running at a given time.
If you want something to happen on the screen at a given rate then you can look at the Timer control.
If you want the program to react whenever you press a key then you can look at the Key_Down event (I see you know this one - cool)
If you put code into the Key_Down event, then each time you press a key AND when VB is listening, the code will run. VB might not be listening because it is too busy running the rest of your code. This would normally only happen if you are in an endless loop or a very long loop anyhow.
Now, you wanted your shape to change direction when a key is pressed right? And you want the shape to move in the last known direction right?
You will need to keep track of the current direction in that case. To do that, decalre a variable in your Form's Declarations area
Code:
Dim mDirection as Integer
Now, when the user presses a key, store this data in the mDirection variable:
Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
mDirection = KeyCode
End Sub
That was easy you say? Of course it was...
Now, in that Timer control's Timer event, you do the actual move. You might like to play around with the timer control's interval property to spped up or slow down the movement.
Code:
Private Sub Timer1_Timer()
Dim yMove As Integer
Dim xMove As Integer
Select Case mDirection
Case KeyCodeConstants.vbKeyUp
yMove = -50
Case KeyCodeConstants.vbKeyLeft
xMove = -50
Case KeyCodeConstants.vbKeyDown
yMove = 50
Case KeyCodeConstants.vbKeyRight
xMove = 50
End Select
' now move the shape as long as it is not going to be outside the form area
With Shape1
If (.Left + xMove >= 0) And (.Left + xMove <= Me.ScaleWidth - .Width) Then
.Left = .Left + xMove
End If
If (.Top + yMove >= 0) And (.Top + yMove <= Me.ScaleHeight - .Height) Then
.Top = .Top + yMove
End If
End With
End Sub
You can see that all the "complex" stuff is in the timer event.
This is a deliberately simple example which I hope helps to guide you for your next project :)
Cheers
-
THanks to all of you especially Paul ! You guys are so good !
I am only beginning to learn VB because I chose this course for my complementary. But we only learn really basic stuffs there. I find this program very interesting !
I'm hoping to learn more about it, and you guys help me to archieve this goal more rapidly , thx again !