Page 1 of 1

Switch and Count?

Posted: Sat Jan 05, 2013 6:31 pm
by MaxMax
hello, Is there a way in liveCode to set 'a switch' , so if something happens then set "switch" to true and if "switch" true then do something else..??

Also, whilst the "switch" is true, is there then a way to count the number of times that the 'on mouseup' has happened..? Then when the count = 5 then do something else.?

Thanks

Re: Switch and Count?

Posted: Sat Jan 05, 2013 6:45 pm
by jmburnod
Hi MAxMax,

If i understand what you want, this script should be useful

Code: Select all

-- cd script
local sNbClic

on opencard
   put 0 into sNbClic
end opencard

on debMybtn 
   add 1 to sNbClic
   put "You have clicked me" && sNbClic && "times" into Mes
   put mes into fld "fComment"
end debMybtn

-- btn script
on mouseup
   debMybtn
end mouseup
Best regards
Jean-Marc

Re: Switch and Count?

Posted: Sat Jan 05, 2013 8:07 pm
by MaxMax
Hi jmburnod, thanks for that, I'm guessing the switch is like a flag, that can be thrown when certain criteria are met.?

I have done this so far, but this does place the next button hit into the field "set2"

if field "fld1" = field "fld2"then
switch now
put label of the target into field "set2"
end switch
end if

Thanks

Re: Switch and Count?

Posted: Sat Jan 05, 2013 9:58 pm
by dave_probertGA6e24
Hi,

the command 'switch' is like a bunch of 'if' statements based on a value.

Code: Select all

if var1 = 100 then
 // do stuff
else
  if var1 = 200 then
    // do other stuff
  else
    // do yet more stuff
  end if
end if
That gets very messy when there are a lot of potential values for the 'var1' variable. The switch command neatens that to this:

Code: Select all

switch var1
  case 100
    // do stuff
    break
  case 200
    // do other stuff
    break
  default
    // do yet more stuff
end switch
The way you are trying to use it in your example is not really correct - it might work this time, but it's probably only a side-effect. Try to understand it properly before using it again, else you will end up getting very confused as to why it doesn't work the way you expect. Always use the dictionary to get a guide instead of guesswork.


I think the term 'switch' you are looking at is simply a flag variable (as jm shows) - but you can easily use a simple 'if' if you are only interested in a single value.

I hope that helps a bit,
Dave

Re: Switch and Count?

Posted: Sun Jan 06, 2013 1:18 am
by dunbarx
This is a perfect time to use a custom property to, exactly as you described, set a flag. Have you ever used these before?

In order to count "mouseUp' messages, you would have to pass that message in every script it appears, and accumulate that count, likely in another custom property. You can then test that property at any time, or, more likely perhaps, every "mouseUp", to see if it has reached five.

Do you know how to do this? Very simple and basic, but I do not know how familiar you are with LiveCode. For example, do you know why you have to pass "mouseUp" in every place and every time it is used? Write back if you need more help.

Craig Newman