Would this work? [EASY]

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
kotwarrior
Prole
Posts: 2
Joined: Thu Oct 02, 2014 3:17 am

Would this work? [EASY]

Post 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;

User avatar
shakesoda
Citizen
Posts: 78
Joined: Thu Sep 25, 2014 11:57 am
Location: Seattle, WA
Contact:

Re: Would this work? [EASY]

Post by shakesoda »

I am terrified.
excessive ❤ moé (LÖVE3D, CPML, ...). holo on IRC.
User avatar
slime
Solid Snayke
Posts: 3159
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Would this work? [EASY]

Post 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
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests