Button

Moderators: LCNeil, heatherlaine, kevinmiller, elanorb

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

Button

Post by marcelloe » Mon Mar 03, 2014 2:28 am

I have figured out how to make the buttons to be random on the stack when it is opened. I am not sure how to make sure the button aren't hidden behind each other. I am getting an error at the following line: set the loc of button i to line i of tLocList.

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
Thanks

Mark

LCNeil
Livecode Staff Member
Livecode Staff Member
Posts: 1223
Joined: Wed Oct 03, 2012 4:07 pm

Re: Button

Post by LCNeil » Tue Mar 04, 2014 3:52 pm

Hi Mark,

It would seem that you have a variable and function named the same. I would recommend changing one of these as it will reduce any confusion if they are used extensively though out your script (e.g. replace tLocList with gLogList)

In regards to your script, it would seem that you are generating a list of your buttons pre-existing locations and then assigning the loc buttons to a random value from this list. If this is the case, then the position of the buttons will not change as all positions are pre existing. Only the buttons present in those positions will change.

You are also not calling your function tLocList anywhere so this will not be doing anything. The function also seems to be slightly wrong as well as you cannot set the location of a variable.

If you want to randomly generate buttons on your stack, then the easiest option would be something like

Code: Select all

on preOpenCard
 repeat with i = 1 to 10
      create button 
      set the name of the last button to "button"&&i
      set the loc of the last button to random(the width of this stack),random(the height of this stack)
   end repeat
end preOpenCard
From this, you should be able to use the intersect function to check if a button is overlapping and then deal with it accordingly.

Kind Regards,


Neil Roger
--
RunRev Support Team ~ http://www.runrev.com
——

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

Re: Button

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

Neil:

I have 10 buttons created and want to change their position randomly when the stack is open. I don't want to create any more buttons. Would the following script work the way I want it to. Do i want to relaunch the preOpenCard Handler? Is that the best way? Also, can I add -20 to the widith and height of stack, so the button aren't half off the card?

Code: Select all

on preOpenCard
   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
   if intersect (button "b" & i, button "b" & i) then
      pass on preOpenCard
   end if
end preOpenCard
thanks

Mark

LCNeil
Livecode Staff Member
Livecode Staff Member
Posts: 1223
Joined: Wed Oct 03, 2012 4:07 pm

Re: Button

Post by LCNeil » Wed Mar 05, 2014 5:32 pm

Hi Mark,

It looks like you received some great responses and a solution on your other post here-

http://forums.runrev.com/viewtopic.php? ... 139#p98139

In regards to checking if buttons are overlapping, then the intersect functions could be one way to do this. The following tutorial should be able to get you started with this function-

https://sites.google.com/a/pgcps.org/li ... collisions

Kind Regards,


Neil Roger
--
RunRev Support Team ~ http://www.runrev.com
——

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

Re: Button

Post by marcelloe » Wed Mar 05, 2014 9:58 pm

Neil:

This is what i have figured out and I have gotten confused from all the posts. I don't know how to move the buttons that are intersecting. I have used the script below, but it times out because I am pass back to preOpenCard. Put me in the right direction.

Code: Select all

on preOpenCard
   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
   repeat with i = 1 to 10
      if intersect (button ("b" & i), button ("b" & i)) then
        preOpenCard
   end if
   end repeat
end preOpenCard

LCNeil
Livecode Staff Member
Livecode Staff Member
Posts: 1223
Joined: Wed Oct 03, 2012 4:07 pm

Re: Button

Post by LCNeil » Thu Mar 06, 2014 3:57 pm

Hi Mark,

It seems like you are getting some great responses from the other button post that you are on.

Hopefully my following script will also give you some leads

Code: Select all

on preOpenCard
   
   lock screen
   repeat with x = 1 to 10
      create button "button"&x
   end repeat 
   unlock screen
   
   checkIntersect
   
end preOpenCard

on checkIntersect
   
   repeat with x = 1 to the number of buttons of me
      repeat with i = 1 to the number of buttons of me
         if x = i then
            next repeat
         end if
         if intersect (button x, button i) then
            set the loc of button x to random(the width of this stack), random(the height of this stack)
            checkIntersect
         end if
      end repeat
   end repeat
   
end checkIntersect
I am creating my buttons via script, but this can be ignored if you have already pre-populated your buttons. My custom check intersect handler ensures that no buttons overlap.

Kind Regards,


Neil Roger
--
RunRev Support Team ~ http://www.runrev.com
——

Post Reply

Return to “idea2app and Coding School”