{Need Help!} Random Coord Generator and Moving Object There!

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
Bradyviz
Posts: 6
Joined: Thu Nov 18, 2010 8:42 pm

{Need Help!} Random Coord Generator and Moving Object There!

Post by Bradyviz » Tue Nov 23, 2010 8:41 pm

Is there a way to find random coordinates and then proceed to move an object there? This is another extension of the Cannonballs! Tutorial that I did and after the "intersect" command, I now am stuck on someone telling someone the sliders to move to a certain area and hitting the target. Anyone know what to do to make it generate random coordinates?

Thanks!

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4000
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: {Need Help!} Random Coord Generator and Moving Object There!

Post by bn » Tue Nov 23, 2010 8:47 pm

Hi Bradyviz,

look up the random function in the dictionary. There is also an example to get a random number from a lower/upper range. Do that for the x and the y coordinate and there you are.

regards
Bernd

Bradyviz
Posts: 6
Joined: Thu Nov 18, 2010 8:42 pm

Re: {Need Help!} Random Coord Generator and Moving Object There!

Post by Bradyviz » Tue Nov 23, 2010 9:10 pm

As you can probably now guess, I am REALLY feeling stupid right now. After the example, I still can't find a way to get the random number in any way whatsoever. When I click the button that is supposed to give me a number when I tested it, nothing happens. When I tell it to put the number into a field, it says there is an error. Any Tips?

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4000
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: {Need Help!} Random Coord Generator and Moving Object There!

Post by bn » Tue Nov 23, 2010 9:12 pm

Hi Bradyviz,

please post your script. You can use the "Code" tab and paste your script in there

regards

Bernd

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

Re: {Need Help!} Random Coord Generator and Moving Object There!

Post by Klaus » Tue Nov 23, 2010 9:43 pm

Hi Brad,

I also highly recommend to go through these stacks to get the basics of Livecode:
http://www.runrev.com/developers/lesson ... nferences/


Best

Klaus

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: {Need Help!} Random Coord Generator and Moving Object There!

Post by Mark » Tue Nov 23, 2010 9:55 pm

Hi,

It is not necessary to shout "need help" in the subject of your script. If a new thread is opened in any other section than the announcements section, it is most likely going to be a "need help" subject. Now, if you don't mean to ask for help, you still don't need to shout but you can write in small capitals and everybody will still hear you. Please, mind your netiquette.

Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

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

Re: {Need Help!} Random Coord Generator and Moving Object There!

Post by dunbarx » Wed Nov 24, 2010 5:58 pm

Is your problem just generating a random point? Try:

On mouseUp
put item 3 of the rect of this card into xMax
put item 4 of the rect of this card into yMax
put random(xMax) & "," & random (yMax) into tRandomCoord
answer tRandomCoord
end mouseUp

This will give you a point within the rect of the card window.

Craig Newman

Bradyviz
Posts: 6
Joined: Thu Nov 18, 2010 8:42 pm

Re: {Need Help!} Random Coord Generator and Moving Object There!

Post by Bradyviz » Tue Nov 30, 2010 8:39 pm

The code
dunbarx wrote: On mouseUp
put item 3 of the rect of this card into xMax
put item 4 of the rect of this card into yMax
put random(xMax) & "," & random (yMax) into tRandomCoord
answer tRandomCoord
end mouseUp
did work to make a random point. But the problem is I already have 2 rectangles in the card so it selects the sky (or grass, not sure) but not the rectangle I want. I am experimenting for an hour right now but if anyone has a an idea right now while this I am experimenting that knows please let me know!

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Location: Aalst, Belgium
Contact:

Re: {Need Help!} Random Coord Generator and Moving Object There!

Post by Janschenkel » Tue Nov 30, 2010 8:49 pm

Assuming you have a rectangle "Bounds" within which you want to determine a random point, try this:

Code: Select all

on mouseUp
  put (the left of graphic "Bounds") + random(the width of graphic "Bounds") & comma & \
      (the top of graphic "Bounds") + random(the height of graphic "Bounds") \
      into tNewPoint
  answer tNewPoint
end mouseUp
HTH,

Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4000
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: {Need Help!} Random Coord Generator and Moving Object There!

Post by bn » Tue Nov 30, 2010 8:56 pm

Bradyviz,

suppose you have an area (rectangle) where you want ramdom coordinates from.

Suppose the rectangle is the rectangle of a hidden graphic (areaOfInterest)

for the x coordinates you want a value between item 1 of the rect and item 3 of the rect
for the y coordinates you want a value between item 2 and item 4 of the rect.
I the dictionary entry for ramdom is a function:
function randomInRange lowerLimit,upperLimit
return random(upperLimit - lowerLimit + 1) + lowerLimit - 1
end randomInRange
supposet the rect is called tRect and has "100,400,200,500" as values.

you could code your script:

Code: Select all

on mouseUp
   put "100,400,200,500" into tRect
   put randominrange(item 1 of tRect, item 3 of tRect) into tX
   put randomInRange(item 2 of tRect, item 4 of tRect) into tY
   answer tX & comma & tY
end mouseUp)

function randomInRange lowerLimit,upperLimit
   return random(upperLimit - lowerLimit + 1) + lowerLimit - 1
end randomInRange
this would give you random coordinates within the rect that you want.

I just see that Jan has posted something that amounts to the same, I post nevertheless.

regards
Bernd

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”