removing an object while rotating causes error

Hi,

I have runtime listener that is listening for touches anywhere to the screen. On touch it rotates a display object. The trouble is I have the display object set to be removed on collision with another object. When this happens while I am rotating the object I get an error because I’m trying to rotate an object that no longer exists. I’m not sure how to resolve the issue. Below is my rotate function:

local function rotate(event) local t = beans[#beans]; #My display object if (event.phase == "began") then touchStart = event.y end if (event.phase == "moved") then if (event.y \< touchStart) then if(t ~= nil) then t:rotate( -2 ); end end if (event.y \> touchStart) then if(t ~= nil) then t:rotate( 2 ); end end touchStart = event.y; end end Runtime:addEventListener("touch", rotate); [import]uid: 31694 topic_id: 22961 reply_id: 322961[/import]

Its hard to help without seeing your code in context, but try to remove the event listener before removing your object. [import]uid: 33608 topic_id: 22961 reply_id: 91726[/import]

Try adding a flag at the beginning of the function. Something like this:

[lua]local function rotate(event)
local t = beans[#beans]; #My display object
if t ~= nil then
if (event.phase == “began”) then
touchStart = event.y
end
if (event.phase == “moved”) then
if (event.y < touchStart) then
if(t ~= nil) then
t:rotate( -2 );
end
end
if (event.y > touchStart) then
if(t ~= nil) then
t:rotate( 2 );
end
end
touchStart = event.y;
end
end
end
Runtime:addEventListener(“touch”, rotate);[/lua] [import]uid: 82699 topic_id: 22961 reply_id: 91734[/import]

Thanks but neither solutions worked. In terms of the flag I already have a flag check around the rotate line. I added the additional flag but it made no difference. I still get the “attempt to call method rotate a nil value” error.

As for removing the listener, if I remove the event listener I am unable to rotate any newly spawned display objects that rely on it.

To add more context: every 2 seconds a display object is created and falls. On collision with the other display objects there is a chance they will be removed. The rotate function has to swap to only affecting the last created display object and has to work by touching anywhere on the screen. Not just touching the display object.
[import]uid: 31694 topic_id: 22961 reply_id: 91884[/import]

I even tried putting the call to the rotate method in a pcall but that still returned an error. [import]uid: 31694 topic_id: 22961 reply_id: 91892[/import]

Ok, let me see if I can explain the problem based the the code you posted from the other post.
When you created the ‘bean’, you placed it in the ‘beans’ table. When the collision happened, you deleted the beans using ‘removeSelf’ but you never deleted that ‘beans’ from the ‘beans’ table. In effect, the ‘bean’ references are still there but there’s no real ‘bean’. So when you do something like this:

 local t = beans[#beans]; #My display object  
 if t ~= nil then  
 if (event.phase == "began") then  
 touchStart = event.y  
 end  
 if (event.phase == "moved") then  
 if (event.y \< touchStart) then  
 if(t ~= nil) then  
 t:rotate( -2 );  
 end  

‘t’ may be not be nil but it’s an old reference; the ‘bean’ is gone and therefore ‘rotate’ will not work as well.
What you need to do is remove the ‘bean’ from the ‘beans’ table every time it’s removed.

I going to a meeting now but will check in later to see if you found a solution.
Note: Depending on how you use the ‘beans’ table, just putting ‘nil’ in place of the old ‘bean’ reference could break your table.

Jeff
[import]uid: 14119 topic_id: 22961 reply_id: 91921[/import]

I don’t know what I would do without you. I have had some success by doing the following:

In my collision test I checked if the collision was with the last generated bean and if so set the reference in beans to ‘none’

if (object2 == beans[#beans] or object1 == beans[#beans]) then  
 beans[#beans] = 'none'  
end  

I’ve also changed my flag to check for none before doing the rotate

if(m ~= nil and m ~= 'none') then  
 beans[#beans]:rotate( 2 );  
end  

I’m currently play testing for bugs as I saw some odd things in the terminal at first that I am having trouble duplicating. If you can suggest a better way I’m all ears. [import]uid: 31694 topic_id: 22961 reply_id: 91936[/import]

Ok scrap that I am still able to generate the error. Although now it is only under certain conditions. [import]uid: 31694 topic_id: 22961 reply_id: 91937[/import]

Basically what you need to do is when the collision occurs and a touch event is in progress, you cancel the touch event and then delete the object.
Remember the ‘isFocus’ flag? Check to see if the ‘isFocus’ flag is set on the object before deleting the object. You can do the check in the ‘bean:pop()’ function.

 if (self.isFocus == true) then  
 display.getCurrentStage():setFocus( nil );  
 self.isFocus = false;  
 self.touchEnabled = false; -- make sure no more touching  
 end  
 end;  
  
 -- delete the bean in the table  
 for k=#beans, 1, -1 do  
 if beans[k] == self then table.remove[beans,k]; end  
  
 --Finally remove the object  
 self:removeSelf();  
 self = nil;  
 collectgarbage("collect");  

I believe this is all you need but I might be forgetting something.

Jeff

[import]uid: 14119 topic_id: 22961 reply_id: 92055[/import]

Yay it appears to be working now. I did have to use a different flag though. I’m now using rotateEnabled to test to rotate and touchEnabled to test if I can move the bean from left to right.

Thanks a lot I really appreciate it. Now on to the next step. I still have to add score, the ability to preview the next colour and increase the speed of the decent as you progress. That last one should be the hardest as I want to start from a much slower speed than I have atm which is being controlled by gravity. [import]uid: 31694 topic_id: 22961 reply_id: 92138[/import]

Of course you probably already discovered it but I forgot to put an ‘end’ to the ‘for’ loop.

 -- delete the bean in the table  
 for k=#beans, 1, -1 do  
 if beans[k] == self then table.remove[beans,k]; end  
 end  

Jeff
[import]uid: 14119 topic_id: 22961 reply_id: 92157[/import]