set location of a image to a random area on my screen

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

lourinho81
Posts: 9
Joined: Thu Dec 18, 2014 10:17 am

set location of a image to a random area on my screen

Post by lourinho81 » Thu Dec 18, 2014 2:51 pm

Hi,

I am new to live code. I have hit a little challange
I would like to set a location of an image to a specific area on my screen. lets say i have and area 100x100 (lets call this an egg tray). I have an image 10x10 (Egg) and i would like to load the image randomly in this area.

Appreciate any assistant that could push me in the right direction.

Lourinho81
:(

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: set location of a image to a random area on my screen

Post by Klaus » Thu Dec 18, 2014 3:00 pm

Hola Lourinho81,

1. welcome to the forum! :D

2. Please let me point you to these great learning resources:
http://www.hyperactivesw.com/revscriptc ... ences.html

3. To your question:
There is the random() function, which you can use.
First get some RANDOM points from your 100*100 area and then
set the LOC of your image to the newly "computed" X and Y values:
...
put random(100) into tNewX
put random(100) into tNewY
set the loc of img "egg" to tNewX,tNewY
...
Try this in a buttons "Mouseup" script and play around with the values to get a feel for what's happening here :D


Best

Klaus

lourinho81
Posts: 9
Joined: Thu Dec 18, 2014 10:17 am

Re: set location of a image to a random area on my screen

Post by lourinho81 » Thu Dec 18, 2014 3:06 pm

Hi Klaus,

Thank you for you advice. I will play around with your recommendation and have a look at the link you posted.

Much appreciated. :)

Regards
Lourinho81

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

Re: set location of a image to a random area on my screen

Post by dunbarx » Thu Dec 18, 2014 3:10 pm

Hi.

Welcome to the forum.

All controls, and an image is a control, have a property known as the "location". or the loc. So if you place an image on screen, and in the message box"

Code: Select all

answer the loc of image 1
You will see the x,y coordinates, based on the upper left corner of the stack window.

Now you have to do this for us. Read up on the "random" function. Know that the loc is a property of a control, and can be "set", like all properties. Can you assemble a handler that will generate those two values, within the constraints of your egg tray? Write back if you need more...

Craig Newman

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

Re: set location of a image to a random area on my screen

Post by Simon » Thu Dec 18, 2014 9:47 pm

Seems to me this is more like tick-tac-toe.
Lourinho81 doesn't really want a random pixel location but a random "box" to put the X or O in (err... egg/carton).
With a look-up table the precise locations could be pre assigned to numerical values... ummm
put random(12) into tEgg
switch tEgg
case 1 set the loc of img"Egg" to 100,100
break
case 2 set the loc of img"Egg" to 100,200
break
etc.
default
end switch

Have I said too much again?

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

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

Re: set location of a image to a random area on my screen

Post by dunbarx » Thu Dec 18, 2014 9:57 pm

Lourinho81

Is Simon correct? I read your request as if you had a fixed tray and wanted to place the image randomly within the extent of that tray. Simon believes that the tray itself is to be placed randomly.

In either case, the methodology is virtually the same, and the same effort would go into either. Are you able to work this out? If so, do both anyway. Write back.

Craig

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

Re: set location of a image to a random area on my screen

Post by Simon » Thu Dec 18, 2014 10:16 pm

Hi Craig,
Happy Holidays!
...a fixed tray and wanted to place the image randomly within the extent of that tray.
Actually that is sort ofwhat I meant... the tray is fixed it's the egg that moves... but the egg must be placed into a slot(?) ( I wonder what you call them?) which are not random in relation to the tray.
It isn't a baking sheet that cookie dough gets placed on anywhere but an ice cube tray where the water is placed in specific locations.

I wonder how many of these I can come up with, there must be a sports one.
Golf/hole not golf/green.. the cubby holes they place the baseball bats in? Checkers board (done games)

I was just on an airplane so
seat 52B not 52AB (ouch!)
Platform 9 3/4????

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

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

Re: set location of a image to a random area on my screen

Post by dunbarx » Thu Dec 18, 2014 11:18 pm

Simon.

Now I see what you meant.

So lourinho81, random placement among several fixed allowed possibilities? Or just random within certain bounds, period?

Craig

lourinho81
Posts: 9
Joined: Thu Dec 18, 2014 10:17 am

Re: set location of a image to a random area on my screen

Post by lourinho81 » Mon Dec 22, 2014 10:51 am

Hi Guys,

The egg tray is in a fixed position and i want to load the egg randomly in the area of egg tray.

cheers

lourinho81
Posts: 9
Joined: Thu Dec 18, 2014 10:17 am

Re: set location of a image to a random area on my screen

Post by lourinho81 » Mon Dec 22, 2014 12:09 pm

Hi Guys,

Im making diff and apps to test functionality so i can learn how to use livecode.
So i have set the following task for myself. I have an egg that is falling from the sky. Once the egg moves off the screen i would like the egg to load into a random area off the screen called eggtray.
I have attached a screen shot of my app.

###Start Of Code###
local sMoving

on preOpenCard
set the location of image "egg.png" to 564,168
put true into sMoving
moveobjects
end preopencard

command moveobjects
if sMoving then
set the bottom of image "egg.png" to the bottom of image "egg.png" + 5
end if
if the top of image "egg.png" >= bottom of graphic "screen" then
set the location of image "egg.png" to 564,168
end if
send "moveobjects" to me in 50 milliseconds
end moveobjects

###End Of Code###

Hopefully this will clear up any confusion.
Many thanks for your input and guidance
Attachments
fallingegg.PNG
fallingegg img

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

Re: set location of a image to a random area on my screen

Post by dunbarx » Mon Dec 22, 2014 4:23 pm

Hi.

I have not run your handler, but it seems that it resets the loc of the egg the moment it drops below the bottom of your screen image, so the egg drops and resets continuously. Is this so?

If it is, then you already have the thing working, mostly. Is your question then how to store the egg offscreen? Why not, instead of resetting to the starting loc, set the loc of the egg offscreen and exit the handler. You can exit either with an "exit to top", which will pass control right through the roof, or you can set the "sMoving" flag to "false".

Do you need help knowing where "offscreen" is? Will there be more than one image stored in this way? Also, do you really need to set the image loc offscreen? Could you just hide the image instead?

Craig

lourinho81
Posts: 9
Joined: Thu Dec 18, 2014 10:17 am

Re: set location of a image to a random area on my screen

Post by lourinho81 » Tue Dec 23, 2014 10:07 am

Hi,

I'm trying to load the egg randomly in egg tray off screen so that the player cant predict it trying to create the element of surprise.
I can get it to load in any random position but i want it to be random in that specific area.

Regards
Lourinho81

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

Re: set location of a image to a random area on my screen

Post by Simon » Tue Dec 23, 2014 6:05 pm

...but i want it to be random in that specific area.
Lourinho81, is the concept like "raining" eggs? Eggs falling down vertically but starting at different horizontal positions?
If so I can see why the code I provided is not exactly what you want.

Lets say the width of your play area (screen) is 768 and the egg size is 50x50 px

Code: Select all

put random(743- 25+ 1) + 25- 1 into tEgg 
set the loc of image "Egg" to tEgg,-25
That first line is a bit complicated to understand but what it does is restrict the result to between 25 (half the width of the egg) and 743.
The second line is to locate the egg offscreen.

This can be made more robust but I'll stop there until I hear if I'm going in the right direction.


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

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

Re: set location of a image to a random area on my screen

Post by dunbarx » Tue Dec 23, 2014 7:54 pm

Hi.

Lourinho81, do you mean that you do indeed want to restart the egg from the top, but from a new random location along that top? If so, think about this handler:

Code: Select all

on mouseUp
   put the width of img "egg.png" / 2 into tMin
   put the width of this cd  into tMax
   put "-" & the height of img "egg.png" into startY --see note below
   set the loc of img "egg.png" to random(tMax - ( tMin * 2)) + tMin & "," & startY
end mouseUp
You may want to test the handler with a value of startY that will allow it to appear ON screen each time you click, like 25.

Now, can you comment on how this works? It is wordy, so make your replies wordy as well.

Craig
Last edited by dunbarx on Wed Dec 24, 2014 3:27 pm, edited 1 time in total.

lourinho81
Posts: 9
Joined: Thu Dec 18, 2014 10:17 am

Re: set location of a image to a random area on my screen

Post by lourinho81 » Wed Dec 24, 2014 9:04 am

Thanks for your feedback guys, i'm going on vacation will try when i get back.

Rg
Lourinho81

Post Reply