Code: Select all
--MAIN.LUA
love.filesystem.require("waitkey.lua")
local rX=0;
local rY=100;
function load()
end
function update()
rX=rX+1;
if rX==100 then
waitKey(waitkeypressed)
end
end
function draw()
love.graphics.rectangle(1, rX, rY, 100, 100);
end
function keypressed()
error("hey")
end
function waitkeypressed(key)
rY=rY+key
end
Code: Select all
-- WAITKEY.LUA
function waitKey(waitFunction)
local oldkp=keypressed;
local oldupdate=update;
local _coroutine=coroutine.create(function()
coroutine.yield(true);
end)
local currentCoroutine=_coroutine;
function update()
if currentCoroutine~=nil then
local result, waiting = coroutine.resume(currentCoroutine);
currentCoroutine=nil;
end
if waiting then
currentCoroutine=_coroutine;
end
end
function keypressed(key)
currentCoroutine=nil;
if oldkp~=nil then
keypressed=oldkp;
end
if oldupdate~=nil then
update=oldupdate;
end
waitFunction(key);
end
end