Add and Subtract

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

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

Add and Subtract

Post by bjb007 » Sat Sep 20, 2008 11:44 am

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.
Life is just a bowl of cherries.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Post by bn » Sat Sep 20, 2008 12:26 pm

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

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

Post by Mark » Sat Sep 20, 2008 12:46 pm

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
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

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

Add and Subtract

Post by bjb007 » Sat Sep 20, 2008 2:34 pm

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!
Life is just a bowl of cherries.

malte
Posts: 1098
Joined: Thu Feb 23, 2006 8:34 pm
Contact:

Post by malte » Sat Sep 20, 2008 2:46 pm

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

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

Add and Subtract

Post by bjb007 » Sat Sep 20, 2008 3:09 pm

malte

Perhaps they aren't interchangeable.

Can you give me an example of where
and why you'd use one rather than the
other?
Life is just a bowl of cherries.

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

Add and Subtract

Post by bjb007 » Sat Sep 20, 2008 3:50 pm

Still can't get it to work.

Have put a stack in Rev Online.

user: bjb007
title: AddSubtractButton
Life is just a bowl of cherries.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Post by bn » Sat Sep 20, 2008 3:59 pm

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 :D

regards

bernd

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Post by bn » Sat Sep 20, 2008 4:24 pm

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

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

Add and Subtract

Post by bjb007 » Sun Sep 21, 2008 8:49 am

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".
Life is just a bowl of cherries.

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

Post by Mark » Sun Sep 21, 2008 9:20 am

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
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

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Post by bn » Sun Sep 21, 2008 10:38 am

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

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

Add and Subtract

Post by bjb007 » Mon Sep 22, 2008 2:17 am

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.
Life is just a bowl of cherries.

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

Post by Mark » Mon Sep 22, 2008 9:18 am

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
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

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Post by bn » Mon Sep 22, 2008 9:45 am

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

Post Reply