Page 1 of 1

make line dissapear after 'x' seconds

Posted: Mon Oct 05, 2020 1:46 pm
by PVNFU
Hi, Im learning Lua with Love as my first programing language, and so far so good but I am stuck into something, I made it so when I press left click the program draws a line between 2 moving objects, and I ran into 2 problems, the line keeps changing shape and moves with the objects, and I need a way to make the line be drawn one frame and be 'frozen' until I tell it to dissapear after an x number of seconds (let's say 1).
please and thanks

Re: make line dissapear after 'x' seconds

Posted: Mon Oct 05, 2020 9:26 pm
by Xugro
To make the line disappear after x seconds. Save the remaining time in a variable and reduce it in love.update until it is zero (or less). If this happens delete the line:

Code: Select all

function love.load()
  remaining_time = x
end

function love.update(dt)
  remaining_time = remaining_time - dt
  
  if remaining_time <= 0 do
    -- delete line
  end
end
To "freeze" the line into position: Save the coordinates of the start- and endpoint in extra variables/tables. Do not make a reference:

Code: Select all

object1 =  {}
object1.position = {}
object1.position.x = 100
object1.position.x = 200

line_start = object1.position -- this is a reference (a pointer to the position table of object1)
What you need is a (deep-)copy:

Code: Select all

object1 =  {}
object1.position = {}
object1.position.x = 100
object1.position.y = 200

line_start = {}
line_start.x = object1.position.x
line_start.y = object1.position.y