Re: +Help!
- Posted by David Cuny <dcuny at DSS.CA.GOV> May 13, 1998
- 822 views
(I think) Luis Campos wrote: > -- mouse press? > clicked =3D and_bits( mouse[1], LEFT_DOWN ) ----->??????? > -- some one knows the use of and_bits() in here???????? The 'and_bits' is needed because there can be more than one mouse event = at a time. For example, the mouse could move AND the left button could = be clicked. The flags are ORd together. In this case, the returned = result would be: MOVE AND LEFT_DOWN which equals=20 1 AND 2 which is, of course, 3. In this case, the test: if mouse[1] =3D LEFT_DOWN would not detect the mouse click event, because 2 !=3D 3. If you look at = the flags, they are set up in powers of 2: global constant MOVE =3D 1, LEFT_DOWN =3D 2, LEFT_UP =3D 4, RIGHT_DOWN =3D 8 RIGHT_UP =3D 16, MIDDLE_DOWN =3D 32, MIDDLE_UP =3D 64 This is not by accident - you store all the flags into a single byte, by = setting the _bits_ in the flag: 1 =3D bit 0 =3D 2^0 2 =3D bit 1 =3D 2^1 4 =3D bit 2 =3D 2^2 8 =3D bit 3 =3D 2^3 16 =3D bit 4 =3D 2^4 32 =3D bit 5 =3D 2^5 64 =3D bit 6 =3D 2^6 To look at a _single_ bit in the byte, you AND the bits together. So the = test: if and_bits( mouse[1], LEFT_DOWN ) works because 3 AND 1 is equal to 1, and a non-zero result is treated as true. Note that 'and_bits' is different than 'and'; 'and' returns true if both = values are non-zero. It works like this: function and( object o1, object o2 ) if o1 then if o2 then return 1 end if end if return 0 end function So the test: -- WRONG! if mouse[1] and LEFT_DOWN then tests to see if both values are non-zero. It doesn't do actually do a = bit-by-bit comparison of the values. Hope this helps! -- David Cuny