Page 1 of 3
Add and Subtract
Posted: Sat Sep 20, 2008 11:44 am
by bjb007
Been trying to code a button so that
the label will increment on left mouse
click and decrement on right mouse click.
The right mouse click doesn't work as expected.
Code: Select all
on mouseDown theButton
if theButton is 1 then
add 1 to me
end if
if theButton is 3 then
subtract 1 from me
end if
end mouseDown
Any help appreciated.
Posted: Sat Sep 20, 2008 12:26 pm
by bn
try this:
Code: Select all
local myCounter
on mouseDown theButton
if theButton is 1 then
add 1 to myCounter
end if
if theButton is 3 then
subtract 1 from myCounter
end if
set the label of me to myCounter
end mouseDown
is there a specific reason why you want it on mouseDown? Usually I expect things to change when the mouse is up again.
hth
Bernd
Posted: Sat Sep 20, 2008 12:46 pm
by Mark
Hi Bernd,
Please use
Code: Select all
if... then
-- do something here
else... if... then
-- do something here
else
-- do something here
end if
It makes your code much more efficient.
Best,
Mark
Add and Subtract
Posted: Sat Sep 20, 2008 2:34 pm
by bjb007
Bernd
I find "mouseUp" rather strange and
haven't found anything that needs
either a "mouseUp" or a "mouseDown".
They seem to be interchangeable.
Windows languages normally use "click"
which seems more natural to me.
Mark
if..if...a case of too much coding!
Posted: Sat Sep 20, 2008 2:46 pm
by malte
bjb,
I never thought of them to be interchangeable. These are clearly 2 different actions and actually there is a third one. mouseRelease, which is invoked, if the mouse came down over a control and is released out of it's bounding rect. This allows a user to still "cancel" the action if they push the mouse and release it outside of the controls rect. Many applications (at least on the Mac) behave this way.
All the best,
Malte
Add and Subtract
Posted: Sat Sep 20, 2008 3:09 pm
by bjb007
malte
Perhaps they aren't interchangeable.
Can you give me an example of where
and why you'd use one rather than the
other?
Add and Subtract
Posted: Sat Sep 20, 2008 3:50 pm
by bjb007
Still can't get it to work.
Have put a stack in Rev Online.
user: bjb007
title: AddSubtractButton
Posted: Sat Sep 20, 2008 3:59 pm
by bn
bjb007,
as malte points out, if you click on a button, keep the button down, move off the button because you clicked by mistake of changed your mind, the mouseUp will not reach the button. With mousedown no chance to do similar.
Also, some people click, say, more dilligently. They would definitely see the change of the label on mouseDown.
On a Mac every program I know behaves "the mouseUp way" and I get a 'funny' feeling if a program does it differently. If the user experience is not consistent, the user gets nervous and will not 'trust' such programs, as good as they might be.
Also, if you go the mouseUp way, you can use the mouseDown to calculate a couple of really long prime numbers in a
repeat until the mouse is up -statement. With those fast computers we have today... just joking
regards
bernd
Posted: Sat Sep 20, 2008 4:24 pm
by bn
bjb,
Code: Select all
local myCounter
on mouseUp theButton
if myCounter = "" or myCounter is not a number then put 0 into myCounter
if theButton is 1 then
add 1 to myCounter
else if theButton is 3 then
subtract 1 from myCounter
end if
put myCounter into field fldCount
set the label of me to myCounter
end mouseUp
this should work. (Mark, I took your advice)
In your script you put some commands outside a handler, that does not work, outside of a handler, by that I mean everything that is outside of an
on handlername
-- your commands go here
end handlername
structure. Outside you only declare global or local variables or constants, dont put any command there, they will never be seen. I was actually surprised that it compiles at all.
The
if myCounter = "" or myCounter is not a number then put 0 into myCounter
I added was just to move the things you tried outside of the handler inside, I dont think this line of code is necessary at all. But that would be the place to do something.
cheers
bernd
Add and Subtract
Posted: Sun Sep 21, 2008 8:49 am
by bjb007
Still not working - very strange.
After clicking the "Clear" button and
having myCounter and the button label
showing zero the first left-click on the
button changes both to 21 or whatever
number was there before clicking "Clear".
Sometimes the right-click
subtracts and sometimes it adds in
an apparently random manner.
Wonder where that number comes from?
I suspect the mysterious "me".
Posted: Sun Sep 21, 2008 9:20 am
by Mark
Hij BJB,
I don't think you mentioned the clear button before. What's the script in that button? What's the current script in the button with the changing label?
Best,
Mark
Posted: Sun Sep 21, 2008 10:38 am
by bn
hi bjb,
it is not the mysterious me, it is the difference between a local and a global variable.
I'm sorry, in my last post I just looked at your button that does the counting.
But here you have a good example of the difference in scope of the variables. For your stack you could go 2 ways.
If you want to have a field that indicates the number of mouseUp then you could go this way
Code: Select all
on mouseUp theButton
if field "fldcount" = "" or field "fldcount" is not a number then put 0 into myCounter
else put field "fldcount" into myCounter
if theButton is 1 then
add 1 to myCounter
else if theButton is 3 then
subtract 1 from myCounter
end if
put myCounter into field fldCount
set the label of me to myCounter
end mouseUp
This way you have the persistence of the value in the field. This works with the stack you have posted on revonline.
More generally speaking of persistence of a variable: the use of local myCounter did just that, myCounter persisted between calls, but only the script of the button had access to myCounter.
If you declare myCounter as a global variable then you could have declared it also in the script of the clear button and you would have access to myCounter. Then you could have 'cleared' myCounter from whatever script where you had declared myCounter as a global variable.
Does all this matter at all if you could stuff the value of a varible into a field for persistence? You would not want to have more fields than necessary, because accessing a field is slower than a local variable. But if you want to have an easy way to preserve a variable betwen restarts then you might want to put it into a field. (Another way would be to store it in a private property, but you have to get used to using private properties which at first sight are not as intuitive as a field)
The mysterious me is actually not that mysterious at all. It refers to the object where you use it, you just have to specify what you want. You could say "set the label of me to "JustALabel"" and a button would duely set its label to JustALabel, just telling a button from its own script "subtract one from me" doesnt help the button because me is a reserved keyword and not a variable that you can script into. So with me you address properties of an object.
Sorry if this is a little long but I think you have to sort this out somehow. It reminds me of my learning of foreign languages (computer or others) At first there is a long time of frustration: I know what I want to say but nobody wants to understand me... After a while the language and I get along, et voila...
Keep the problems coming,
cheers
Bernd
Add and Subtract
Posted: Mon Sep 22, 2008 2:17 am
by bjb007
Can't figure this out.
The code works.
With the left-click addition is fast.
With the right-click the subtraction is
slow - so that if I click too soon it
does an addition.
As this is only a few lines of code is
there a reason for the slow processing
of the right-click?
Perhaps a mouse problem - something that
I've come to expect after many years. I've
had more "mice" than I can remember and
the more sophisticated the mouse the shorter
its life.
Perhaps time for another one - after barely
three months.
Posted: Mon Sep 22, 2008 9:18 am
by Mark
Hi BJB,
How much slower is the right-click? If it is something like a quarter of a second, not really slow but still noticeable, it might be because of Revolution's front scripts. If you try the same in standalone, you shouldn't see this problem anymore.
Is the script you use exactly the same as the script posted by Bernd or have you changed anything? If you made a change, no matter how small, post it again.
Best,
Mark
Posted: Mon Sep 22, 2008 9:45 am
by bn
Hi bjb,
it has to do with the mouseDoubleUp message that gets sent when you click fast enough. with the right mouse it somehow adds instead of subtracts. I tried to figure it out with the message watcher but couldnt realy determine the sequence of events.
Anyway this cures it for me. Just add the following code to your button.
Code: Select all
on mouseDoubleUp theButton
if field "fldcount" = "" or field "fldcount" is not a number then put 0 into myCounter
else put field "fldcount" into myCounter
if theButton is 1 then
add 1 to myCounter
else if theButton is 3 then
subtract 1 from myCounter
end if
put myCounter into field fldCount
set the label of me to myCounter
end mouseDoubleUp
it is exactly the code from the mouseUp now in a mouseDoubleUp handler.
Does anybody have an explanation for this behaviour?
regards
bernd