Pressing two buttons at once

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
User avatar
Brah
Prole
Posts: 13
Joined: Tue Feb 01, 2022 7:43 am
Location: The Internet

Pressing two buttons at once

Post by Brah »

I have two buttons in two different scenes in the same position. When I press button one in scene one button two in scene two is also pressed and I end up back in scene one. How do I make function love.mousepressed(x,y,button) only work once.

Code: Select all

scenes = {
    sceneone = true,
    scenetwo = false,
}

function love.load()
    require "sceneone"
    require "scenetwo"
end

function love.update(dt)

end

function love.draw()
    if scenes.sceneone == true then
        sceneone:draw()
    end
    if scenes.scenetwo == true then
        scenetwo:draw()
    end
end

function love.mousepressed(x, y, b)
    if scenes.sceneone == true then
        sceneone:mousepressed(x, y, b)
    end
    if scenes.scenetwo == true then
        scenetwo:mousepressed(x, y, b)
    end
end

Code: Select all

sceneone = {}
local button = {x = 10, y = 10, w = 100, h = 30}

function sceneone:update(dt)

end

function sceneone:draw()
    love.graphics.setColor(1,0,0)
    love.graphics.print("ONE", 45, 17)
    love.graphics.rectangle("line", button.x, button.y, button.w, button.h)
end


function sceneone:mousepressed(x, y, b)
    if b == 1 and scenes.sceneone == true then
        if (x > button.x and x < button.x + button.w)
        and (y > button.y and y < button.y + button.h)
        then
            print("this is scene two")
            scenes.sceneone = false
            scenes.scenetwo = true
        end
    end
end

Code: Select all

scenetwo = {}
local button = {x = 10, y = 10, w = 100, h = 30}

function scenetwo:update(dt)

end

function scenetwo:draw()
    love.graphics.setColor(0,0,1)
    love.graphics.print("TWO", 45, 17)
    love.graphics.rectangle("line", button.x, button.y, button.w, button.h)
end


function scenetwo:mousepressed(x, y, b)
    if b == 1 and scenes.scenetwo == true then
        if (x > button.x and x < button.x + button.w)
        and (y > button.y and y < button.y + button.h)
        then
            print("this is scene one")
            scenes.scenetwo = false
            scenes.sceneone = true
        end
    end
end
User avatar
darkfrei
Party member
Posts: 1169
Joined: Sat Feb 08, 2020 11:09 pm

Re: Pressing two buttons at once

Post by darkfrei »

You are need states: (not tested)

Code: Select all

 -- main.lua
Scenes = {
	sceneOne = require "sceneone",
	sceneTwo = require "scenetwo",
}
Scene = Scenes.sceneOne

function love.load()
	
end

function love.update(dt)
	Scene:update (dt)
	
end

function love.draw()
	Scene:draw()
	
end

function love.mousepressed(x, y, b)
	Scene:mousepressed(x, y, b)

end
Comment and uncomment commented lines for scenetwo.lua

Code: Select all

 -- sceneone.lua
local scene = {}
scene.button = {x = 10, y = 10, w = 100, h = 30}

function scene:update(dt)
	
end

function scene:draw()
	love.graphics.setColor(1,0,0)
	love.graphics.print("ONE", 45, 17)
--	love.graphics.print("TWO", 45, 17)
	local button = self.button
	love.graphics.rectangle("line", button.x, button.y, button.w, button.h)
end


function scene:mousepressed(x, y, b)
	if b == 1 then
		local button = self.button
		if (x > button.x and x < button.x + button.w)
		and (y > button.y and y < button.y + button.h)
		then
			Scene = Scenes.sceneTwo -- switch to other scene
--			Scene = Scenes.sceneOne

		end
	end
end

return scene -- most important thing

:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: Pressing two buttons at once

Post by ReFreezed »

You can return a value from the :mousepressed() functions to signal whether the mouse event was handled or not.

Code: Select all

function scenetwo:mousepressed(x, y, b)
    if b == 1 and scenes.scenetwo == true then
        if (x > button.x and x < button.x + button.w)
        and (y > button.y and y < button.y + button.h)
        then
            print("this is scene one")
            scenes.scenetwo = false
            scenes.sceneone = true
            return true -- Handled!
        end
    end
    return false -- Not handled!
end
And return from love.mousepressed() when the event was handled.

Code: Select all

function love.mousepressed(x, y, b)
    if scenes.sceneone == true then
        if sceneone:mousepressed(x, y, b) then  return  end
    end
    if scenes.scenetwo == true then
        if scenetwo:mousepressed(x, y, b) then  return  end
    end
end
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
User avatar
Brah
Prole
Posts: 13
Joined: Tue Feb 01, 2022 7:43 am
Location: The Internet

Re: Pressing two buttons at once

Post by Brah »

Thanks!
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 58 guests