Hi,
I have a labyrinth and want to click with the mouse to set the start point. I call love.mousepressed(x, y, button) and have defined start, but love draw is executed at first and if I say start point in love.draw , it has a nil value. So the start image should appear when the mouse is clicked.Can someone give me an advice. Thank you
set mouseklick start
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: set mouseklick start
Look at the WIKI example for mousepressed.
Assign some default values to x & y.
Check in draw if the values have changed.
Assign some default values to x & y.
Check in draw if the values have changed.
Re: set mouseklick start
I tried so . i took x ,y =0 . the start image was drawn at x,y =0 and as I klicked with the mouse it disappered
- Roland_Yonaba
- Inner party member
- Posts: 1563
- Joined: Tue Jun 21, 2011 6:08 pm
- Location: Ouagadougou (Burkina Faso)
- Contact:
Re: set mouseklick start
Code: Select all
local startPoint -- undefined
function love.draw()
-- draw the startPoint if defined
if startPoint then
love.graphics.draw(startPoint.x, startPoint.y, startPoint.image)
end
end
function love.mousepressed(x,y,button)
-- Define the startPoint.
-- You might want to it once, then wrap the following into an appropriate if-clause
startPoint = {x = x, y = y, image = someImage}
end
Re: set mouseklick start
Thank you,Roland, I tried a variable named status true or false but it doesn't work
Code: Select all
function love.load()
map={}
k=1
v=1
for line in love.filesystem.lines("labyrinth1.txt") do
map[k] = {}
for i in string.gmatch(line,"%S+") do
map[k][v] = tonumber(i)
v= v+1
end
k= k+1
v=1
end
target = {
grid_x = 13,
grid_y = 10
}
status = false
img_fn = {"robot"}
imgs = {}
for _,v in ipairs(img_fn) do
imgs[v]=love.graphics.newImage("assets/"..v..".png")
end
-- load sounds
ups = love.audio.newSource( "assets/ups.ogg" , "static" )
yeah = love.audio.newSource( "assets/yeah.wav" , "static" )
end
function love.draw()
for y=1, #map do
for x=1, #map[y] do
if map[y][x] == 1 then
love.graphics.setColor(50,50,50)
love.graphics.rectangle("fill", x * scale, y * scale, scale, scale)
else
love.graphics.setColor(255,255,255)
love.graphics.rectangle("fill", x * scale+1, y * scale+1, scale-2, scale-2)
end
end
end
love.graphics.setColor(0,200,0)
love.graphics.setPointStyle("smooth")
love.graphics.setPointSize(scale-2)
love.graphics.point(target.grid_x*scale + scale /2, target.grid_y*scale + scale / 2)
if status then
love.graphics.setColor(200,0,0)
love.graphics.setPointStyle("smooth")
love.graphics.setPointSize(scale-2)
love.graphics.point(start.grid_x*scale + scale /2, start.grid_y*scale + scale / 2)
love.graphics.setColor(255,255,255)
love.graphics.draw(imgs["robot"], start.grid_x*scale + scale /2, start.grid_y*scale + 18)
end
end
function love.mousepressed(x, y, button)
if button == 'l' then
start = {grid_x = x, grid_y = y}
status = true
end
end
- Roland_Yonaba
- Inner party member
- Posts: 1563
- Joined: Tue Jun 21, 2011 6:08 pm
- Location: Ouagadougou (Burkina Faso)
- Contact:
Re: set mouseklick start
Can you just give us a *.love file ?nadula wrote:Thank you,Roland, I tried a variable named status true or false but it doesn't work
Re: set mouseklick start
Hi, I still need your help. Please, I could not implement the suggestion. Can someone tell me what's wrong in my code? Thank you
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 3 guests