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

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

Intersecting Buttons

Post by marcelloe » Mon Mar 03, 2014 5:35 pm

I have some what figured out how to make buttons move randomly when the stack is opened. I am having an issue at this line and not sure how to fix it. set the loc of button i to line i of tLocList. Also, how do I stop the button from getting behind each other?

Here is the code I am using.

Code: Select all

global tLocList

function tLocList
   set the loc of tLocList to the rec of this stack
end tLocList

on preOpenCard
   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 i to line i of tLocList
  end repeat
end preOpenCard
Thank you for your help

Mark

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

Re: Intersecting Buttons

Post by Klaus » Mon Mar 03, 2014 7:30 pm

Hi Mark,

1. you cannot name a function the same as a variable and vice versa!
Maybe you can, but that is asking for trouble! :D

2.

Code: Select all

function tLocList
   set the loc of tLocList to the rec of this stack
end tLocList
???
You cannot set the LOC of a VARIABLE holding a CR delimited list!
Maybe you can, but that is also asking for trouble! :D
And there is a T missing in RECT.

So what exactly is your issue with your repeat loop?
Do you get any errors? Do you get so far at all?
The syntax looks correct.

To avoid the buttons from getting behind each other, you will need to check and
keep track of all locations of all your buttons.

But since they obviously are already placed on the card by you, you only use the
current locs, why not place them correctly from the beginning? 8)


Best

Klaus

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

Re: Intersecting Buttons

Post by marcelloe » Mon Mar 03, 2014 7:58 pm

At this line:

Code: Select all

Set the loc of button i to line i of tLocList
I get the following error. execution error at line 14 (Handler: can't find handler) near "tLocList".

What do you suggest to fix the Loc of a Variable holding a CR?

Everything seems to be working from what I can tell.

I tried to use an IF statement using the intersect function, but could not figure it out.

To avoid the buttons from getting behind each other, you will need to check and
keep track of all locations of all your buttons.
How do I keep track of all the locs?

But since they obviously are already placed on the card by you, you only use the
current locs, why not place them correctly from the beginning? 8)
Not sure what you mean by the above statement?

Thanks

Mark

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

Re: Intersecting Buttons

Post by dunbarx » Mon Mar 03, 2014 8:11 pm

You cannot set the loc of a variable at all. It does not matter what that variable contains, and certainly variables can hold CR delimited lists. There is no "loc" property of a variable, in other words.

In your last code line, if there is a button i, and line i of tLocList contains a valid point reference, then that line will work. Have you checked to see if those two conditions are in fact met?

To keep track of all the locs, so you can find crashes, you need to check the intersect of each pair of buttons. Can you do this? Sort of like:

Code: Select all

repeat with y = 1 to the number of buttons
repeat with u = 1 to the number of buttons
  if intersect(button y,button u) then doSomeThingQuick
end repeat
end repeat
This is a little redundant. Can you see why? This may take a little time to run if you have a lot of buttons, so you may want to run this only at intervals. Are you OK with that as well? To avoid crashes, look up the intersect function, and experiment with its "threshold" parameter.

Write back, it sounds like you are making a fun project.

Craig Newman

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

Re: Intersecting Buttons

Post by marcelloe » Mon Mar 03, 2014 8:25 pm

All I really want to do is every time the stack opens the 10 buttons are randomly moved, but none are hidden. I fumbled my way through that script and for the most part it works minus the buttons getting behind each other. There might an easier way of doing this, but not sure how or am I on the right track?

Mark

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

Re: Intersecting Buttons

Post by dunbarx » Mon Mar 03, 2014 8:37 pm

Hi.

This is how it is done. You work, you fail, your work some more, you win.

Sometimes this sequence is run like a thousand pass repeat loop.

So what did you do with that script, when you stepped through it? You did put that code snippet into the openCard script and step through it, right? No, eh? You will want to add a "breakpoint" command at the beginning of the handler. What did LC do when it tried to find "doSomeThingQuick"?

Please run this like I said, and write back with what you find. What do you think we might come up with for the handler call "doSomeThingQuick"?

Craig

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

Re: Intersecting Buttons

Post by marcelloe » Mon Mar 03, 2014 9:11 pm

LC through up an error becuase there is not a Handler of doSomethingQuick. My orginal thought was to have it run back through the preOpenCard Handler.

I think before anything will work I need to figure out the loc and variable thing. I am not sure how to fix it at all.

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

Re: Intersecting Buttons

Post by dunbarx » Mon Mar 03, 2014 9:38 pm

Right. It would be worrisome if it did not.

So when you start up, I assume you set the locs of the various buttons to random values, and now and then some of those values overlap. correct? Maybe it is better to test those values (are they in tLocList?) before even setting the button locs. If you know the sizes of the buttons, you could manage the list beforehand and make sure that no two locs are close enough to cause an overlap. Just a thought.

Anyway, you need to make a handler named "doSomeThingQuick". If you just create it at the card or stack level, the error will go away. Do you see that? And now you can try to add the functionality to that handler that will cure the overlaps.

Again, whether you do this with the actual buttons or early on with the list of locs, the method would be similar.

Ready? Go.

Craig

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 » Mon Mar 03, 2014 9:47 pm

You have the right idea and your original code is almost correct. The problem is that you are placing all the buttons to the same location. I think I see what the cause is. First, get rid of the function tLocList. KLaus's advice was correct.

From the code, it looks like you named your buttons - 1, 2, 3, etc. That is usually not a good idea. Instead, change them to b1, b2, b3, ... or something similar.

Then in your last repeat loop where you place the buttons on the screen change the line:

Code: Select all

   set the loc of button i to line i of tLocList 
to

Code: Select all

   set the loc of button ("b" & i) to line i of tLocList  
and see if that works.
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 » Mon Mar 03, 2014 11:07 pm

Newbie4:

When I changed the script to what you suggested I get the following error.

Execution error at line 14 (Chunk: no such object) near "b1", char 20

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

Re: Intersecting Buttons

Post by dunbarx » Mon Mar 03, 2014 11:22 pm

Hi.

Newbie is not such a newbie anymore. Did you change the names of the buttons as he suggested? Should work if you did.

Do you see how LC evaluates the phrase "b" & i ("b" is a literal, and i is a variable that must be written as he showed) to create the control reference "button b1" (when i is "1")"?

Craig
Last edited by dunbarx on Tue Mar 04, 2014 4:01 pm, edited 2 times in total.

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

Re: Intersecting Buttons

Post by marcelloe » Tue Mar 04, 2014 3:56 pm

I have made all the changes Newbie suggested. Now I am getting the following error: execution error at line n/a (Object: coordinate is not a point) near "132,342202,200"

For the doSomethingQuick Handler: This is what I think needs to happen. If buttons are touching then move again. Is that close?

Mark

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

Re: Intersecting Buttons

Post by Klaus » Tue Mar 04, 2014 4:00 pm

Hi Mark,

a valid coordinate/point is a pair of 2 numbers, separated with a comma.
So do you think this is a valid coordinate: "132,342202,200" :D

Looks like you forget to add a CR when creating the list of locs?
I think this should be TWO points:
132,342
202,200


Best

Klaus

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

Re: Intersecting Buttons

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

Even when I add another tLocList I still get the same error.

Code: Select all

set the loc of button ("b" & i) to line i of tLocList tLocList

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

Re: Intersecting Buttons

Post by Klaus » Tue Mar 04, 2014 4:17 pm

marcelloe wrote:Even when I add another tLocList I still get the same error.

Code: Select all

set the loc of button ("b" & i) to line i of tLocList tLocList
:shock: :shock: :shock:

Please read my last posting again and check the part that creates your tLocList!

I highly recommend you to take a look at these stack to get the basics of Livecode:
http://www.hyperactivesw.com/revscriptc ... ences.html

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”