Intersect not working

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
ddaddy
Posts: 17
Joined: Sun May 15, 2011 5:40 pm

Intersect not working

Post by ddaddy » Tue May 17, 2011 10:52 pm

I've copied some code from a tutorial video but I cant get the intersect working.

I have this to move a button (Button1) across the screen, and I have Button2 stationary next to it.

Code: Select all

on moveButton
   put item 1 of the location of button "Button1" into tLocX
   
   if the location of button "Button1" is within the rect of this card or tLocX < 0 then
      set the left of button "Button1" to the left of button "Button1" + 5
      send "moveButton" to me in 50 milliseconds
   else
      //Off screen
      set the location of button "Button1" to -100,50
      send "moveButton"
   end if
end moveButton
This is the collision code

Code: Select all

on enemyCollide
   show graphic "Outline" of me
   repeat 3 times
      set the lineSize of graphic "Outline" of me to 3
      set the lineSize of graphic "Outline" of me to 1
   end repeat
   hide graphic "Outline" of me
end enemyCollide

on checkForCollision
   if intersect (button "Button1", button "Button2") then
      answer "collide"
      //send "enemyCollide" to image "snackacan"
   end if
   send checkForCollision to me in 50 milliseconds
end checkForCollision
I've put in 'answer "collide" ' while I test, but Button1 just passes underneath Button2 without triggering the collision.

Any tips?

Thanks

dglass
Posts: 519
Joined: Thu Sep 24, 2009 9:10 pm
Contact:

Re: Intersect not working

Post by dglass » Tue May 17, 2011 11:52 pm

I don't see anywhere that you call 'checkForCollision' except within itself.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4163
Joined: Sun Jan 07, 2007 9:12 pm

Re: Intersect not working

Post by bn » Wed May 18, 2011 12:01 am

Hi ddaddy,

as dglass pointed out you dont start checkforColision.

here is a script that I put in a card script. It is started from a button.

Code: Select all

local sGo = true

on startMove
   put not sGo into sGo
   if sGo then
      send moveButton to me in 1 millisecond
      send checkForCollision to me in 1 milliseconds
   end if
end startMove

on moveButton 
   if sGo  then
         put item 1 of the location of button "Button1" into tLocX
         
         if the location of button "Button1" is within the rect of this card or tLocX < 0 then
         set the left of button "Button1" to the left of button "Button1" + 5
         send "moveButton" to me in 50 milliseconds
         else
               //Off screen
               set the location of button "Button1" to -100,50
               send "moveButton" to me in 1 millisecond
         end if
      
   end if
end moveButton

on checkForCollision
   if sGo then
      if intersect (button "Button1", button "Button2") then
         put  "collide" & the milliseconds
               //send "enemyCollide" to image "snackacan"
      else
         put ""
      end if
      send checkForCollision to me in 50 milliseconds
   end if
end checkForCollision

--This is the collision code
--Code:
on enemyCollide
   show graphic "Outline" of me
   repeat 3 times
      set the lineSize of graphic "Outline" of me to 3
      set the lineSize of graphic "Outline" of me to 1
   end repeat
   hide graphic "Outline" of me
end enemyCollide
The button script is:

Code: Select all

on mouseUp
   startMove
end mouseUp
note that the button script starts and stops the moving.

The script local variable in the card script sGo takes care of that.

Kind regards

Bernd

ddaddy
Posts: 17
Joined: Sun May 15, 2011 5:40 pm

Re: Intersect not working

Post by ddaddy » Wed May 18, 2011 9:26 am

Many thanks for that. All working now.
The reason I got confsed, is if I selected the moveButton script from the pull down menu in the code window, and pressed the Play button, the button started moving.
So I assumed if I selected the checkForCollision code and pressed play, then that bit of code should run, but it doesn't/didn't.
Any explanation as to why?

Also once i'd started the moveButton code, there is no way of stopping it. shoudn't there also be a stop button?
I know one of the concepts of LC is that its always running, so it helps testing, but earlier I got stuck in a messagebox (answer) loop that I couldn't get out of without forceing the application to close.

Thanks again for your help.

ddaddy
Posts: 17
Joined: Sun May 15, 2011 5:40 pm

Re: Intersect not working

Post by ddaddy » Wed May 18, 2011 9:27 am

One more quick thing about your code. Is the 'to me in 1 millisecond' needed for the button script?

I couldn't understand why it was there, and removing it doesn't affect the functionality of it at all.

Thanks

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4163
Joined: Sun Jan 07, 2007 9:12 pm

Re: Intersect not working

Post by bn » Wed May 18, 2011 12:17 pm

Hi ddaddy,
if I selected the moveButton script from the pull down menu in the code window, and pressed the Play button, the button started moving.
So I assumed if I selected the checkForCollision code and pressed play, then that bit of code should run, but it doesn't/didn't.
Any explanation as to why?
It would be best to post a zipped version of your example stack.
One more quick thing about your code. Is the 'to me in 1 millisecond' needed for the button script?
in this case not really needed. But the difference between send and send in time is that send in time is send after the current handler finishes. Send is send when the current handler issues it and after the command that you have send is finished execution returns to the original handler that issues the send.

So send in time detaches it from the sending handler. In most cases you will not notice no big difference, in some it is important to know the difference.

Kind regards

Bernd

Post Reply