Difference between revisions of "love.keyboard.setKeyRepeat"

m (remove .lua from require)
(Replaced example (much simpler, doesn't involve AnAL))
Line 11: Line 11:
 
Nothing.
 
Nothing.
 
== Examples ==
 
== Examples ==
=== Press key to continue moving left or right ===
+
=== Hold key to continue moving left or right ===
 +
Please note that a generally better way to move an object would be to put code in [[love.update]]() which uses [[love.keyboard.isDown]]. This is just an example.
 
<source lang="lua">
 
<source lang="lua">
 
function love.load()
 
function love.load()
require("AnAL")
+
x = 400
  -- Load the animation source.
+
love.keyboard.setKeyRepeat(10, 200)
imgl = love.graphics.newImage("walkl.png")
 
imgr = love.graphics.newImage("walkr.png")
 
imgsl = love.graphics.newImage("stopl.png")
 
imgsr = love.graphics.newImage("stopr.png")
 
  -- Create animation.
 
 
 
  anim = newAnimation(imgsl, 32, 48, 0.1, 0)
 
  animX = 100
 
  animY = 100
 
  --  Interval value = Animation's Delay time * The number of Animation's frame
 
  -- 100 ms * 2 frame = 200 ms (Interval)
 
  love.keyboard.setKeyRepeat(10, 200)
 
 
end
 
end
  
function love.update(dt)
+
function love.keypressed(key)
  -- Updates the animation. (Enables frame changes)
+
if key == "left" then x = x - 20
  anim:update(dt)
+
elseif key == "right" then x = x + 20
 +
end
 
end
 
end
  
 
function love.draw()
 
function love.draw()
  -- Draw the animation at (100, 100).
+
love.graphics.circle("fill", x,300, 30,30)
  anim:draw(animX , animY)
 
 
end
 
end
 +
</source>
  
function love.keypressed(key,unicode)
 
if key == "left" then
 
anim = newAnimation(imgl,32,48,0.1,0)
 
anim:setMode ("once")
 
animX = animX - 10
 
elseif key == "right" then
 
anim = newAnimation(imgr, 32, 48, 0.1, 0)
 
animX = animX + 10
 
anim:setMode ("once")
 
end
 
end
 
</source>
 
 
== See Also ==
 
== See Also ==
 
* [[parent::love.keyboard]]
 
* [[parent::love.keyboard]]

Revision as of 06:18, 2 October 2011

Enables key repeating and sets the delay and interval.

Function

Synopsis

love.keyboard.setKeyRepeat( delay, Interval )

Arguments

number delay
The amount of time before repeating the key (in milliseconds). 0 disables key repeat.
number Interval
The amount of time between repeats (in milliseconds)

Returns

Nothing.

Examples

Hold key to continue moving left or right

Please note that a generally better way to move an object would be to put code in love.update() which uses love.keyboard.isDown. This is just an example.

function love.load()
	x = 400
	love.keyboard.setKeyRepeat(10, 200)
end

function love.keypressed(key)
	if key == "left" then x = x - 20
	elseif key == "right" then x = x + 20
	end
end

function love.draw()
	love.graphics.circle("fill", x,300, 30,30)
end

See Also


Other Languages