I want to press a button and random generate one of 2 images

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
jnep95331
Posts: 6
Joined: Sat Apr 25, 2015 3:51 pm

I want to press a button and random generate one of 2 images

Post by jnep95331 » Sat Apr 25, 2015 4:04 pm

Hi Everyone,

For learning purposes, let's say that I want to create an app whose home screen is just a button. When the button is pressed, one of two images is randomly generated (as illustrated in the screenshot).
Screen Shot 2015-04-25 at 11.03.36 AM.png
How would I code this? I don't mean to sound lazy, so if someone could direct me to some sort of tutorial on this, I would equally appreciate it.

Thank you!

SparkOut
Posts: 2961
Joined: Sun Sep 23, 2007 4:58 pm

Re: I want to press a button and random generate one of 2 im

Post by SparkOut » Sat Apr 25, 2015 5:12 pm

There are so many ways to do this it would be impossible to go through all the ways here. (Well, with me using this phone to write this, I am not going to try anyway.)

With your stack of one card, place the button, and both images.

In the card script

Code: Select all

on openCard
 hide image "Image1"
 hide image "Image2"
end openCard
this will mean every time the user starts the app, neither image will be visible. In the button script

Code: Select all

on mouseUp
  put any item of "Image1,Image2" into tShowImg
  show image (tShowImg)
end mouseUp
this will pick a random image name from the list (of two here) and put it into a variable. Then the image with the name resolved from the variable is made visible. When you revisit the card the images will be hidden again to try again. You can hopefully see you can add to the list of image names to choose from a wider selection. You can also do other things like pick a random number with "random(2)" and concatenate the image name to show. Or many other options. Play, experiment, work out your own ways. Look up the scripting conferences for lots of great starting materials.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10220
Joined: Fri Feb 19, 2010 10:17 am

Re: I want to press a button and random generate one of 2 im

Post by richmond62 » Sat Apr 25, 2015 5:14 pm

I would suggest you have 2 images sitting on your card, img "WUN" and img "TOOO" and both
of them have their visible set to false so we cannot see them.

Then have this sort of code in a button:

on mouseUp
set the vis of img "WUN" to false
set the vis of img "TOOO" to false
put random(2) into XYZ
if XYZ = 1 then
set the vis of img "WUN" to true
set the vis of img "TOOO" to false
end if
if XYZ = 2 then
set the vis of img "WUN" to false
set the vis of img "TOOO" to true
end if
end mouseUp
SR.zip
Here's the stack
(2.92 KiB) Downloaded 223 times
WUN.png
WUN.png (7.68 KiB) Viewed 9539 times
TOO.png
TOO.png (7.67 KiB) Viewed 9539 times

jnep95331
Posts: 6
Joined: Sat Apr 25, 2015 3:51 pm

Re: I want to press a button and random generate one of 2 im

Post by jnep95331 » Sun Apr 26, 2015 2:56 am

Thank you so much, both of you!

One question - when it puts random(2) in XYZ, what exactly is XYZ?

Thanks!

jnep95331
Posts: 6
Joined: Sat Apr 25, 2015 3:51 pm

Re: I want to press a button and random generate one of 2 im

Post by jnep95331 » Sun Apr 26, 2015 3:16 am

Sorry - one more thing: let's say that instead of just image1 or image2 popping up with the button is pressed, I want two check marks to pop up along with those images.
Screen Shot 2015-04-25 at 10.11.32 PM.png
Is there a way to collectively name image1, check1, and check2 as one item, and image2, check3, and check4 as a second item, so that all three items pop up when I use the button code "on mouseUp put any item of "Image1,Image2" into tShowImg"

SparkOut
Posts: 2961
Joined: Sun Sep 23, 2007 4:58 pm

Re: I want to press a button and random generate one of 2 im

Post by SparkOut » Sun Apr 26, 2015 8:14 am

Select the items you want to treat as a group and click the "group" button in the menu bar. Then do the same with the other things so you have two (or more, if you require) groups. Make sure you give the groups sensible names. Then change the button script to refer to the groups, not the individual images:
put any item of "myGroup1,myOtherGroup" into tShowMe
show group (tShowMe)

I will let you work out how to adjust things to hide the groups.


XYZ in Richmond's example is a variable, just the same as tShowMe above. You have only a few restrictions on what you can name it, it is just a container you make to hold data and its contents may be varied. Very often you will see the "Hungarian-lite" convention of prefixing variable names by type, but this is only a convention (although one I would recommend).

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10220
Joined: Fri Feb 19, 2010 10:17 am

Re: I want to press a button and random generate one of 2 im

Post by richmond62 » Sun Apr 26, 2015 8:25 am

'XYZ' is just a variable holder generated on-the-fly to get the "magic number", i.e. the one generated by random(2) to
where it is needed on the next line of the script.

If you are asking questions such as "what is XYZ" you need to sit down and do some fairly basic homework before
you go any further, and by "basic homework" I mean (shock, horror) READING, to understand what

VARIABLES and CONSTANTS are.

There are some people "out there" who would have you believe that you can just sit down in front of a computer,
open the programming environment, and "away you go". This is NOT TRUE, and slightly disingenuous.

Like any other language or system of logic, a good grounding in First Principles will ensure you
don't come badly unstuck later on down the line.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10220
Joined: Fri Feb 19, 2010 10:17 am

Re: I want to press a button and random generate one of 2 im

Post by richmond62 » Sun Apr 26, 2015 8:35 am

I want two check marks to pop up along with those images.

No sooner said than done:

on mouseUp
set the vis of img "WUN" to false
set the vis of img "TOOO" to false
set the vis of btn "Check 1" to false
set the vis of btn "Check 2" to false
set the vis of btn "Check 3" to false
set the vis of btn "Check 4" to false
put random(2) into XYZ
if XYZ = 1 then
set the vis of img "WUN" to true
set the vis of img "TOOO" to false
set the vis of btn "Check 1" to true
set the vis of btn "Check 2" to true
set the vis of btn "Check 3" to false
set the vis of btn "Check 4" to false
end if
if XYZ = 2 then
set the vis of img "WUN" to false
set the vis of img "TOOO" to true
set the vis of btn "Check 1" to false
set the vis of btn "Check 2" to false
set the vis of btn "Check 3" to true
set the vis of btn "Check 4" to true
end if
end mouseUp
SR2.zip
Here it is.
(3.13 KiB) Downloaded 231 times
WUN2.png
WUN2.png (9.93 KiB) Viewed 9448 times
TOO2.png
TOO2.png (10.03 KiB) Viewed 9448 times
The chap who suggested using groups has a more elegant solution; mine is rude, crude, and just right for beginners!

Now, if that should suffice for you to get on with things by yourself!

SparkOut
Posts: 2961
Joined: Sun Sep 23, 2007 4:58 pm

Re: I want to press a button and random generate one of 2 im

Post by SparkOut » Sun Apr 26, 2015 8:37 am

Well said Richmond. If I wasn't using my phone to visit this morning I would have said more on these lines and directed to tutorials and guides and the Scripting Conferences.

jnep95331
Posts: 6
Joined: Sat Apr 25, 2015 3:51 pm

Re: I want to press a button and random generate one of 2 im

Post by jnep95331 » Sun Apr 26, 2015 10:34 pm

Thanks everyone.

jnep95331
Posts: 6
Joined: Sat Apr 25, 2015 3:51 pm

Re: I want to press a button and random generate one of 2 im

Post by jnep95331 » Mon Apr 27, 2015 1:29 am

One step farther: let's say that when I press a button, I want 1 set of objects out of 100 possible sets to appear. Is there any way to lay out these sets of objects on separate cards, or do I have to stack all 100 sets on top of each other within the same card? If the latter is the case, editing seems like it would be a nightmare.

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

Re: I want to press a button and random generate one of 2 im

Post by dunbarx » Mon Apr 27, 2015 5:23 am

Hi.

You really need to learn the basics of LC. That said, there is no better way than experimenting with real stacks. But do read the lessons and tutorials, the user guide and the dictionary, etc.

Anyway, the trick here is to set up a reference system that will allow you to use LC tools to advantage. Say you had your 100 objects (perhaps images?), and named them in a systematic way, for example:

img1
img2
img3
etc.

Let us say that all 100 were hidden, (they can be placed anywhere on screen, even on top of each other). Now you can do something like this in a button:

Code: Select all

on mouseUp
  get random(100)
  show image "img" & it
end mouseUp
Do you see how and why this would work? Do you see how you might, in a subsequent click on that button, remember the name of the last image shown, hide it, and get a new one? Can you do that three different ways? In ten???

Craig Newman

SparkOut
Posts: 2961
Joined: Sun Sep 23, 2007 4:58 pm

Re: I want to press a button and random generate one of 2 im

Post by SparkOut » Mon Apr 27, 2015 7:40 am

jnep95331 wrote:One step farther: let's say that when I press a button, I want 1 set of objects out of 100 possible sets to appear. Is there any way to lay out these sets of objects on separate cards, or do I have to stack all 100 sets on top of each other within the same card? If the latter is the case, editing seems like it would be a nightmare.
That's several steps off to the side. Yes, you can lay out different sets on different cards and pick a random one to display just as easily. But if you don't know how to go to one of the cards, you would be much better off with learning the general basics of Livecode first.

jnep95331
Posts: 6
Joined: Sat Apr 25, 2015 3:51 pm

Re: I want to press a button and random generate one of 2 im

Post by jnep95331 » Mon Apr 27, 2015 4:15 pm

The problem is that I don't even know where to begin with the manual. There are only a limited number of functions that I need to accomplish with Livecode, most of which I have already figured out by asking targeted questions on forums, and then extrapolating based on the replies. It's far more efficient and productive for me to learn by specific examples than just reading the whole user guide, as I'm not aiming to become adept at LiveCode at large.

That said, can you please direct me to a sample project/stack that illustrates how one can make an image from one card appear on another when a button is pressed?

Thanks!

SparkOut
Posts: 2961
Joined: Sun Sep 23, 2007 4:58 pm

Re: I want to press a button and random generate one of 2 im

Post by SparkOut » Mon Apr 27, 2015 5:20 pm

Two among the various sources of helpful info are:

http://lessons.runrev.com/

http://www.hyperactivesw.com/revscriptc ... ences.html

If you go back to your original idea, well, not the original one, the one with the 100 sets of options. There are still many ways to do this, but following your thoughts about laying out groups on different cards you have your first card as the one with the button. Check in the Application Browser that this is card number 1. Add as many other cards as you need, with the layouts you want. You know already that you can get a random number by using the random(x) function, where x is the maximum value you want to be returned, so random(9) will return a value in the range 1 to 9. So in the button script

Code: Select all

on mouseUp
  -- we don't want to go to card number 1 as this is the first card
  -- that we are using with the button
  -- so let's get a random number one less than the maximum card number
  put random(the number of cards of this stack - 1) into tRandomCardNr
  -- and then add 1 to it
  add 1 to tRandomCardNr
  -- so our range will be 2 to the number of cards in the stack
 -- now we have a random card number, let's go to it...
  go card (tRandomCardNr)
end mouseUp

Post Reply