EXAMPLE: If i press the M6 button, my cursor go to a X place, if i press the M6 button again he goes to a Y place, how can i does this alternation ?
local TOPX, TOPY, MIDX, MIDY
TOPX = 59305 -- Top side X
TOPY = 54527 -- Top side Y
MIDX = 61764 -- Mid lane X
MIDY = 58683 -- Mid lane Y
function OnEvent(event, arg)
for n = 1,2
do
if event == "MOUSE_BUTTON_RELEASED" and arg == 6 then
MoveMouseTo(MIDX, MIDY);
for n = 2,4
do
if
event == "MOUSE_BUTTON_RELEASED" and arg == 6 then
MoveMouseTo(TOPX, TOPY);
end
end
end
end
end
One way to do it would be having an outter flag boolean variable which would allow you to identify the state of the M6 button strokes.
local buttonPressedOnce = true
function onEvent(event, arg)
-- Check if the button is the one we desire.
if (event == "MOUSE_BUTTON_RELEASED" and arg == 6) then
if (buttonPressedOnce) then
-- execute X update code
else
-- execute Y update code
end
buttonPressedOnce = not(buttonPressedOnce)
end
end
Explanation: The first press will always be the X axis update code, so we initialize the flag as true, and with each click call we proceed to update the flag value, making sure each button stroke will switch to the desired state.
Related
I have a logitech g102 mouse and i want to create a lua script which will press a different key every time i press the mouse button 4. Specifically i want each time i click the mouse button 4 to loop around 3 keyboard strokes (5,6,7). So if i click the first time my mouse button 4 it will press the number 3, the second time the number 4, third time number 5 and then repeat this as many times i press mouse button 4. I have already tried some code but didnt get anywhere. Can somebody help me?
local keys = {"h", "e", "l", "l", "o"} -- cycle of keys
local idx
local tm = -math.huge
function OnEvent(event, arg)
if event == "MOUSE_BUTTON_PRESSED" and arg == 4 then
if GetRunningTime() - tm > 2000 then
idx = 0
end
idx = idx % #keys + 1
PressKey(keys[idx])
elseif event == "MOUSE_BUTTON_RELEASED" and arg == 4 then
ReleaseKey(keys[idx])
tm = GetRunningTime()
end
end
This is my first attempt at LUA so bear with me!
I'm trying to combine a couple of functions that I want to loop at the same time, but I can only get them to run sequentially.
Ideally, the function bound to mouse button 6 would start on press and stop on release (currently it doesn't stop at all!!). Separately the function on mouse button 3 would run when it is pressed and stop on release (this one does stop).
At the moment the button 3 loop works as it should, but the button 6 loop doesn't end.
Ideally, I would like both to work and stop on their respective button press and release, AND for them to BOTH run at the same time when both 3 & 6 are pressed at together.
Can this be done and if so what do I need to change to achieve this?
EnablePrimaryMouseButtonEvents(true);
function OnEvent(event, arg)
if event == "MOUSE_BUTTON_PRESSED" and arg == 6 then
repeat
MoveMouseRelative(300,0)
Sleep(150)
MoveMouseRelative(-300,0)
Sleep(1500)
until event == "MOUSE_BUTTON_RELEASED" and arg == 6
elseif IsMouseButtonPressed(3) then
repeat
MoveMouseRelative(0,300)
Sleep(1000)
until not IsMouseButtonPressed(3)
end
end
Thanks in advance :D
Step 1.
Make sure you're not using MB#4 ("back") in the game.
If you don't use MB#4 in the game, just skip "Step 1".
If some action is assigned to MB#4 in the game, do the following:
choose keyboard button you don't currently use in the game (for example, F12)
goto GHUB (KEYS tab); bind F12 to your physical MB#4
goto game options; bind the action to F12 instead of MB#4
Now when you press physical MB#4, the game sees F12 and activates your old action.
Step 2.
Goto GHUB (SYSTEM tab)
Unbind "Back" from MB#4
Bind "Back" to MB#6
Step 3.
The script.
local all_buttons = {
[4] = { -- mouse button #6
{time=0, x=50 }, -- at time 0 move mouse right 50 pixels
{time=150, x=-50}, -- at time 150 move mouse left 50 pixels
{time=150+1500}, -- at time 150+1500 repeat the loop
},
[3] = { -- right mouse button
{time=0, y=50}, -- at time 0 move mouse down 50 pixels
{time=1000}, -- at time 1000 repeat the loop
}
}
function OnEvent(event, arg)
if event == "PROFILE_ACTIVATED" then
EnablePrimaryMouseButtonEvents(true)
elseif event == "MOUSE_BUTTON_PRESSED" then
repeat
Sleep(10)
local tm = GetRunningTime()
local some_loop_is_active
for btn_no, info in pairs(all_buttons) do
if not info.started and IsMouseButtonPressed(btn_no) then
info.started = tm
info.next_step = 1
end
some_loop_is_active = some_loop_is_active or info.started
local next_action = info[info.next_step]
if info.started and tm - info.started >= next_action.time then
info.next_step = info.next_step + 1
local x, y = next_action.x, next_action.y
if x or y then
x, y = x or 0, y or 0
local sx, sy = x < 0 and -1 or 1, y < 0 and -1 or 1
x, y = x*sx, y*sy
while x > 0 or y > 0 do
local px, py = x < 127 and x or 127, y < 127 and y or 127
MoveMouseRelative(px*sx, py*sy)
x, y = x-px, y-py
end
else
info.started = nil
end
end
end
until not some_loop_is_active
end
end
You should know that MoveMouseRelative() is limited by 127 pixels.
To move mouse cursor 300 pixels you should invoke it 3 times:
MoveMouseRelative(0,100);MoveMouseRelative(0,100);MoveMouseRelative(0,100)
I'm trying to have the below code execute the MoveMouseRelative function only once when mouse button 1 is pressed or held. I've tried removing the "repeat" line but it breaks the code. Currently when activated and mouse 1 is held the cursor is dragged down constantly.
function OnEvent(event, arg)
OutputLogMessage("event = %s, arg = %d\n", event, arg)
if (event == "PROFILE_ACTIVATED") then
EnablePrimaryMouseButtonEvents(true)
elseif event == "PROFILE_DEACTIVATED" then
ReleaseMouseButton(2) -- to prevent it from being stuck on
end
if (event == "MOUSE_BUTTON_PRESSED" and arg == 5) then
recoilx2 = not recoilx2
spot = not spot
end
if (event == "MOUSE_BUTTON_PRESSED" and arg == 1 and recoilx2) then
if recoilx2 then
repeat
--Sleep(35)
Sleep(5)
MoveMouseRelative(0, 3)
until not IsMouseButtonPressed(1)
end
end
Goal: Lua script for Logitech mouse that performs the following tasks:
When mouse button 5 has been "activated":
- Left clicks once every 1000 milliseconds (time in between shots),
- and also pulls the mouse down once every 1000 milliseconds.
So if I hold left mouse button it continuously shoots but only pulls down when it does shoot
Select a keyboard button you never use in the game and set it as the only way to fire the pistol, this key will be used to fire programmatically.
I assume the key is P, but you can choose any other button: "f12", "backspace", "num9", ...
The game must do nothing on left mouse button press.
local fire_button = "P"
function OnEvent(event, arg)
OutputLogMessage("event = %s, arg = %d\n", event, arg)
if event == "PROFILE_ACTIVATED" then
EnablePrimaryMouseButtonEvents(true)
elseif event == "PROFILE_DEACTIVATED" then
-- to prevent mouse buttons from being stuck on
for j = 1, 5 do ReleaseMouseButton(j) end
end
if event == "MOUSE_BUTTON_PRESSED" and arg == 5 then
recoilx2 = not recoilx2
end
if event == "MOUSE_BUTTON_PRESSED" and arg == 1 then
PressKey(fire_button)
Sleep(50)
ReleaseKey(fire_button)
if recoilx2 then
while IsMouseButtonPressed(1) do
MoveMouseRelative(0, 25)
local next_shot_time = GetRunningTime() + 1000
local LMB
repeat
Sleep(50)
LMB = IsMouseButtonPressed(1)
until not LMB or GetRunningTime() >= next_shot_time
if LMB then
PressKey(fire_button)
Sleep(50)
ReleaseKey(fire_button)
end
end
end
end
end
I am trying to complete this Lua tic-tac-toe tutorial in Corona SDK.
I managed to get through the 1st part but got lost during this 2nd part where he is establishing variables within a table to register the alternating turns for "x" and "o".
He is using the tap count to determine which turn it is and I did try to use Corona's touch.id to mimic this technique, to no avail.
I am hoping someone can explain how I can achieve this using Corona.
Here is what I have so far (from Part1 of Tutorial):
d = display
w20 = d.contentWidth * .2
h20 = d.contentHeight * .2
w40 = d.contentWidth * .4
h40 = d.contentHeight * .4
w60 = d.contentWidth * .6
h60 = d.contentHeight * .6
w80 = d.contentWidth * .8
h80 = d.contentHeight * .8
----DRAW LINES FOR BOARD
local lline = d.newLine(w40,h20,w40,h80 )
lline.strokeWidth = 5
local rline = d.newLine(w60,h20,w60,h80 )
rline.strokeWidth = 5
local bline = d.newLine(w20,h40,w80,h40 )
bline.strokeWidth = 5
local tline = d.newLine(w20,h60,w80,h60 )
tline.strokeWidth = 5
--PLACE BOARD COMPARTMENT DIMENSIONS IN TABLE
board ={
{"tl", 1, w20, h40, w40, h20,0},
{"tm",2, w40,h40,w60,h20,0},
{"tr",3, w60,h40,w80,h20,0},
{"ml", 4, w20, h60, w40, h40,0},
{"mm",5, w40,h60,w60,h40,0},
{"mr",6, w60,h60,w80,h40,0},
{"bl", 7, w20, h80, w40, h60,0},
{"bm",8, w40,h80,w60,h60,0},
{"br",9, w60,h80,w80,h60,0}
}
--
--FILL COMPARTMENT W/ COLOR WHEN TOUCHED
local function fill (event)
if event.phase == "began" then
tap = 0
for t = 1, 9 do
if event.x > board[t][3] and event.x < board [t][5] then
if event.y < board[t][4] and event.y > board[t][6] then
r = d.newRect(board[t][3],board [t][6],w20,h20)
r:setFillColor(1,1,0)
r.anchorX=0
r.anchorY=0
end
end
end
end
end
Runtime:addEventListener("touch", fill)
I use new variable whichTurn to figure out what to put on board in each turn. I think code is self-explanatory. Try
...
local EMPTY, X, O = 0, 1, 2
local whichTurn = X -- X is starting game
...
--FILL COMPARTMENT W/ COLOR WHEN TOUCHED
local function fill (event)
if event.phase == "began" then
for t = 1, 9 do
if event.x > board[t][3] and event.x < board [t][5] then
if event.y < board[t][4] and event.y > board[t][6] then
if board[t][7] == EMPTY then
board[t][7] = whichTurn
--[[
The operator AND returns its first argument if it is false;
otherwise, it returns its second argument. The operator OR
returns its first argument if it is not false; otherwise, it
returns its second argument.
--]]
whichTurn = whichTurn == X and O or X
end
end
end
end
end
end
Runtime:addEventListener("touch", fill)
You can read more about differences between tap and touch events in Corona.
Check also this from Corona documentation
Filtering Multiple Taps
Using the event.numTaps property, you can easily determine whether an
object was tapped multiple times and concurrently ignore single taps
on the object.
I have asked a question similar to this, but i was asking it for Processing.JS. Now i am making something out of LOVE2D Lua, what I'm trying to do is that i have a button where when clicked it adds a circle on-screen. I already have code that when i click and hold on the circle, i can move it around. But when i add a second circle, they both are using he same variables. I want it so i write once instance of the code to move and add the circle, but i can call it multiple times and each one be unique without writing code to anticipate an infinite amount of circles. Here is my code:
obn = 0
ellipsex = 50
ellipsey = 50
ellipsew = 50
ellipseh = 50
ellipser = 255
ellipseg = 0
ellipseb = 0
function love.draw()
mousex, mousey = love.mouse.getPosition()
for i=0,obn,1 do
ellipse()
end
end
function love.mousereleased(x, y, button)
if button == 2 then
obn = obn + 1
end
end
function love.update(dt)
if love.mouse.isDown(1) then
if mousex > ellipsex and mousex < ellipsex + ellipsew and mousey > ellipsey and mousey < ellipsey + ellipseh then
ellipsex = mousex
ellipsey = mousey
end
end
end
function ellipse()
love.graphics.setColor(ellipser, ellipseg, ellipseb)
love.graphics.ellipse("fill", ellipsex, ellipsey, ellipsew, ellipseh)
end
But when i right-click to add a circle(increment how many times the for-loop runs) it doesnt add a second circle for me to move independent from the first one. Help?
The way I would go about handling multiple instances of ellipses is creating a table for every ellipse that would hold it's properties and a draw function.
local ellipses = {} -- store all ellipses into a table
function create_ellipse(x,y,w,h,r,g,b)
local ellipse = {
x = x,
y = y,
w = w,
h = h,
r = r,
g = g,
b = b
}
function ellipse.draw()
love.graphics.setColor(ellipse.r,ellipse.g,ellipse.b)
love.graphics.ellipse("fill",ellipse.x,ellipse.y,ellipse.w,ellipse.h)
end
ellipses[#ellipses+1] = ellipse -- insert new ellipse into ellipses table
return ellipse
end
function love.draw()
for i = 1,#ellipses do
ellipses[i].draw(); -- call each ellipse's separate draw function
end
end
function love.mousereleased(x,y,button)
if button == 2 then
create_ellipse(x,y,50,50,255,0,0) -- bonus: every ellipse is created where the user clicked
end
end
function love.update(dt)
if love.mouse.isDown(1) then
local mousex,mousey = love.mouse.getPosition() -- there is no need to request the mouse position every frame, but only when a user clicks anywhere on the screen
for i = 1,#ellipses do
local current_ellipse = ellipses[i]
if mousex >= current_ellipse.x and mousex <= current_ellipse.x+current_ellipse.w and mousey >= current_ellipse.y and mousey <= current_ellipse.y+current_ellipse.h then
current_ellipse.x = mousex
current_ellipse.y = mousey
end
end
end
end
You could even make your own Ellipse class if you're into OOP.