I want to press a button and random generate one of 2 images
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
I want to press a button and random generate one of 2 images
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).
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!
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).
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!
Re: I want to press a button and random generate one of 2 im
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 scriptthis will mean every time the user starts the app, neither image will be visible. In the button scriptthis 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.
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 openCardCode: Select all
on mouseUp
put any item of "Image1,Image2" into tShowImg
show image (tShowImg)
end mouseUp-
richmond62
- 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
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
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
Re: I want to press a button and random generate one of 2 im
Thank you so much, both of you!
One question - when it puts random(2) in XYZ, what exactly is XYZ?
Thanks!
One question - when it puts random(2) in XYZ, what exactly is XYZ?
Thanks!
Re: I want to press a button and random generate one of 2 im
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.
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"
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"
Re: I want to press a button and random generate one of 2 im
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).
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

- Posts: 10220
- Joined: Fri Feb 19, 2010 10:17 am
Re: I want to press a button and random generate one of 2 im
'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.
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

- Posts: 10220
- Joined: Fri Feb 19, 2010 10:17 am
Re: I want to press a button and random generate one of 2 im
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
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!
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
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!
Re: I want to press a button and random generate one of 2 im
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.
Re: I want to press a button and random generate one of 2 im
Thanks everyone.
Re: I want to press a button and random generate one of 2 im
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.
Re: I want to press a button and random generate one of 2 im
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:
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
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 mouseUpCraig Newman
Re: I want to press a button and random generate one of 2 im
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 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.
Re: I want to press a button and random generate one of 2 im
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!
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!
Re: I want to press a button and random generate one of 2 im
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
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