Intersect() or Within Issues

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
istech
Posts: 194
Joined: Thu Sep 19, 2013 10:08 am

Intersect() or Within Issues

Post by istech » Thu Dec 12, 2013 2:33 pm

Hi all I wonder if someone can point me in the right direction.

I have 5 images and one of the images is set as "target" via name and the rest is just "button" by name.

I have a grab image which has a simple script of:

if within (img "button", the MouseLoc) then
show img "XXX"
end if
if within (img "target", the MouseLoc) then
show img "XXX"
end if

Now when I use the MouseUp handler above it only works on 2 out of the 4 possible "button" target options.

I have tried both intersect() and within with the same results. Is there something I am missing?

Thanks all

I have checked the docs but nothing relevant that I can see.

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Intersect() or Within Issues

Post by Klaus » Thu Dec 12, 2013 2:45 pm

Hi istech,

I think the problem is that you have 4 images with the SAME name on your card.
If LC accesses an object by NAME, it will always use the object with that name at the lowest layer!
Means, LC only checks if the FIRST (lowest layer) image named "button" is intersecting your other image.

But how is your script:

Code: Select all

...
if within (img "button", the MouseLoc) then
show img "XXX"
end if
if within (img "target", the MouseLoc) then
show img "XXX"
end if
...
triggered?
Mouse-up/down/enter/move?


Best

Klaus

istech
Posts: 194
Joined: Thu Sep 19, 2013 10:08 am

Re: Intersect() or Within Issues

Post by istech » Thu Dec 12, 2013 6:43 pm

Hi Klaus,

I think you are right Klaus. I have changed the layer before had the same problem but different image. So if I was to set it using the IDs instead it should be fine? Or should I go at it in the different direction.

I just used this:

on mouseDown
grab me
end mouseDown

on mouseUp
if within (img "button", the MouseLoc) then
show img "XXX"
end if
if within (img "target", the MouseLoc) then
show img "XXX"
end mouseUp

Thanks again Klaus you are a life saver and a credit to the forums.

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Intersect() or Within Issues

Post by Klaus » Thu Dec 12, 2013 8:05 pm

Hi istech,

I would name the images:
button1
button2
button3
button4

And then use a repeat loop to check ALL of them, that WILL work.
Having multiple objects with the same name IS asking for trouble :D

Code: Select all

on mouseUp
  repeat with i = 1 to 4
    if within (img ("button" & i), the MouseLoc) then
     show img "XXX"

    ## Only ONE hit possible, right? Otherwise delete this line
     exit repeat
   end if
  end repeat
  if within (img "target", the MouseLoc) then
    show img "XXX"
 end if
end mouseUp
Best

Klaus

istech
Posts: 194
Joined: Thu Sep 19, 2013 10:08 am

Re: Intersect() or Within Issues

Post by istech » Fri Dec 13, 2013 12:42 pm

Thanks again Klaus

As always your coding touch works and advise followed :lol:

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”