Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

How to remove display objects declared with the same variable
Started by Quitalizner Nov 20 2018 02:00 AM

3 replies to this topic
display.remove

Best Answer XeduR @Spyric , 20 November 2018 - 02:41 AM

You'll probably want to read this: http://lua-users.org/wiki/GarbageCollectionTutorial.

There are means of removing variables in Lua even after you've lost reference to them. In Corona, perhaps the easiest approach of removing display objects is to simply add both lines into a display group. Then you'll retain a reference to those objects via the group, e.g.

 

local myGroup = display.newGroup()
local line
line = display.newLine(myGroup, 10, 20, 50, 60) -- group child #1
line = display.newLine(myGroup, 50, 70, 60, 40) -- group child #2

display.remove(line)
line = nil
display.remove(myGroup[1])

Still, this isn't something that you should knowingly do. I would ask you a counter question: If you know all that, then why would you want to create a problem for yourself by creating two line(s) and intentionally losing the reference to the first line?

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

Quitalizner

[GLOBAL: userInfoPane.html]
Quitalizner
  • Enthusiast

  • 30 posts
  • Corona SDK

So when i create two different lines using the same variable and try to use display.remove, only the recently created line is removed.

I am aware that the reference to the previous line is lost. But is there a way to remove that too.

And i am also aware that i can use a hash table to create two or more lines using same variable. But what i want to know is whether any method exists to remove the previous line.

 

Example:-

local line
line = display.newLine(10, 20, 50, 60) -- First
line = display.newLine(50, 70, 60, 40) -- Second
 
display.remove(line)
line = nil.
 
Here only the second line gets removed. But now i have lost reference to the first line. So i want to know if there is a way to remove the first line.
 
I'm just asking to know if its possible.


[TOPIC: post.html]
#2

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 368 posts
  • Corona SDK

  Best Answer

You'll probably want to read this: http://lua-users.org/wiki/GarbageCollectionTutorial.

There are means of removing variables in Lua even after you've lost reference to them. In Corona, perhaps the easiest approach of removing display objects is to simply add both lines into a display group. Then you'll retain a reference to those objects via the group, e.g.

 

local myGroup = display.newGroup()
local line
line = display.newLine(myGroup, 10, 20, 50, 60) -- group child #1
line = display.newLine(myGroup, 50, 70, 60, 40) -- group child #2

display.remove(line)
line = nil
display.remove(myGroup[1])

Still, this isn't something that you should knowingly do. I would ask you a counter question: If you know all that, then why would you want to create a problem for yourself by creating two line(s) and intentionally losing the reference to the first line?


  • Quitalizner likes this

[TOPIC: post.html]
#3

Quitalizner

[GLOBAL: userInfoPane.html]
Quitalizner
  • Enthusiast

  • 30 posts
  • Corona SDK

I'll take a look into the article you have sent. And to answer your question, i sometimes make the above mistake when there is looping involved. I was curious to find out if there was a way to remove the objects without reference.i thought maybe i can call garbage collection process to free up the memory.

I apologize if you felt my question a bit dumb. Thanks again for pointing out to the article.   



[TOPIC: post.html]
#4

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 368 posts
  • Corona SDK

It isn't a dumb a question at all, but I think that this is a classic case of figuring out how to patch a problem instead of actually fixing the cause of the problem.

It is always better to fix the root of the problem instead of trying to mitigate the negative results.


  • Quitalizner likes this


[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: display.remove