Difference between revisions of "love.wheelmoved"

m (Arguments)
m (Add example for smooth scrolling)
Line 26: Line 26:
 
function love.draw()
 
function love.draw()
 
     love.graphics.print(text, 10, 10)
 
     love.graphics.print(text, 10, 10)
 +
end
 +
</source>
 +
 +
 +
=== Smooth scrolling ===
 +
<source lang="lua">
 +
local posx, posy = love.graphics.getWidth() * 0.5, love.graphics.getHeight() * 0.5
 +
local velx, vely = 0, 0 -- The scroll velocity
 +
 +
function love.draw()
 +
    love.graphics.rectangle( 'line', posx, posy, 50, 50 );
 +
end
 +
 +
function love.update( dt )
 +
    posx = posx + velx
 +
    posy = posy + vely
 +
 +
    -- Gradually reduce the velocity to create smooth scrolling effect.
 +
    velx = velx * 0.95
 +
    vely = vely * 0.95
 +
end
 +
 +
function love.wheelmoved( dx, dy )
 +
    velx = velx + dx
 +
    vely = vely + dy
 
end
 
end
 
</source>
 
</source>

Revision as of 12:42, 27 December 2015

Available since LÖVE 0.10.0
This function is not supported in earlier versions.

Callback function triggered when the mouse wheel is moved.

Function

Synopsis

love.wheelmoved( x, y )

Arguments

number x
Amount of horizontal mouse wheel movement. Positive values indicate movement to the right.
number y
Amount of vertical mouse wheel movement. Positive values indicate upward movement.

Returns

Nothing.

Examples

local text = ""

function love.wheelmoved(x, y)
    if y > 0 then
        text = "Mouse wheel moved up"
    elseif y < 0 then
        text = "Mouse wheel moved down"
    end
end

function love.draw()
    love.graphics.print(text, 10, 10)
end


Smooth scrolling

local posx, posy = love.graphics.getWidth() * 0.5, love.graphics.getHeight() * 0.5
local velx, vely = 0, 0 -- The scroll velocity

function love.draw()
    love.graphics.rectangle( 'line', posx, posy, 50, 50 );
end

function love.update( dt )
    posx = posx + velx
    posy = posy + vely

    -- Gradually reduce the velocity to create smooth scrolling effect.
    velx = velx * 0.95
    vely = vely * 0.95
end

function love.wheelmoved( dx, dy )
    velx = velx + dx
    vely = vely + dy
end

See Also


Other Languages