Page 1 of 1

Pressing two buttons at once

Posted: Wed Sep 28, 2022 2:38 am
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

Re: Pressing two buttons at once

Posted: Wed Sep 28, 2022 9:06 am
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


Re: Pressing two buttons at once

Posted: Wed Sep 28, 2022 3:22 pm
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

Re: Pressing two buttons at once

Posted: Sat Oct 01, 2022 9:14 am
by Brah
Thanks!