How to scale like this and keep my love.mousepressed to work.HELP

Show off your games, demos and other (playable) creations.
Post Reply
Donut-Dezz
Prole
Posts: 25
Joined: Sat Aug 03, 2019 11:56 am

How to scale like this and keep my love.mousepressed to work.HELP

Post by Donut-Dezz » Thu Feb 06, 2020 8:46 pm

How to scale like this and keep my love.mousepressed to work. when i click on the start(is an image button) button it dont work.but when i remove the love.graphics.scale() it works. but i want to keep the love.graphics.scale(). it works great on mobile


state = "menu"


start_button = love.graphics.newImage("start.png")
dx = 228
dy = 398

image_height = start_button:getHeight()
image_width = start_button:getWidth()


scalex = 795
scaley = 720

window_width = love.graphics.getWidth()
window_height = love.graphics.getHeight()

function love.load()



end



function love.draw()

love.graphics.scale(window_width /scalex,window_height / scaley)
if state =="play" then
love.graphics.print("The Game Has Started",200,200)
end
if state =="menu" then
love.graphics.draw(start_button,dx,dy)
end

end



function love.mousepressed(x,y,button)

if x > dx and x < dx + image_width and y > dy and y < dy + image_height then
state = "play"
end



end

User avatar
sleepy
Prole
Posts: 5
Joined: Tue May 28, 2019 9:45 am

Re: How to scale like this and keep my love.mousepressed to work.HELP

Post by sleepy » Fri Feb 07, 2020 12:03 pm

When doing a scaling operation your coordinates and dimensions of the input image scale as well. In your case the point in rectangle calculation is wrong, because you scaled dx, dy and the image dimensions by using love.graphics.scale.

In order to obtain the correct values you would need to scale dx, dy and the dimensions, too.

Code: Select all

local dsx = dx * (window_width /scalex)
Like that for all the other values.

Donut-Dezz
Prole
Posts: 25
Joined: Sat Aug 03, 2019 11:56 am

Re: How to scale like this and keep my love.mousepressed to work.HELP

Post by Donut-Dezz » Fri Feb 07, 2020 2:08 pm

can you show me an example bro?

User avatar
sleepy
Prole
Posts: 5
Joined: Tue May 28, 2019 9:45 am

Re: How to scale like this and keep my love.mousepressed to work.HELP

Post by sleepy » Fri Feb 07, 2020 2:30 pm

Code: Select all

function love.mousepressed(x,y,button)
	--getting the scale factor
	local sx = window_width /scalex
	local sy = window_height / scaley
	
	--corrected translation according to scale
	local dsx = dx * sx
	local dsy = dy * sy
	--new image boundaries
	local iw = image_width * sx
	local ih = image_height * sy
	
	if x > dsx and x < dsx + iw and y > dsy and y < dsy + ih then
		state = "play"
	end 
end 
Depends on what your goal is you might need to structure your code accordingly.

Donut-Dezz
Prole
Posts: 25
Joined: Sat Aug 03, 2019 11:56 am

Re: How to scale like this and keep my love.mousepressed to work.HELP

Post by Donut-Dezz » Fri Feb 14, 2020 1:22 pm

THANKS SLEEPY. You answer my question. im just now seeing this but thanks bro this worked.

Post Reply

Who is online

Users browsing this forum: No registered users and 13 guests