Function not returning a value
BlitzPlus Forums/BlitzPlus Beginners Area/Function not returning a value
| ||
According to the Help, a function can return a value. I am trying to get the following test code to work, but to no avail. Basically I want the function to return true or false if the objects have collided. The Remmed out Imagescollide line works if it is unremmed.... Graphics 800,600,0,2 SetBuffer BackBuffer() Global player = CreateImage(50,50) Global box = CreateImage(800,600) Cls ;Draw Box Rect 50,50,600,400,0 GrabImage box,0,0 Cls ;Draw player Oval 0,0,20,20,1 GrabImage player,0,0 ;Main Loop Repeat Cls DrawImage box,0,0 DrawImage player,MouseX(),MouseY() If collision=1 Then Text 280,240,"COLLISION!!!" ;If ImagesCollide(player,MouseX(),MouseY(),0,box,0,0,0) Then Text 280,240,"COLLISION!!!" Flip Until KeyDown(1) End Function collision() If ImagesCollide(player,MouseX(),MouseY(),0,box,0,0,0) Then Return True Else Return False End If End Function Any ideas would be much appreciated! Thanks Andy |
| ||
Spot the difference...If collision=1 Then Text 280,240,"COLLISION!!!" If collision() Then Text 280,240,"COLLISION!!!" You can also express this as If Collision() = True. Try getting into the habit of using True and False instead of 1 and 0. Makes your code much more readable. |
| ||
Thanks GfK! I have been trying for a day or so to sort this....... You can also express this as If Collision() = True. Try getting into the habit of using True and False instead of 1 and 0. Makes your code much more readable. Well it is the beginners area!...... I have always have had a nasty habit of writing spaghetti code.. I must try harder! Thanks again Andy |
| ||
Well it is the beginners area!...... Yep, but no harm in mentioning it. :DI wish I'd had somebody to explain to me about code consistency when I first started programming 22 years ago. |
| ||
As GFK indicated, function names need to be followed by parenthesis if you want the function to return a value. The parenthesis are optional if you don't need to return a value. Eg. Function MyFunc1() ; Function code. Return x End Function Function MyFunc2( a ) ; Function code. Return x End Function If MyFunc1() Then whatever If MyFunc2( b ) Then whatever This also applies to native Blitz functions. Note that some Blitz commands that return a value are actually language constructs, rather than functions, and putting parenthesis after them will cause an error. Some examples are: New, Before, After, First, Last. |
| ||
Cheers Bill! Thanks for your input Andy |