Page 1 of 1

Using "backgroundColor" to count....

Posted: Tue Jun 03, 2008 2:43 am
by bjb007
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.

Visited Property

Posted: Tue Jun 03, 2008 10:43 am
by benjibeaumont
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

Posted: Tue Jun 03, 2008 11:44 am
by Mark
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

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

Posted: Tue Jun 03, 2008 12:15 pm
by Klaus
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!

Using "backgroundColor" to count....

Posted: Tue Jun 03, 2008 2:55 pm
by bjb007
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.

Posted: Tue Jun 03, 2008 3:11 pm
by gyroscope
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...

Using "backgroundColor" to count....

Posted: Wed Jun 04, 2008 12:16 am
by bjb007
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!

Posted: Thu Jun 05, 2008 8:47 am
by Klaus
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".

Using "backgroundColor" to count....

Posted: Fri Jun 06, 2008 8:01 am
by bjb007
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.