GetCursorMovedOn: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Created page with "{{Useful Function}} __NOTOC__ This function checks in which way the cursor is currently moving. ==Syntax== <syntaxhighlight lang="lua">string getCursorMoveOn( )</syntaxhighlig...")
 
No edit summary
Line 13: Line 13:
local cP = {
local cP = {
     x = 0,
     x = 0,
y = 0,
    y = 0,
move = nil,
    move = nil,
timer = false,
    timer = false,
}
}


function getCursorMoveOn()
function getCursorMoveOn()
return cP.move
    return cP.move
end
end


Line 26: Line 26:


if cursorX > cP.x then
if cursorX > cP.x then
        cP.move = "right"
            cP.move = "right"
elseif cursorX < cP.x then
elseif cursorX < cP.x then
    cP.move = "left"
    cP.move = "left"
Line 33: Line 33:
elseif cursorY < cP.y then
elseif cursorY < cP.y then
    cP.move = "down"
    cP.move = "down"
    end
        end
cP.x = cursorX
cP.x = cursorX
Line 45: Line 45:
cP.timer = setTimer(
cP.timer = setTimer(
    function ()
    function ()
    cP.move = "nil"
cP.move = "nil"
end, 50, 1
    end, 50, 1
)
)
end
end

Revision as of 17:08, 20 November 2017


This function checks in which way the cursor is currently moving.

Syntax

string getCursorMoveOn( )

Returns

Returns "left", "right", "up" or "down", "nil" if cursor didn't move.

Code

Click to collapse [-]
Function source
local cP = {
    x = 0,
    y = 0,
    move = nil,
    timer = false,
}

function getCursorMoveOn()
    return cP.move
end

function onClientCursorMove(cursorX, cursorY)
    if not isCursorShowing() then return end

	if cursorX > cP.x then
            cP.move = "right"
	elseif cursorX < cP.x then
	    cP.move = "left"
	elseif cursorY > cP.y then
	    cP.move = "up"
	elseif cursorY < cP.y then
	    cP.move = "down"
        end
	
	cP.x = cursorX
	cP.y = cursorY
	
	
	if isTimer(cP.timer) then
	    killTimer(cP.timer)
	end
	
	cP.timer = setTimer(
	    function ()
		cP.move = "nil"
	    end, 50, 1
	)
end
addEventHandler("onClientCursorMove",root,onClientCursorMove)