Snapping objects/ buttons to locations

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
Noytal
Posts: 9
Joined: Sun Apr 10, 2016 6:10 pm

Snapping objects/ buttons to locations

Post by Noytal » Sun Apr 10, 2016 6:25 pm

So my IT course requires that we design and create a simple game. I decided to make a mini Professor Layton type of puzzle game. The first puzzle is one of the "rearrange the matches to make a different shape" ones, and I'm having some problems.

I have a set up with a bunch of buttons with an image of a matchstick set as the icon. I can make these move with

Code: Select all

on mouseDown
   Grab me
end mouseDown
But the problem I have is with the MouseUp code.
I tried this at first:

Code: Select all

on mouseUp
   if the loc of me is <20 graphic "Grid V1" then
      set the loc of me to loc of graphic "grid v1"
Followed by else if's for the other grid lines.
"Grid V1" refers to a bunch of equal sized lines I made and arranged like a grid. The idea is that you can snap the matches to the grid lines, and they will automatically rotate to fit onto that section of the grid.
But obviously that isn't the correct way to phrase that. I googled around and found something that kind of works, but it doesn't work all the time:

Code: Select all

on mouseUp
   if abs(item 1 of the loc of me - the left of graphic "Grid V1") < 20 then
      set the loc of me to loc of graphic "grid v1"
I don't really know what that means, and I think the "left" bit is wrong for me.

Is there some way I can get the matches to snap to the grid and rotate to be the same rotation as the grid line? Thank you :)

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Snapping objects/ buttons to locations

Post by dunbarx » Sun Apr 10, 2016 9:35 pm

Hi.

One at a time:
if the loc of me is <20 graphic "Grid V1" then
The loc must be defined by two integers separated by a comma.

Period.

So (you have to think to yourself) how does LC interpret the phrase " <20 graphic "Grid V1" as a pair of integers separated by a comma?

I am not being flip. I believe you have a concept in mind, but we must pay attention to the syntax of LC. Write back. When we fix this, we can look at the other...

Craig Newman

Noytal
Posts: 9
Joined: Sun Apr 10, 2016 6:10 pm

Re: Snapping objects/ buttons to locations

Post by Noytal » Mon Apr 11, 2016 11:47 am

Thanks, I managed to work something out.

Code: Select all

on mouseUp
   if intersect(button "Match U1", graphic "grid v1")then
      set the loc of me to loc of graphic "grid v1"
This works perfectly for my needs, the only thing now is working out how to make the matches rotate to align with the grid.

Post Reply