Page 1 of 1

Would this work? [EASY]

Posted: Thu Oct 02, 2014 4:01 am
by kotwarrior
Hello! So, I'm new to LOVE2D (just started today), but I have around 2 years in Lua experience. I just wanted to confirm with the love library, this is how I'd create an image and a label saying the message in the region. Also, this would be how I can make the image and printing disappear when the screen is clicked, correct?

Code: Select all

local env = getfenv();
env["$draw"] = true;

env["@updatestatus"] = function(val)
	env["$draw"] = val;
end;

function love.load()
	env["$logo"] = love.graphics.newImage("military12.png");
end;

function love.draw()
	if(env["$draw"] == true) then
		love.graphics.print("Welcome to a game created by KOTwarrior", 400, 300);
		love.graphics.draw(env["$logo"], 300, 200);
	end;
end;

function love.mousepressed(x, y, button)
	if(button == "l") then
		env["@updatestatus"](false);
	end;
end;


Re: Would this work? [EASY]

Posted: Thu Oct 02, 2014 4:45 am
by shakesoda
I am terrified.

Re: Would this work? [EASY]

Posted: Thu Oct 02, 2014 4:51 am
by slime
That general idea should work, yes, although the code you posted is really far from idiomatic Lua – the way you're creating and using global variables is really verbose.

Here's the same thing, rewritten to be a lot cleaner and more maintainable:

Code: Select all

local draw = true

function updatestatus(val)
    draw = val
end

function love.load()
    logo = love.graphics.newImage("military12.png")
end

function love.draw()
    if draw then
        love.graphics.print("Welcome to a game created by KOTwarrior", 400, 100)
        love.graphics.draw(logo, 300, 200)
    end
end

function love.mousepressed(x, y, button)
    if button == "l" then
        updatestatus(false)
    end
end

function love.mousereleased(x, y, button)
    if button == "l" then
        updatestatus(true)
    end
end
Or using [wiki]love.mouse.isDown[/wiki] instead of love.mousepressed and love.mousereleased:

Code: Select all

local draw = true

function love.load()
    logo = love.graphics.newImage("military12.png")
end

function love.update(dt)
    local mousedown = love.mouse.isDown("l")
    draw = not mousedown
end

function love.draw()
    if draw then
        love.graphics.print("Welcome to a game created by KOTwarrior", 400, 100)
        love.graphics.draw(logo, 300, 200)
    end
end