button to change color error

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
avengine
Posts: 6
Joined: Tue Apr 29, 2014 3:15 pm

button to change color error

Post by avengine » Wed May 07, 2014 12:05 am

I am following the tutorial in the link below to create buttons to change the text into different color, but I get error on

on mouseup
local tcolor

## use the target function to check which button was pressed
## the short name of the buttons are red, blue and green
## so we can use that to set the color
put the short name of the target into tcolor
set the textcolor of field "text" to tcolor
end mouseup

when I click apply and run it, I got this error

card id 1002: execution error at line n/a (Object: unknown color) near "card id 1002"

but when I click on the button, the color change is fine.
what might be wrong.
thanks.

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

Re: button to change color error

Post by Simon » Wed May 07, 2014 12:21 am

Hi avengine,
looks like you might not have set the name of your field to "text".
And don't forget that script is in your card script not the button script... in the example there are no scripts in the buttons unless you left them in.

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

avengine
Posts: 6
Joined: Tue Apr 29, 2014 3:15 pm

Re: button to change color error

Post by avengine » Thu May 08, 2014 9:22 pm

on the card 1002

I just put
on mouseUp
put the short name of the target into tColor
set the textColor of field "text" to tColor
end mouseUp

if I click apply and run
errors
card id 1002: excution error at line n/a (Bject: unknow color) near "card id 1002"

on the button just have on mouseUp

end mouseUp
they are empty inside

the text field did call it text already.

if I click the stop button on card id 1002 window and just go the card and with the run arrow to click the red, blue or green. the text can change color.
but my question is why the error show up and what might be wrong on this step. is there a finish livecode that i can download to see what is wrong on my part.
I have attach a file call button message test3.livecode
thanks.
Attachments
button message test3.jpg
this is the file that I have error

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

Re: button to change color error

Post by dunbarx » Thu May 08, 2014 9:41 pm

Hi.

Did you try to set the textColor of the field to the name of the card?

Valid colors are based on a palette resident in the program, like "red" or "cornflowerblue". Or explicitly referenced, as in "255,255,0".

But there is no color "card id 1002". If you change that line to substitute "red", does the problem go away?

Craig Newman

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

Re: button to change color error

Post by Simon » Fri May 09, 2014 2:31 am

Hi avengine,
on the button just have on mouseUp

end mouseUp
they are empty inside
You have to "pass mouseUp" otherwise the mouseUp message gets trapped in the button.
on mouseUp
pass mouseUp
end mouseUp
Or remove the mouseUp from the buttons all together.

I would suggest that you start with a new stack, your code does work.
card id 1002: excution error at line n/a (Bject: unknow color) near "card id 1002"
That comes when you click on the card and not a button.
The short name of the card is "card id 1002" :)


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

avengine
Posts: 6
Joined: Tue Apr 29, 2014 3:15 pm

Re: button to change color error

Post by avengine » Sat May 10, 2014 1:37 pm

I did what you suggest, but i hope i am just missing the way the syntax expected.
can you point me to what should i change on this?
as you know i am learning step by step in here, i really need to go thur this step.
thanks.
Attachments
button error.jpg
my screen shot of my error

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

Re: button to change color error

Post by Klaus » Sat May 10, 2014 4:05 pm

Hi avengine,

ouch! Please delete that script immediately! :shock:

Here is what happens:
You had the (almost) correct script at the beginning:

Code: Select all

on mouseup
  ## use the target function to check which button was pressed
  ## the short name of the buttons are red, blue and green
  ## so we can use that to set the color
  put the short name of the target into tcolor
  set the textcolor of field "text" to tcolor
end mouseup
This works, as long you click on the BUTTONS!
BUT when you click NOT on the buttons but somewhere on the CARD, then this:
## put the short name of the target into tcolor
will put the short name of the card into your variable -> "card id 1002"

And you will agree that this is not a color, right?

OK, what to do?
1. At first, remove all (empty) mouseup scripts from your buttons!

2. You need to also handle the case when a user does NOT click on your button.
You can do so by making sure "the target" is a button by checking "the name of the target".
That will give you
a. the TYPE of control -> button, field, card etc...
b. the NAME (short name) of the control

Examples:
button "Red"
card "card id 1002"
etc...

But we only check the TYPE in your case, which is the first WORD of the name of the target:

Code: Select all

on mouseup
  put the name of the target into tTargetName
  
  ## We check WORD 1 of this, if it is NOT button then we exit the handler immediately:
  if word 1 of tTargetName <> "button" then
     exit mouseup
  end if
 
  ##Now we have made sure that a BUTTON has been clicked and we can carry on without any error!
  put the short name of the target into tColor
  set the textcolor of field "text" to tColor
end mouseup
Please take alook at these stack., great learning resource to get the basics of Livecode:
http://www.hyperactivesw.com/revscriptc ... ences.html

Best

Klaus

avengine
Posts: 6
Joined: Tue Apr 29, 2014 3:15 pm

Re: button to change color error

Post by avengine » Tue May 13, 2014 4:21 am

Thanks Klaus
your example work very well, I need to learn your sample in detail.
and I can see that this can be a very powerful program.
thanks again for all the help in this forum.

Post Reply