Re: waitkey()
Posted: Sun Mar 15, 2009 11:17 am
How's this? Does same thing as my previous example, but this time uses coroutines. No visible difference, so I wont bother making .love and just post the code here.
P.S I still don't fully understand how coroutines works; Just copied the magic off your example. :/ I'm not even sure if coroutines are doing the work... I dont think they are :s
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