Removing object from main variable?

Below is just a sample code to reproduce my problem i am facing recently. Assume that createObject and onRelease function can’t access stageObj variable.

local stageObj  
  
-------------------------------------------------  
local function createObj()  
 local obj = display.newImage( "apple.png" )  
  
 function obj:remove()  
 display.remove( self )  
 self = nil  
 end  
  
 return obj  
end  
-------------------------------------------------  
local function move()  
  
 if stageObj ~= nil then  
 stageObj.x = stageObj.x + 1  
 if stageObj.x \>= display.contentWidth - ( stageObj.contentWidth / 2 ) then  
 stageObj.x = stageObj.contentWidth / 2  
 stageObj.y = stageObj.y + stageObj.contentHeight  
 end  
 end  
end  
  
-------------------------------------------------  
local function onRelease( event )  
 if event.phase == "ended" then  
 event.target:remove()  
 end  
  
 return true  
end  
  
-------------------------------------------------  
local function main()  
 Runtime:addEventListener( "enterFrame", move )  
  
 stageObj = createObj()  
 stageObj:addEventListener( "touch", onRelease )  
  
end  
  
-------------------------------------------------  
main()  

My problem: When the object is removed by calling it’s own remove method, stageObj won’t be nil. Can anyone explain why and how do i resolve this issue? [import]uid: 74723 topic_id: 27556 reply_id: 327556[/import]

When you set self = nil, you’re just assigning a nil value to the self variable, not stageObj since it’s just a reference. Instead of self = nil, use stageObj = nil and it’d work. [import]uid: 108204 topic_id: 27556 reply_id: 112238[/import]