Eli mulla on tässä tällanen koodi:
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Dim abc As Boolean 'Tätä ei ole pakko käyttää, mutta suositeltavaa
Dim x2 As Integer
Dim y2 As Integer
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
abc = True
x2 = Picture1.Left
Select Case KeyCode
Case vbKeyRight And abc 'Oikealle
x = Picture1.Left
y = Picture1.Top
Picture1.Left = Picture1.Left + 5
x2 = x2 + 5
Form1.Caption = x2 & ", " & y2
abc = False
Case vbKeyLeft And abc 'Vasemmalle
x = Picture1.Left
y = Picture1.Top
Picture1.Left = Picture1.Left - 5
x2 = x2 - 5
Form1.Caption = x2 & ", " & y2
abc = False
Case vbKeyUp And abc 'ylös
x = Picture1.Left
y = Picture1.Top
Picture1.Top = Picture1.Top - 5
y2 = y2 - 5
Form1.Caption = x2 & ", " & y2
abc = False
Case vbKeyDown And abc 'alas
x = Picture1.Left
y = Picture1.Top
Picture1.Top = Picture1.Top + 5
y2 = y2 + 5
Form1.Caption = x2 & ", " & y2
abc = False
End Select
If Picture1.Left + Picture1.Width > Picture2.Left Then
If Picture1.Left < Picture2.Left + Picture2.Width Then
If Picture1.Top + Picture1.Height > Picture2.Top Then
If Picture1.Top < Picture2.Top + Picture2.Height Then
Picture1.Left = x
Picture1.Top = y
End If
End If
End If
End If
End Sub
Private Sub Form_Load()
Form1.Width = 15360
Form1.Height = 11520
x2 = 0
y2 = 0
End Sub
Private Sub mnuLopeta_Click()
End
End Sub..ja tässä tuo sama on exenä: www.freewebs.com/apoch/harkka.exe
Ja ongelma on se että kun tossa ohjelmassa kun kulkee ukon kanssa valkoisen laatikon yläpuolelle ja kävelee sitä päin (alas päin nuoli pohjassa) niin ukko kyllä pysähtyy mutta y:n arvo jatkaa vain kasvamistaan. (sama toimii myös alhaalta päin)
....mutta tuo sama virhe ei kuitenkaan toistu kun ukolla kulkee boxia päin sivu suunnassa.
apuva???!
Koodiasi voi aika lailla lyhentää, esim. seuraavalla tavalla. Tosin kannattaisi ehkä ensin tarkistaa, että liikkuminen on mahdollista, ja sitten vasta liikuttaa kuvaa.
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
x = Picture1.Left
y = Picture1.Top
Select Case KeyCode
Case vbKeyRight
Picture1.Left = Picture1.Left + 5
Case vbKeyLeft
Picture1.Left = Picture1.Left - 5
Case vbKeyUp
Picture1.Top = Picture1.Top - 5
Case vbKeyDown
Picture1.Top = Picture1.Top + 5
End Select
If Picture1.Left + Picture1.Width > Picture2.Left Then
If Picture1.Left < Picture2.Left + Picture2.Width Then
If Picture1.Top + Picture1.Height > Picture2.Top Then
If Picture1.Top < Picture2.Top + Picture2.Height Then
Picture1.Left = x
Picture1.Top = y
End If
End If
End If
End If
Form1.Caption = Picture1.Left & ", " & Picture1.Top
End Sub
Private Sub Form_Load()
Form1.Width = 15360
Form1.Height = 11520
End Sub
Private Sub mnuLopeta_Click()
End
End SubPS. Muista ensi kerralla kirjoittaa viestille kunnollinen otsikko ja laittaa koodin ympärille kooditagit.
kooditagit?
https://www.ohjelmointiputka.net/ohje.php?tunnus=kohjeet
RTFM!!!
edit: Ajoissa sentään...
joo joo
Aihe on jo aika vanha, joten et voi enää vastata siihen.