make line dissapear after 'x' seconds

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
PVNFU
Prole
Posts: 1
Joined: Mon Oct 05, 2020 1:21 pm

make line dissapear after 'x' seconds

Post 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
Xugro
Party member
Posts: 110
Joined: Wed Sep 29, 2010 8:14 pm

Re: make line dissapear after 'x' seconds

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

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot], Semrush [Bot] and 80 guests