Hello @this_name_is_taken! Hello again @TinkerSmith!
@this_name_is_taken i tried to use your example. Let's see what I got.
Let me remind you what I'm trying to achieve: mouse buttons should be tracked separately and independently. In the examples below, a cube will be used. The cube can jump and change color.
The task set:
1.
The cube must jump once every time the left mouse button is pressed (in my examples, it can also jump only if it is on the ground). The cube should not jump if the button remains clamped.
2.
The cube should change color once every time the right mouse button is pressed (blink). The cube should not change color if the button is still clamped.
3.
The jump should not interfere with the color change of the cube. Changing the color of the cube should not interfere with the jump in any way.
4.
The "jump" must be performed exclusively with the left mouse button. The "color change" should be performed exclusively with the right mouse button.
Let's see if I managed to meet all the conditions.
I made my code examples based on what @this_name_is_taken replied to me.
Example 1:
init = function()
LeftMousePress=false
RightMousePress=false
end
update = function()
if mouse.press then
LeftMousePress=true
RightMousePress=true
end
if mouse.release then
LeftMousePress=false
RightMousePress=false
end
if LeftMousePress and cub_y==0 then
cub_vy=7
LeftMousePress=false
end
if RightMousePress then
cub_color="rgb(0,255,0)"
RightMousePress=false
else cub_color="rgb(255,0,0)"
end
cub_vy-=0.3
cub_y=max(0,cub_y+cub_vy)
end
draw = function()
screen.clear("rgb(142,217,255)")
screen.fillRect(0,-80,400,40,"rgb(0,170,170)")
screen.fillRect(0,-50+cub_y,20,20,cub_color)
end
It was obvious that it wouldn't work. The cube reacts to any mouse buttons and jumps and changes color at the same time.
Also, the cube cannot jump if the right mouse button remains clamped. In addition, it cannot change color if the left mouse button remains clamped. Thus, conditions 3
and 4
are not met.
Well, I excluded the color change to check if the right mouse button would interfere with the jump:
init = function()
LeftMousePress=false
end
update = function()
if mouse.press then
LeftMousePress=true
end
if mouse.release then
LeftMousePress=false
end
if LeftMousePress and cub_y==0 then
cub_vy=7
LeftMousePress=false
end
cub_vy-=0.3
cub_y=max(0,cub_y+cub_vy)
end
draw = function()
screen.clear("rgb(142,217,255)")
screen.fillRect(0,-80,400,40,"rgb(0,170,170)")
screen.fillRect(0,-50+cub_y,20,20,"rgb(0,255,0)")
end
It turns out that it interferes. Hold down the right mouse button and you will see that it is now impossible to make a jump!
In addition, the jump is still performed with any mouse button - which contradicts the condition. Therefore, I will significantly rewrite and shorten the code. Now I will also track the left mouse button at the same time as mouse.press.
update = function()
if mouse.press and mouse.left and cub_y==0 then
cub_vy=7
end
cub_vy-=0.3
cub_y=max(0,cub_y+cub_vy)
end
draw = function()
screen.clear("rgb(142,217,255)")
screen.fillRect(0,-80,400,40,"rgb(0,170,170)")
screen.fillRect(0,-50+cub_y,20,20,"rgb(0,255,0)")
end
Well, now the jump is made only by pressing the left mouse button. In addition, the code has become much shorter. But the right mouse button still interferes with making the jump.
Now I will return the ability to change the color of the cube:
Example 2:
update = function()
if mouse.press and mouse.left and cub_y==0 then
cub_vy=7
end
if mouse.press and mouse.right then
cub_color="rgb(0,255,0)"
else cub_color="rgb(255,0,0)"
end
cub_vy-=0.3
cub_y=max(0,cub_y+cub_vy)
end
draw = function()
screen.clear("rgb(142,217,255)")
screen.fillRect(0,-80,400,40,"rgb(0,170,170)")
screen.fillRect(0,-50+cub_y,20,20,cub_color)
end
Condition 3
is not met.
OK, I'll rewrite the first example. Now I use everything: the variables "LeftMousePress" and
"RightMousePress", as well as "mouse.left" and "mouse.right".
Example 3:
init = function()
LeftMousePress=false
RightMousePress=false
end
update = function()
if mouse.press and mouse.left then
LeftMousePress=true
end
if mouse.press and mouse.right then
RightMousePress=true
end
if mouse.release and not mouse.left then
LeftMousePress=false
end
if mouse.release and not mouse.right then
RightMousePress=false
end
if LeftMousePress and cub_y==0 then
cub_vy=7
LeftMousePress=false
end
if RightMousePress then
cub_color="rgb(0,255,0)"
RightMousePress=false
else cub_color="rgb(255,0,0)"
end
cub_vy-=0.3
cub_y=max(0,cub_y+cub_vy)
end
draw = function()
screen.clear("rgb(142,217,255)")
screen.fillRect(0,-80,400,40,"rgb(0,170,170)")
screen.fillRect(0,-50+cub_y,20,20,cub_color)
end
Paste this code into the editor and check. You will immediately realize that example 3
does the same thing as example 2
. Only the code has become twice as long!
Now the most successful example is the 2nd example. It does not comply with only one third condition, and is also much shorter and clearer.
Let's talk about him. The third condition is not met because mouse.press==1
only when any mouse button is pressed for the first time, then mouse.press==0
. But while any mouse button is clamped, when another button is pressed, mouse.press==0
is still used. Therefore, for example, one condition for the jump will not be met:
if mouse.press and mouse.left and cub_y==0 then //With the right mouse button held down, mouse.press=false
cub_vy=7
end
Thus, I failed to meet all four conditions. The question is still open to me. You can send your code examples where all 4 conditions will be met.
@this_name_is_taken thanks for the example with timers, I haven't tried to do this, it will definitely come in handy for me!