set mouseklick start

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
nadula
Prole
Posts: 10
Joined: Wed Sep 25, 2013 8:23 am

set mouseklick start

Post by nadula »

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
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: set mouseklick start

Post by Ref »

Look at the WIKI example for mousepressed.
Assign some default values to x & y.
Check in draw if the values have changed.
nadula
Prole
Posts: 10
Joined: Wed Sep 25, 2013 8:23 am

Re: set mouseklick start

Post by nadula »

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
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: set mouseklick start

Post by Roland_Yonaba »

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
How is that working for you ?
nadula
Prole
Posts: 10
Joined: Wed Sep 25, 2013 8:23 am

Re: set mouseklick start

Post by nadula »

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
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: set mouseklick start

Post by Roland_Yonaba »

nadula wrote:Thank you,Roland, I tried a variable named status true or false but it doesn't work
Can you just give us a *.love file ?
nadula
Prole
Posts: 10
Joined: Wed Sep 25, 2013 8:23 am

Re: set mouseklick start

Post by nadula »

Sorry, now the lua file
Attachments
conf.lua
(124 Bytes) Downloaded 120 times
main.lua
(1.65 KiB) Downloaded 150 times
nadula
Prole
Posts: 10
Joined: Wed Sep 25, 2013 8:23 am

Re: set mouseklick start

Post by nadula »

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

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 3 guests