Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Second call loses reference
Started by horacebury Sep 01 2018 09:41 PM

1 reply to this topic
constructor widget touch

Best Answer horacebury , 01 September 2018 - 09:59 PM

It turns out that the event listener was not being removed - so I added a finalize() and remove the touch listener:

I have created a wrapper for the scrollview widget and when I first call it everything is fine - the internal 'scrollview' variable is referenced as intended.

When I remove the scrollview and, later, create it again, the internal 'scrollview' variable is still referencing the old value.

Why is this happening? Have I forgotten something fundamental about Lua closures?
local widget = require("widget")

function widget.newDragItemsScrollView( params )
	local scrollview = widget.newScrollView( params )
	print("scrollview: ",scrollview)
	
	function scrollview:add( item, listener, dragtime, angle, radius, touchthreshold )
		scrollview:insert( item )
		
		touch = function( event )
			touchevent = event
			if (event.phase == "began") then
				display.currentStage:setFocus( event.target, event.id )
				event.target.hasFocus = true
				return true
			elseif (event.target.hasFocus) then
				if (event.phase == "moved") then
					scrollview:takeFocus(event)
print(scrollview) -- always prints the FIRST value of scrollview
					return false
				else
					display.currentStage:setFocus( event.target, nil )
					event.target.hasFocus = nil
				end
				return true
			end
			return false
		end
		
		item:addEventListener( "touch", touch )
		
		scrollview:addEventListener( "finalize", function()
			item:removeEventListener( "touch", touch )
		end )
	end
	
	return scrollview
end

[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

horacebury

[GLOBAL: userInfoPane.html]
horacebury
  • Corona Geek

  • 3,069 posts
  • Corona SDK

I have created a wrapper for the scrollview widget and when I first call it everything is fine - the internal 'scrollview' variable is referenced as intended.

 

When I remove the scrollview and, later, create it again, the internal 'scrollview' variable is still referencing the old value.

 

Why is this happening? Have I forgotten something fundamental about Lua closures?

local widget = require("widget")

function widget.newDragItemsScrollView( params )
	local scrollview = widget.newScrollView( params )
	print("scrollview: ",scrollview)
	
	function scrollview:add( item, listener, dragtime, angle, radius, touchthreshold )
		scrollview:insert( item )
		
		touch = function( event )
			touchevent = event
			if (event.phase == "began") then
				display.currentStage:setFocus( event.target, event.id )
				event.target.hasFocus = true
				return true
			elseif (event.target.hasFocus) then
				if (event.phase == "moved") then
					scrollview:takeFocus(event)
print(scrollview) -- always prints the FIRST value of scrollview
					return false
				else
					display.currentStage:setFocus( event.target, nil )
					event.target.hasFocus = nil
				end
				return true
			end
			return false
		end
		
		item:addEventListener( "touch", touch )
	end
	
	return scrollview
end



[TOPIC: post.html]
#2

horacebury

[GLOBAL: userInfoPane.html]
horacebury
  • Corona Geek

  • 3,069 posts
  • Corona SDK

  Best Answer

It turns out that the event listener was not being removed - so I added a finalize() and remove the touch listener:

I have created a wrapper for the scrollview widget and when I first call it everything is fine - the internal 'scrollview' variable is referenced as intended.

When I remove the scrollview and, later, create it again, the internal 'scrollview' variable is still referencing the old value.

Why is this happening? Have I forgotten something fundamental about Lua closures?
local widget = require("widget")

function widget.newDragItemsScrollView( params )
	local scrollview = widget.newScrollView( params )
	print("scrollview: ",scrollview)
	
	function scrollview:add( item, listener, dragtime, angle, radius, touchthreshold )
		scrollview:insert( item )
		
		touch = function( event )
			touchevent = event
			if (event.phase == "began") then
				display.currentStage:setFocus( event.target, event.id )
				event.target.hasFocus = true
				return true
			elseif (event.target.hasFocus) then
				if (event.phase == "moved") then
					scrollview:takeFocus(event)
print(scrollview) -- always prints the FIRST value of scrollview
					return false
				else
					display.currentStage:setFocus( event.target, nil )
					event.target.hasFocus = nil
				end
				return true
			end
			return false
		end
		
		item:addEventListener( "touch", touch )
		
		scrollview:addEventListener( "finalize", function()
			item:removeEventListener( "touch", touch )
		end )
	end
	
	return scrollview
end




[topic_controls]
[/topic_controls]