Appearing 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
MMH
Posts: 2
Joined: Thu Oct 26, 2017 4:08 pm

Appearing Images

Post by MMH » Thu Oct 26, 2017 5:15 pm

Hi! I want to make a program where you can put into fields the size of angles of a triangle and the program will show an image according to the angles (right, scalene, isosceles, equilateral or error) this is the code I made, I have already put the fields and the images, in the image property I selected it to not be visible but I do not know why it does not work. And also the name of the img is correct.
on mouseUp

put field "1" into x
put field "2" into y
put field "3" into z

if x+y+z=180 and x=y and x=z and z=y
then set the visible of img "equilatero" to TRUE
else
if x+y+z=180 and x=y or y=z or x=z
then set the visible of img "isosceles" to TRUE
else
if x+y+z=180 and x=90 or y=90 or z=90
then set the visible of img "recto" to TRUE
else
if x+y+z=180 and x<y or x>y and x<z or x>z and y<z or y>z
then set the visible of img "escaleno" to TRUE
else
if x+y+z<180
then set the visible of img "error" to TRUE
else
if x+y+z>180
then set the visible of img "error" to TRUE
end if
end if
end if
end if
end if

end mouseUp

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

Re: Appearing Images

Post by Klaus » Thu Oct 26, 2017 5:36 pm

Hi MMH,

1. welcome to the forum! :D

2. Important hint: Do NOT and NEVER name your LC objects with a number!
That will confuse the engine and it will use the object with that LAYER number and probably not the object you want.
So -> field "1" will be treated as field 1 = the FIRST field on the card, which may NOT be -> field "1"

3. Please explain what exactly does not work?

4. Don't forget to also hide all images that are currently not needed in a condition!


Best

Klaus

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

Re: Appearing Images

Post by Klaus » Thu Oct 26, 2017 6:33 pm

Here my solution with lots of comments:

Code: Select all

on mouseUp
   ## Better name the fields like this:
   put field "x" into x
   put field "y" into y
   put field "z" into z
   
   ## I "outsourced" the command that does the work
   ## and execute it when one conditons = TRUE
   ## I also pass the name of the image that should be shown when a conditon is TRUE to that command
   
   if x+y+z=180 and x=y and x=z and z=y then
      hide_all_but_show "equilatero"
   end if
   
   if x+y+z=180 and x=y or y=z or x=z then 
      hide_all_but_show "isosceles"
   end if
   
   if x+y+z=180 and x=90 or y=90 or z=90 then
      hide_all_but_show "recto"
   end if
   
   if x+y+z=180 and x<y or x>y and x<z or x>z and y<z or y>z then
      hide_all_but_show "escaleno"
   end if
   
   if x+y+z<180 then
      hide_all_but_show "error"
   end if
   
   if x+y+z>180 then
      hide_all_but_show "error" 
   end if
end mouseUp

## "Outsourcing" lengthy handlers make the code more readable
command hide_all_but_show currentImage
   
   ## Always use this if you manipulate the visibility of objects!
   ## This way everything is faster and the user will NOT see any flickering on the screen!
   lock screen
   
   ## Create a list of all used images
   put "equilatero,isosceles,recto,escaleno,error" into tImages
   
   ## Now first hide all these images in a loop...
   repeat for each item tImage in tImages
      hide img tImage
   end repeat
   
   ## ... and only show the correct (for the current condtition) image
   show img currentImage
   
   ## Done, we can raise the "curtain" again
   unlock screen
   
   ## No need to continue the "mouseup" handler, so we simply leave all handlers here:
   exit to top
end hide_all_but_show
Best

Klaus

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Appearing Images

Post by [-hh] » Thu Oct 26, 2017 8:32 pm

Hi all, let's do first the math which is a little bit incorrect here.

The problem are the missing braces, because "and" binds stronger than "or".
You write for example:
    if x+y+z=180 and x=y or y=z or x=z
but you certainly mean, what is different:
    if x+y+z=180 and (x=y or y=z or x=z)

So assuming x and y and z are not negative, these are actually the OP's cases, having partly superfluous but correct conditions:

Code: Select all

if x+y+z=180 and x=y and x=z and z=y then
    set the visible of img "equilatero" to TRUE
else if x+y+z=180 and (x=y or y=z or x=z) then
    set the visible of img "isosceles" to TRUE
else if x+y+z=180 and (x=90 or y=90 or z=90) then
    set the visible of img "recto" to TRUE
else if x+y+z=180 and x <> y and x <> z and y<>z then 
    set the visible of img "escaleno" to TRUE
else if x+y+z <> 180 then
    set the visible of img "error" to TRUE
end if
One option to shorten this could be (what is the same math):

Code: Select all

if x+y+z <> 180 or x < 0 or y < 0 or z < 0 then
    set the visible of img "error" to TRUE
else if x=60 and y=60 then
    set the visible of img "equilatero" to TRUE
else if x=y or y=z or x=z then
    set the visible of img "isosceles" to TRUE
else if x=90 or y=90 or z=90 then
   set the visible of img "recto" to TRUE
else
   set the visible of img "escaleno" to TRUE
end if
Now I suggest to apply the LC-things, the clear handler hide_all_but_show of Klaus.

[Edit. Sorry, forgot in the first "short" version one case, now we have the OP's cases]
shiftLock happens

Newbie4
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 327
Joined: Sun Apr 15, 2012 1:17 am
Location: USA
Contact:

Re: Appearing Images

Post by Newbie4 » Thu Oct 26, 2017 10:06 pm

keep it simple

Code: Select all

hideAll
if x+y+z <> 180 then
    set the visible of img "error" to TRUE
else if x=y and x=z and z=y then
      set the visible of img "equilatero" to TRUE
else if x=y or y=z or x=z then
    set the visible of img "isosceles" to TRUE
else if x=90 or y=90 or z=90 then
    set the visible of img "recto" to TRUE
else  
    set the visible of img "escaleno" to TRUE
end if

command hideAll
   hide img "error"
   hide img "equilatero"
   hide img "isosceles"
   hide img "recto" 
   hide img "escaleno"
end hideAll
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/

capellan
Posts: 654
Joined: Wed Aug 15, 2007 11:09 pm

Re: Appearing Images

Post by capellan » Tue Oct 31, 2017 8:09 pm

Check this handler made with code picked from previous messages.
Just notice that you should create a text field named "resultado"
and rename the 3 fields as: "A1" "A2" "A3" (A for Angle)

Code: Select all

on mouseUp
   
   hideAll
   
   put field "A1" into x
   put field "A2" into y
   put field "A3" into z
   put empty into fld "resultado"
   
      -- equilatero have three equal angles
   if x+y+z=180 and x=60 and y=60 and z=60 then 
      set the visible of img "equilatero" to TRUE
      put "triangulo equilatero" into fld "resultado"
      exit to top
   end if
   
   -- isosceles have two equal angles
   if x+y+z=180 and (x=y or y=z or z=x) and (x<>90 and y<>90 and z<>90) then 
      set the visible of img "isosceles" to TRUE
      put "triangulo isosceles" into fld "resultado"
      exit to top
   end if
   
   -- recto have a single 90 degree angle
   if x+y+z=180 and (x=90 or y=90 or z=90) then 
      set the visible of img "recto" to TRUE
      put "triangulo recto" into fld "resultado"
      exit to top
   end if
   
   -- escaleno have three different angles
   if x+y+z=180 and (x<>y) and (x<>z) and (y<>z) then 
      set the visible of img "escaleno" to TRUE
      put "triangulo escaleno" into fld "resultado"
      exit to top
   end if
   
   if (x+y+z<180) or (x+y+z>180) then 
      set the visible of img "error" to TRUE
      put "error: la suma de los angulos no equivale a 180" into fld "resultado"
   end if
   
   put x+y+z
   
end mouseUp

command hideAll

   hide image "equilatero"
   hide image "isosceles"
   hide image "recto" 
   hide image "escaleno"
   hide image "error"
   
end hideAll
Tell us if this works!

Al

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”