Discord
Login
Community
DARK THEME

how do you delete one object without clearing the whole screen?

?

Sorry, this is pretty long, but I recommend reading all of it :)

td;lr just clear the screen, and draw every object except the one you want to remove. Shown below is a simple way to do that. Another way I'm a fan of is using classes and calling their draw function in draw, or just adding a simple if statement to an element you want to toggle. I really don't know what situation this is in, so here's my best solution based on what I think you need. If this is inconvenient or just doesn't suit your need, just tell me :D

If you are going with a list-based approach, just have a list of objects with sprite, width, height, x, and y data (i,e

init = function()
  objects = [
    ["sprite1", -0, -10, 30, 30]
    ["sprite2", -40, 5, 10, 30]
    ["sprite3", 3, 100, 60, 134]
    ["sprite1", 23, -40, 50, 30]
  ]
end

) then in draw, iterate through them.

draw = function()
  screen.clear()
  for obj in objects
    screen.drawSprite(obj[0], obj[1], obj[2], obj[3], obj[4])
  end
end

and if you want to, say, remove the last object when you press the F key, you could do that in update pretty simply

update = function()
  if keyboard.press.F then
    objects.removeAt(objects.length-1)
  end
end

or maybe you want to remove the third object after 300 frames?

update = function()
  if t >= 300 and not removed then
    removed = 1
    objects.removeAt(2)
  end
  t += 1
end

Hope this helps :D

Thanks a lot this really helps.

yw :)

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community