Archived

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

-Fruz-

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

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 this post


Link to post
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


and change your AND to &&, your or to ||

Share this post


Link to post
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 If
End Function


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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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


and change your AND to &&, your or to ||

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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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...

Thanx for your help people!

JFK

Share this post


Link to post
Share on other sites
quote:

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


My mistake, for a moment i understood that he wanted to change his code to a c function. next time i should read more carefully
quote:

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


you cant, i was using a common syntax for optional code

Share this post


Link to post
Share on other sites