#### Archived

This topic is now archived and is closed to further replies.

# How can I make a function to this code???

This topic is 6032 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I made this little code to c if two object''s collided... ---------------------------------------------------------------- Private Sub Timer1_Timer() If O1.Left >= O2.Left - O1.Width And O1.Left <= O2.Left + O2.Width And O1.Top >= O2.Top - O1.Height And O1.Top <= O2.Top + O2.Height Then Label1.Caption = "Crash" Else Label1.Caption = "" End If End Sub ---------------------------------------------------------------- How can I make a function so I can just say like: If Crash object1, Object2 By using the crash code i made? Plz help me! JFK

##### Share on other sites
create a function with return type bool. Change your if then..
to
  if (condition) {// your code}[else [if (otherConditioncondition)] {}][else{}]// the code between [] is optional

##### Share on other sites
Assuming you have defined an Object (with the properties Left, Top, Right and Bottom) type:
Private Function Crash(O1 As Object, O2 As Object) As Boolean  If O1.Left >= O2.Left - O1.Width \     And O1.Left <= O2.Left + O2.Width \     And O1.Top >= O2.Top - O1.Height \     And O1.Top <= O2.Top + O2.Height \     Then Crash = True  Else     Crash = False  End IfEnd Function

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!

##### Share on other sites
I''m a bit rusty on VB but I think this is how you''d do it.

Function Crash(O1 as Object, O2 as Object) as Boolean
If O1.Left >= O2.Left - O1.Width And O1.Left <= O2.Left + O2.Width And O1.Top >= O2.Top - O1.Height And O1.Top <= O2.Top + O2.Height Then
Crash = True
Else
Crash = False
End If
End Function

quote:
Original post by -Fruz-
I made this little code to c if two object''s collided...
----------------------------------------------------------------
Private Sub Timer1_Timer()
If O1.Left >= O2.Left - O1.Width And O1.Left <= O2.Left + O2.Width And O1.Top >= O2.Top - O1.Height And O1.Top <= O2.Top + O2.Height Then
Label1.Caption = "Crash"
Else
Label1.Caption = ""
End If
End Sub
----------------------------------------------------------------
How can I make a function so I can just say like:

If Crash object1, Object2

By using the crash code i made?
Plz help me!

JFK

##### Share on other sites
quote:
Original post by hewhay
create a function with return type bool. Change your if then..
to <snip> and change your AND to &&, your or to ||

It's rather fascinating that you didn't notice he wasn't programming in a C-derivative.

[Edit:] Formatting.

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!

Edited by - Oluseyi on January 13, 2002 7:17:09 PM

##### Share on other sites
quote:
Original post by hewhay
create a function with return type bool. Change your if then..
to

  if (condition) {// your code}[else [if (otherConditioncondition)] {}][else{}]// the code between [] is optional

hmm what do the square brackets mean?? i didnt know yuo could do that in c++

##### Share on other sites
Thanx guys!
btw... Is it a simpler n faster way to detect object collision then this?
(I''m doing this in VB)

##### Share on other sites
Oops... I forgot something! I wan''t this code to work with different pictures... C? So if i write for example this:
--------------------
if crash bullet, car
--------------------
It detects a collision with the bullet and the car, and if i write this:
--------------------
if crash car, car2
--------------------
It detects a collision with 2 cars... U guys understand?

##### Share on other sites
quote:
Original post by -Fruz-
Oops... I forgot something! I wan''t this code to work with different pictures... C?

a.) Stop using single letters as substitutes for words. It is neither "cool" nor does it save you keystrokes to type "C" instead of "see", "2" instead of "to"/"too", etc. Furthermore, it confuses the situation here since "C" is the name of a programming language.

b.) To answer your question, you need to define a data structure that represents any movable object - the Object parameter type in responses above. This Object type would need to have the properties Left, Right, Top and Bottom. It would also have some form of Picture property, which would be the image you would see.

c.) The syntax would be more If Crash(Car1, Car2) Then ... The parentheses are required in VB (a very regimented language per code formatting).

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!

##### Share on other sites
This is what i ended up with (To test it):
----------------------------------------------------------------
Function Crash(O1 As Object, O2 As Object) As Boolean
If O1.Left >= O2.Left - O1.Width _
And O1.Left <= O2.Left + O2.Width _
And O1.Top >= O2.Top - O1.Height _
And O1.Top <= O2.Top + O2.Height Then
Crash = True
Else
Crash = False
End If
End Function

Private Sub Timer1_Timer()
If Crash(object1, object2) = True Then
Label1.Caption = "Crash"
Else
Label1.Caption = ""
End If
End Sub
----------------------------------------------------------------
I''m really happy with this solution because it''s the easiest way (in my idea). If not then you tell me if there is any simpler and faster way to detect collision, cause I can''t imagine there is any...

JFK

1. 1
2. 2
Rutin
24
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 22
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631766
• Total Posts
3002221
×