Why can't the object-function link be retrieved after recreating the object.

If I delete an object, which launches a function, and re-creates it with the same name it loses the link to the function. Is there a way to get that link back?

How are you creating your objects??

The address of the functions are always unique, assigned on creation.
So if you are creating your function twice, then you are going to have 2 different addresses even if both functions do exactly the same.

No matter if the code runs in the exactly the same line or if the variable names are the same, every time it executes = function() ... end, you are creating a new function with a new address.

There are some ways of preserving/sharing the same address among multiple objects, those may look different, but at the end, the reason is the same.

Copying it directly from the original object

local t1 = {x = function() end}
local t2 = {}
t2.x = t1.x

print(t1.x, t2.x)

Using an intermediary

local x = function() end
local t1 = {x = x}
local t2 = {x = x}

print(t1.x, t2.x)

Using class inheritance

local T = {x = function() end}
    
function T:new(o)
    o = o or {}
    setmetatable(o, self)
    self.__index = self
    return o
end
    
function T:x()
   
end
    
local t1 = T:new()
local t2 = T:new()

print(t1.x, t2.x)

Of course, it goes without saying that each method has its reason for being and they work better in one situation or another.

I hope I have correctly understood your question and been helpful. :slight_smile:

I think it was not well understood. Let’s look at the problem step by step.

First I create the object:

Button = display.newImage (“Advance.png”, 674,480)

that object is assigned a function:

function Siguiente(event)

  • something

end

and that object and function are linked like this:

Button: addEventListener (“tap”, Siguiente)

Very simple.

At some point during the execution of the program I need to delete the object:

Button: removeSelf ()
Button = nil

Later … I need to return the object …

Then I create it again.

Button = display.newImage (“Advance.png”, 674,480)

Now the problem is another. The link between object and function was broken.
How do I get it back?

You can’t get the link back. Once the object is removed, you lost it.

What is the problem of readding the event listener to the new object??

I do not understand the question. But the problem starts when one wants to scale an object: make it bigger or smaller.

That is what leads me to “destroy” an object and recreate it.

It is very easy to enlarge it:

Object: scale (4, 4)

But if I want to take it to the original size, in stages, to be able to appreciate a progressive reduction of the object, things get complicated …
It is very complicated and it is very difficult for the object to return to its original size. Either it is smaller or it is larger. It is simply a decimal problem when multiplying or dividing.

So a solution was to delete it and recreate it. Which works perfectly. As long as that object does not have a link to some function.

I’m not completely sure if I understood the idea, but you may insert the image into a group and add the event listener to the group, not the image.

local group = display.newGroup()
group: addEventListener (“tap”, Siguiente)

local button = display.newImage (group, “Advance.png”, 674,480)

then…

button:removeSelf()

And later…

button = display.newImage (group, “Advance.png”, 674,480)

You can (and should) rescale objects using object.xScale, object.yScale = 1, 1 as those scaling factors remain constant.

I am going to try.

xScale and yScale are not functions. They are properties. If you set xScale to 0.5, it will scale the display object horizontally to 50%. If you then set it back to 1, it will scale it back to 100%.

The object:scale(x,y) method that you had described before scales the display object relative to its current size. The xScale and yScale, being properties, will always set the absolute value, not relative.


Also, like @depilz had said before, if you remove a display object and set it to nil, then anything associated with it will be removed from it (since it doesn’t exist anymore). If you want to restore listeners/functions, etc. that were attached to it, then you need to simply add them to it again manually.

If you scale using Object:scale(4, 4) then to return it (using scale) would be Object:scale (1/4, 1/4). The scaling is always based on the original asset so you will not lose fidelity like if you did this in a graphics package.

As previously stated, the best way to return an object to default is Object.xScale, Object.yScale = 1, 1

But if I want to take it to the original size, in stages, to be able to appreciate a progressive reduction of the object, things get complicated …

Is it really necessary to delete and recreate? Can’t you just use the transition library?

local obj = display.newRect(100, 100, 10, 10)
obj.xScale, obj.yScale = 4, 4
transition.to(obj, {xScale = 1, yScale = 1, time = 3000})