PDA

Click to See Complete Forum and Search --> : A very Hard Question!!!!!!!!!!!


kourosh
Jan 14th, 2000, 12:08 PM
Hi, I am doing a project for my school and our teacher ask me to creat an application which contain an image and when I press a button image start moving and when the image hit the walls change it direction randomly, I have created the codes but when every I run it my application crashes. Please help me if you know what is wrong with these codes.

CODES
=======
Private Sub cmdstart_Click()
Dim Random As Integer
Dim Direction As Integer
If imgmove.Left = 0 Then
Do
Random = Int((4 - 1) * Rnd + 1)
Loop While (Random <> 3)
If Random = 1 Then
Do
Direction = Int((5 - 1) * Rnd + 1)
imgmove.Top = (Val(imgmove.Top) - Val(Direction))
Loop Until (imgmove.Top = 0)

Else
If Random = 2 Then
Do
Direction = Int((5 - 1) * Rnd + 1)
imgmove.Top = (Val(imgmove.Top) + Val(Direction))
Loop Until (imgmove.Top = 4140)
Else
If Random = 4 Then
Do
Direction = Int((5 - 1) * Rnd + 1)
imgmove.Left = (Val(imgmove.Left) + Val(Direction))
Loop Until (imgmove.Left = 6900)
End If
End If
End If
Else
If imgmove.Top = 0 Then
Do
Random = Int((4 - 1) * Rnd + 1)
Loop While (Random <> 1)
If Random = 2 Then
Do
Direction = Int((5 - 1) * Rnd + 1)
imgmove.Top = (Val(imgmove.Top) + Val(Direction))
Loop Until (imgmove.Top = 4140)
Else
If Random = 3 Then
Do
Direction = Int((5 - 1) * Rnd + 1)
imgmove.Left = (Val(imgmove.Left) - Val(Direction))
Loop While (imgmove.Left = 0)
Else
If Random = 4 Then
Do
Direction = Int((5 - 1) * Rnd + 1)
imgmove.Left = (Val(imgmove.Left) + Val(Direction))
Loop Until (imgmove.Left = 6900)
End If
End If
End If
Else
If imgmove.Top = 4140 Then
Do
Random = Int((4 - 1) * Rnd + 1)
Loop While (Random <> 2)
If Random = 3 Then
Do
Direction = Int((5 - 1) * Rnd + 1)
imgmove.Left = (Val(imgmove.Left) - Val(Direction))
Loop Until (imgmove.Left = 0)
Else
If Random = 4 Then
Do
Direction = Int((5 - 1) * Rnd + 1)
imgmove.Left = (Val(imgmove.Left) + Val(Direction))
Loop Until (imgmove.Left = 6900)
Else
If Random = 1 Then
Do
Direction = Int((5 - 1) * Rnd + 1)
imgmove.Top = (Val(imgmove.Top) - Val(Direction))
Loop Until (imgmove.Top = 0)
End If
End If
End If
Else
If imgmove.Left = 6900 Then
Do
Random = Int((4 - 1) * Rnd + 1)
Loop While (Random <> 4)
If Random = 1 Then
Do
Direction = Int((5 - 1) * Rnd + 1)
imgmove.Top = (Val(imgmove.Top) - Val(Direction))
Loop Until (imgmove.Top = 0)

Else
If Random = 2 Then
Do
Direction = Int((5 - 1) * Rnd + 1)
imgmove.Top = (Val(imgmove.Top) + Val(Direction))
Loop While (imgmove.Top = 4140)
Else
If Random = 3 Then
Do
Direction = Int((5 - 1) * Rnd + 1)
imgmove.Left = (Val(imgmove.Left) - Val(Direction))
Loop While (imgmove.Left = 0)
Else
MsgBox "Unable to comply", vbInformation + vbOKOnly, "Error"
End If
End If
End If
End If
End If
End If
End If
End Sub
=============================
Code Ended
=============================
Thanks alot for your help.

DiGiTaIErRoR
Jan 14th, 2000, 04:42 PM
Not so hard...
here's what you'll need
two text boxes[text1,text2]
one timer[timer1]
on image[image1]
command button[command1]
---
in the timer put:

image1.move text1 text2
if image1.left + image1.width > form1.width then text1.text = int (90 * rnd + 1) * -1
if image1.top + image1.height - 300 > form1.height then text2.text = int (90 * rnd + 1) * -1
if image1.left < 0 then text1.text = int (90 * rnd + 1)
if image1.top < 0 then text2.text = int (90 * rnd + 1)

In the textboxes put a number or have an onload generate a random number for them but make them invisible
in the command button put:

if timer1.enabled = true then
timer1.enables = false
else
timer1.enables = true
end if

do whatever with the image
the -300 may need to be changed depending the title bar because that's included with the height.
set the interval of the timer to higher for a slower scroll lower for faster
you might want to change the rnd statement to:
(45 * rnd + 1) + 45
this will set the speed for a faster update


------------------
DiGiTaIErRoR

kourosh
Jan 15th, 2000, 03:10 AM
Hi
Thanks for your help, but I didn't understand the reason that you used the text box's??
Maybe you didn't understand my question!! I have an image in the center of the form and I want the image to find a random direction and move. Whenever it hit one of the sides then change it direction. I don't think I be needing any text box though.

''''''''''''''''
Kourosh Gonabadi
VB Student
''''''''''''''''

[This message has been edited by kourosh (edited 01-15-2000).]

DiGiTaIErRoR
Jan 15th, 2000, 05:01 AM
textboxes tell it the direction to go

------------------
DiGiTaIErRoR