Random button moves

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
KennyR
Posts: 256
Joined: Thu Jan 19, 2012 4:25 am

Random button moves

Post by KennyR » Wed Jan 29, 2014 6:36 am

Hello all...I am attempting to move some buttons around on a card when the card opens. No big deal but I am having troubles keeping them within the boundaries of the card. I have written a simple button script that randomly generates x,y loc and moves the button, but I am unsure what upper limit I need to use, or if I can pass multiple values in the random command to keep it within the boundary of the card...see below

Code: Select all

local vNewLoc,vOldLoc
on mouseUp
   put random(512) into item 1 of vNewLoc
   put  random(384) into item 2 of vNewLoc
   put the loc of me into vOldLoc
   move me from vOldLoc to vNewLoc in 10 ticks
   put empty into vNewLoc
end mouseUp
as soon as I figure this out, I am going to use the intersect command to keep the buttons from overlapping each other, but first I need to get this straight! Thanks all...

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Random button moves

Post by jmburnod » Wed Jan 29, 2014 10:43 am

Hi KennyR,

I see two ways
1. Using a loop that check the intersect for each loc with the risk to get a loop without end
2. Create a list of locations and get "randomed lines" of this list

Best regards

Jean-Marc
https://alternatic.ch

KennyR
Posts: 256
Joined: Thu Jan 19, 2012 4:25 am

Re: Random button moves

Post by KennyR » Wed Jan 29, 2014 3:24 pm

Thanks Jean-Marc,
I didn't consider using pre defined locations and using a loop to assign new locations. This method would prevent me from having to assess each buttons location to ensure each button doesn't overlap or intersect with another. I am currently working on this now and will report back with my code so others can see... 8)

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

Re: Random button moves

Post by dunbarx » Wed Jan 29, 2014 4:18 pm

Hi.

I like your way better than pre-defined locs, and there is nothing wrong with your handler. But what is happening to your buttons? Is it possible that the card window is smaller than the "512,342" you assign? If so, either lower those values or make the window bigger.

Remember that the loc is the center of an object, so you may have to deduct half the height and width of your values so that portions of the objects do not fall off the edge.

Craig Newman

KennyR
Posts: 256
Joined: Thu Jan 19, 2012 4:25 am

Re: Random button moves

Post by KennyR » Wed Jan 29, 2014 8:15 pm

Hey Craig...thanks for the response...here is what I am trying to do.. I have attached a pic of the program...it is a simple math program to teach my daughter her math facts. I just want to move the answers around the screen on openCard so she doesn't get used to where the answers are located on the screen. We will call the answers the "answersGroup" so as to keep the other buttons out of the movement process. The code I wrote logs the position of the "answers" only and puts them in a variable called vLoc. I then am trying to randomize that list and then use a loop to assign "random" locations for each button. I made a loop within a loop but it only works with unexpected results. Maybe you can give me a little insight on what I am doing wrong here..

Code: Select all

local vLoc
on mouseUp
   put 85,212 & cr & 171,318 & cr & 71,392 & cr & 187,484 & cr & 65,576 & cr & 951,162 & cr & 885,272 & cr & 969,372 & cr & 847,438 & cr & 951,586 into vLoc
   
   sort lines of vLoc by random(the number of lines of vLoc)
   repeat with x =1 to the number of btns in group "answers"
      repeat for each line tLine in vLoc
         set the loc of btn x to tLine 
      end repeat
   end repeat
  
   put empty into vLoc
end mouseUp
screen grab.tiff
screen grab.tiff (98.92 KiB) Viewed 5346 times

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Random button moves

Post by Simon » Wed Jan 29, 2014 10:08 pm

Wahoo... Kenny is back :)
I think you want to use the upper/lower random function

Code: Select all

  function randomInRange lowerLimit,upperLimit
return random(upperLimit - lowerLimit + 1) + lowerLimit - 1
  end randomInRange
Will keep the btns on the screen.

But I'm having a sneezing fit and I could have read everything wrong :)

Achoo
Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

KennyR
Posts: 256
Joined: Thu Jan 19, 2012 4:25 am

Re: Random button moves

Post by KennyR » Wed Jan 29, 2014 10:38 pm

SIMON! Missed ya buddy! I'll take a look and see what I can drum up...I'll report back with my mess.... :D

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Random button moves

Post by Simon » Wed Jan 29, 2014 10:50 pm

Code: Select all

set the loc of me to (random(512) + 55),(random(384) + 55)
That says your btn is a square 110x110 (loc is center)
Untested but I think that is right.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Post Reply