Intersecting Buttons

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9665
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Intersecting Buttons

Post by dunbarx » Tue Mar 04, 2014 4:31 pm

For the doSomethingQuick Handler: This is what I think needs to happen. If buttons are touching then move again. Is that close?
Not just close, exactly. You can do this in many ways. My suggestion, though, is to create a list that does not have this problem in the first place. In other words, make sure that all the locs are sufficiently separated so that no button will overlie another. Any ideas?

Craig

marcelloe
Posts: 140
Joined: Thu Oct 17, 2013 2:26 pm

Re: Intersecting Buttons

Post by marcelloe » Tue Mar 04, 2014 4:41 pm

not sure what you mean about creating a list. This is what I was think?

Code: Select all

if intersect (button "b" & i, button "b" & i) then
  pass on preOpenCard
end if
Thanks

Mark

Newbie4
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 327
Joined: Sun Apr 15, 2012 1:17 am
Location: USA
Contact:

Re: Intersecting Buttons

Post by Newbie4 » Tue Mar 04, 2014 5:02 pm

You are getting off base and getting confused by the different posts. Klaus was helping you fix basic errors. Dunbarx was talking about fixing the intersecting buttons. Their advice was good but you are mixing up the suggestions. If you pre-arrange your buttons to spots that do not intersect, you will not need to perform and corrective actions. Right now, the locations you are using are messed up and overlapping or incorrect.

I was working with your original code. It was basically correct. What you are running into now are the result of your original list. It has become corrupted or your buttons misplaced. Let us start fresh

Go back to your first post and that code. Change it to the following:

Code: Select all

global tLocList

on preOpenCard
    scrambleBtns
end preOpenCard

on scrambleBtns
   repeat with i = 1 to 10
     put the loc of button i & CR after tLocList
   end repeat
  delete char -1 of tLocList
  sort lines of tLocList by random(100)
  repeat with i = 1 to 10
   set the loc of button ("b" & i) to line i of tLocList  --this line is changed
  end repeat
end scrambleBtns

(What I have done is moved your code to a separate handler so that you can test it and play with it. You do not have to close and open up the stack everytime to test your code. )

Make sure that your buttons are named "b1", "b2", ...."b10". Arrange them as you want.

To test your code, open up the message box and type "scrambleBtns".

If you get an error, look at your data (tLocList) and see what is wrong with it. Let us know what error you get and what the list looks like.

To look at your data (tLocList) at any time do the following: At the bottom window of your code screen is a tab labelled "variables". Click on that and scroll down to your variable "tLocList". Click on the magnifying glass to the right and you can see what your list looks like.

Is it correct? Do the points all look good?
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/

marcelloe
Posts: 140
Joined: Thu Oct 17, 2013 2:26 pm

Re: Intersecting Buttons

Post by marcelloe » Tue Mar 04, 2014 5:38 pm

that is working now, but I think I have a much simpler way to do what I want. I thin this script works the same as the one you just gave me. Yes I was getting confused with all the different suggestions.

Code: Select all

repeat with i = 1 to 10
     set the loc of button ("b" & i) to random (the width of this stack) , random (the height of this stack)
   end repeat

Newbie4
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 327
Joined: Sun Apr 15, 2012 1:17 am
Location: USA
Contact:

Re: Intersecting Buttons

Post by Newbie4 » Tue Mar 04, 2014 5:44 pm

That's great.

I was under the impression that the buttons were in set places and you just wanted to switch them around. But for putting them anywhere, that is the simplest solution.

Good work
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/

marcelloe
Posts: 140
Joined: Thu Oct 17, 2013 2:26 pm

Re: Intersecting Buttons

Post by marcelloe » Tue Mar 04, 2014 5:53 pm

Now I need to figure out the intersect thing to keep the buttons from getting behind each other.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9665
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Intersecting Buttons

Post by dunbarx » Tue Mar 04, 2014 5:59 pm

But you will still get overlapping with this. So the next step is to either:

1- Fix the overlap on the fly or in successive runs (less desirable and certainly less elegant)

2- Create the list of locs in such a way that no button is on top of one another. In other words, to create a list where each loc is sufficiently separated from all the other locs so that when you assign those locs to the buttons, they will not overlap. Can you think of ways to create such a list? It is a very nice exercise in the following rule:

"A dollar's worth of analysis is worth a million dollars worth of programming"

Craig

marcelloe
Posts: 140
Joined: Thu Oct 17, 2013 2:26 pm

Re: Intersecting Buttons

Post by marcelloe » Tue Mar 04, 2014 7:24 pm

1. Would the intersect function fix the overlapping issue.

2. i would assume the tLocList will gather the current locs? Am I getting the locs so the original position of all the buttons be reused, but with a different button? If not I am totally confused.

marcelloe
Posts: 140
Joined: Thu Oct 17, 2013 2:26 pm

Re: Intersecting Buttons

Post by marcelloe » Wed Mar 05, 2014 6:13 pm

Ok, first off I want to thank everyone for their help yesterday. I got confused and I think i am still confused. I have the script to make the buttons move. Now I need to focus on making sure they don't get behind each other. I am not sure if using the intersect function is the best way from keep them for touching. Also, not really sure on how to tell the touching buttons to do something else. I am I on the right track?

Thanks

Mark

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9665
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Intersecting Buttons

Post by dunbarx » Wed Mar 05, 2014 6:28 pm

Remember that thing I wrote
Create the list of locs in such a way that no button is on top of one another.
Let us say your buttons are all 80 pixels wide and 20 pixels high. Can you:

1- make a list of points that are, say, 100 pixels apart horizontally and 30 pixels apart vertically, that fill the entire card?
2- create a random sublist from that collection of points so that the sublist contains the same number of elements as the number of buttons?
3- Fill in what this step would likely be?

This is just one way to do this, but it seems like fun.

Write back.

Craig

marcelloe
Posts: 140
Joined: Thu Oct 17, 2013 2:26 pm

Re: Intersecting Buttons

Post by marcelloe » Wed Mar 05, 2014 6:51 pm

This might be a stupid question but how do I create that list? I have my buttons set to width to 80 and height to 80. to keep thing similar.

I think I figured out how to create the loc list thanks to Klaus. The list is the locs after the buttons have been moved. This is the script I used.

Code: Select all

put the loc of btn ("b" & i) & CR after tLocList

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9665
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Intersecting Buttons

Post by dunbarx » Wed Mar 05, 2014 7:25 pm

OK. You seem like a nice guy. But I want you to examine this handler until you know it so well you can can teach it to others.

Let us say the width of your card is 400, and the height is also 400.

I do not have your button arrangement, so I started with a blank card of the above rect, and am making new buttons. In the card script:

Code: Select all

on makeArray
   lock screen
   repeat with y = 1 to 3
      repeat with u = 1 to 10
         put y * 140 -60 & "," & u * 40 - 15 & return after temp
      end repeat
   end repeat
   --answer temp --if you want to see the list
   
   repeat the number of lines of temp
      create button
   end repeat
   
   repeat with y = 1 to the number of lines of temp
      set the loc of btn y to line y of temp
   end repeat
end makeArray
Now this array may or may not be to your liking, and the numbers are hard-coded, as opposed to using the properties of the environment to set the parameters, but you should see what I was up to. Now can you use this sort of thing in your project with the button suite you have? Can you combine the last two repeat loops into one? Hint: read up on the "last" keyword.

Craig

marcelloe
Posts: 140
Joined: Thu Oct 17, 2013 2:26 pm

Re: Intersecting Buttons

Post by marcelloe » Wed Mar 05, 2014 7:54 pm

Let me see if I understand what is going on. Correct me if I am wrong.

The follwoing script is creating a temp list of button positions.

Code: Select all

   lock screen
   repeat with y = 1 to 3
      repeat with u = 1 to 10
         put y * 140 -60 & "," & u * 40 - 15 & return after temp
      end repeat
   end repeat
The next part is just creating the buttons from the temp list.

Code: Select all

repeat the number of lines of temp
      create button
   end repeat
I am not sure what this script is doing. I think it is setting the loc or y value to the buttons.

Code: Select all

repeat with y = 1 to the number of lines of temp
      set the loc of btn y to line y of temp
   end repeat

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9665
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Intersecting Buttons

Post by dunbarx » Wed Mar 05, 2014 8:14 pm

Marcello.

Correct. If you comment out the "lock screen" command and step through the handler, you can see each step. Try it. Watch the variable "temp" through the first loop and watch the buttons as they are set to the locs. Note that when they are created, they are all placed at the center of the card. But they are stacking up at that point for sure.

The list is assembled in such a way that each loc is separated from the previous by an amount larger than the rect of a button.

Craig

marcelloe
Posts: 140
Joined: Thu Oct 17, 2013 2:26 pm

Re: Intersecting Buttons

Post by marcelloe » Wed Mar 05, 2014 8:34 pm

I like the fact that the buttons will be in random areas on the card each time it opens. I am not sure how I could use this array to make the buttons not overlap. If I am wrong steer me in the right direction.

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”