Intersect Command in Sheep Herder

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Mbessette
Posts: 9
Joined: Wed Feb 21, 2018 3:23 pm

Intersect Command in Sheep Herder

Post by Mbessette » Wed Apr 11, 2018 2:36 am

Hey all,

Having troubles making sense of this to truly wrap my head around it (maybe I'm just reading too far into nothing) but if anyone could shed some light on this that would be great.

In the tutorial for Sheep Herder, it has you use the intersect command to make sure that the sheep aren't overlapping the "pen" graphic or the groupControls and erase it if it does. What I'm not understanding is why the intersect commands are set to false? Wouldn't setting the intersect command to false imply that the sheep and pen are not touching or are/ are not visible over one another and vise versa with the groupControls and so should be erased?

The code works, I'm not getting any errors and it does what it's supposed to. I'm just trying to make sense of why/ how in my head lol. The line of code is below for further reference.

if intersect(button("sheep"&x),graphic"pen",255) is false and intersect(button("sheep"&x),group"groupControls", "0") is false then
end if
delete button("sheep"&x)


Thanks in advance!
-Matthew

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Intersect Command in Sheep Herder

Post by bogs » Wed Apr 11, 2018 3:24 am

Hm. Mind you, I never did the tutorial, and your only showing 3 lines of code, but my read on it is that as long as the intersection is 'false' in both tests, what ever is going on continues. I assume that if one test fails, then the result being tested for happens, like deleting a sheep.
Image

Mbessette
Posts: 9
Joined: Wed Feb 21, 2018 3:23 pm

Re: Intersect Command in Sheep Herder

Post by Mbessette » Wed Apr 11, 2018 3:55 am

But wouldn't it have to be True to continue? If button("sheep"&x) intersects with either graphic"pen" or group"groupControls" then it should erase, but this is stating that the intersection is False so shouldn't it be the other way around? Again I'm sure I'm over-analyzing but would like to get to the bottom of this lol. More code from the card is below.

on sheepGenerate
repeat with x = 1 to sLevel
repeat
clone button "templateSheep"
set the name of the last button to ("sheep"&x)
set the loc of the last button to random(320), random(480)
set the visible of the last button to true
if intersect(button("sheep"&x),graphic"pen",255) is false and intersect(button("sheep"&x),group"groupControls", "0") is false then
exit repeat
end if
delete button("sheep"&x)
end repeat
end repeat

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Intersect Command in Sheep Herder

Post by bogs » Wed Apr 11, 2018 4:36 am

Heh, I wasn't expecting you to post the code from the tutorial, sorry if I gave that impression.
Mbessette wrote:
Wed Apr 11, 2018 3:55 am
But wouldn't it have to be True to continue?
A lot of coding has to do with personal style, but to answer that question directly, no, it doesn't have to be true to continue, continuation (or not) could be based on any number of factors. One way to see exactly what is going on is to step through that handler in the debugger. This way, you would see exactly what is represented in each variable as you progress.

Some of the code is pretty generic, so lets step through it, I'll comment as I go. I'm sure if I goof up completely someone else will point it out :)

Code: Select all

on sheepGenerate
// handler starts...
	repeat with x = 1 to sLevel
// repeat the number of times equal to the number in "sLevel"
		repeat // do each of the following steps...
			clone button "templateSheep" // make a copy of the template button
// set the name of the button to where you are in the loop...
			set the name of the last button to ("sheep"&x)
// put the button in a random location in the window...
			set the loc of the last button to random(320), random(480)
// make the button visible...
			set the visible of the last button to true
			if intersect(button("sheep"&x),graphic"pen",255) is false and intersect(button("sheep"&x),group"groupControls", "0") is false then
// in this case, it is saying that if both conditions are false, your exiting to the top
// of this handler because the sheep is not touching whatever, so you don't have  
// to move to the next few lines and delete the button...
				exit repeat // exits the inner repeat loop...
			end if
// IF one or both of the conditions in the if statement are true, then the button
// is being deleted here...
			delete button("sheep"&x)
		end repeat // exits the inner loop if it made it this far...
	end repeat // exits the loop completely...
end sheepGenerate // end of the handler...
I'm sure all of the above comments weren't necessary, but I don't know how well commented the tutorial is. Again, though, I may have mis-read it, and if I did I certainly will apologize, but that was my take on it.
Image

Mbessette
Posts: 9
Joined: Wed Feb 21, 2018 3:23 pm

Re: Intersect Command in Sheep Herder

Post by Mbessette » Wed Apr 11, 2018 12:51 pm

Ahhhhhh okay so the statement being False sends us to the top of the loop again rather than deleting something each time. That's what I wasn't understanding.

Thanks so much for clearing that up!!

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Intersect Command in Sheep Herder

Post by bogs » Wed Apr 11, 2018 4:37 pm

My pleasure, glad I could help :)

Little problems like this is where the debugger really helps you understand the logic used, F11 will step through the lines, F5 will run through it quicker. In a case like this, where you are doing repeats to make sure it all works correctly, I F11 through it to make sure it is doing what I expect it to a few times, then F5 to get through the loop faster but still exit when it hits the statement past the loop.

No sheep were harmed in the writing of this message :D
Image

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”