Using "backgroundColor" to count....

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

Using "backgroundColor" to count....

Post by bjb007 » Tue Jun 03, 2008 2:43 am

I want to keep track of buttons which
have been clicked. As the "backgroundColor"
changes when clicked I've tried to use that
to count the number of buttons clicked but
not been able to get a result so far.

repeat with i = 1 to 30
put "btn" & i into btnTarget
if backgroundColor of button btnTarget not "200,255,255" then
add 1 to cTot
end if
end repeat
put cTot into field lblColorCount

Suggestions appreciated.
Life is just a bowl of cherries.

benjibeaumont
Livecode Staff Member
Livecode Staff Member
Posts: 53
Joined: Tue Jun 03, 2008 10:31 am

Visited Property

Post by benjibeaumont » Tue Jun 03, 2008 10:43 am

Hi

You can use the 'visited' property of the button. When a button is clicked, the visited property will be true.

put the visisted of button 'x'

If you are trying to be a little more advanced and track items over a longer period I would use a custom property which I set to 'true' or a count everytime the button is clicked. You can then iterate of the controls of your stack and check your own custom property.

I hope that helps.

Ben
Ben Beaumont | Runtime Revolution

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Tue Jun 03, 2008 11:44 am

bjb,

What you are trying to do might be possible, but I wouldn't recommend your approach.

You can use the visited property, but often this doesn't give me sufficient control. For example, navigating through cards automatically resets the visited property, which is not always desired.

You should save the status of a button in a custom property (there are other acceptable solutions, of course). Try this:

Code: Select all

on mouseUp
  set theButtonClicked of the target to true
  -- rest of your script here
end mouseUp
Now you can get the number of buttons the were clicked on by the user:

Code: Select all

function buttonsClicked
  put 0 into myCounter
  repeat with x = 1 to number of buttons
    if the theButtonClicked of btn x is true then
      add 1 to myCounter
    end if
  end repeat
  return myCounter -- myCounter buttons have been clicked
end buttonsClicked
You also need a way to reset the custom properties.

Code: Select all

on preOpenStack
  repeat with x = 1 to number of buttons
    set theButtonClicked of btn x to false
  end repeat
  pass preOpenStack
end preOpenStack
Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

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

Re: Using "backgroundColor" to count....

Post by Klaus » Tue Jun 03, 2008 12:15 pm

bjb007 wrote:...
if backgroundColor of button btnTarget not "200,255,255" then
..
This line gives me an error!


One of these lines does not:

Code: Select all

...
 if backgroundColor of button btnTarget IS NOT "200,255,255" then
...
 if backgroundColor of button btnTarget <> "200,255,255" then
...
So your solution does work, but only with the correct syntax!

bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

Using "backgroundColor" to count....

Post by bjb007 » Tue Jun 03, 2008 2:55 pm

Thanks to you all.

Problem with trying the various alternatives
was that some of them gave an error saying
that the "then" was missing from the "if"
statement when it wasn't, so couldn't test
all possibilities because when this
message appeared and I saved and ran
an error came up for the "on mouseup"
button code.

Very strange.
Life is just a bowl of cherries.

gyroscope
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 404
Joined: Tue Jan 08, 2008 3:44 pm
Contact:

Post by gyroscope » Tue Jun 03, 2008 3:11 pm

Hi bjb007

I've had this prob big time a couple of weeks ago and it took me hours to work out what was going on (including pairing down my if...then statement to one line). At a guess, if your stack includes a substack, your prob might be from similar cause:

Put a blank preOpenStack, OpenStack and/or CloseStack handler in your subStack script (depending what you've put in your main stack script); then possibly put your script in an onOpenCard handler.

Hope that sorts it for you...

:)

PS Why it jumps at watertight, good ol' if...then statements as an error, Ive no idea...

bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

Using "backgroundColor" to count....

Post by bjb007 » Wed Jun 04, 2008 12:16 am

gyroscope

In my case the problem definitely was
something to do with the "if" statement
because when I commented it out
both errors disappeared.

Must be one of the "Magnificent 400"
bugs in 2.8.1!
Last edited by bjb007 on Fri Jun 06, 2008 7:57 am, edited 1 time in total.
Life is just a bowl of cherries.

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

Post by Klaus » Thu Jun 05, 2008 8:47 am

I tested your exact script with the CORRECT syntax (see my first post) and it worked without any problems:

Code: Select all

...
put 0 into cTot
repeat with i = 1 to 30 
  put "btn" & i into btnTarget 
  if backgroundColor of button btnTarget <> "200,255,255" then 
    add 1 to cTot 
  end if 
end repeat 
...
Why are you asking for help here if you do not try out our proposals?

There is nothing wrong with the "if...then..." as you stated, if you use the correct syntax for your comparison like "<>" or "IS not".

bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

Using "backgroundColor" to count....

Post by bjb007 » Fri Jun 06, 2008 8:01 am

Klaus

I did indeed use your suggestion of "<>"
which works.

My comments since your original post about
problems I had refer to my attemps to get
the code to work before I asked the question.

Long and short of it -- I try a lot of alternatives
before I post here and when I get an answer I
use it.
Life is just a bowl of cherries.

Post Reply