Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Deleting Text - But, I promise, I have searched everywhere and I am still lost.
Started by dalekirkwood May 02 2019 01:41 PM

* * * * * 1 votes
8 replies to this topic
composer error removeself()

Best Answer anaqim , 03 May 2019 - 02:04 PM

Hi,

 

Text objects can be added to display groups just like any other display objects.

 

local dg=display.newGroup()

local mytext=display.newText(blablabl)

dg:insert(mytext) -- insert the text object into the display group

 

to get rid of the display group and all objects inside it:

display.remove(dg)

dg=nil

 

and just as easily to get rid of a text object:

display.remove(mytext)

mytext=nil

 

 

it is in general better to use display.remove() instead of object:removeSelf()

 

if you use the same variable in a loop to create objects, you will lose control over them (unless you also add them to a display group!

its would then be better create a table to hold those loop objects like this:

 

myobjects={}

for n=1,10 do

   myobjects[n]=display.newText(/blablabla)

end

 

this way you can reference each of the looped objects directly, unless you put all in a parent display group of course, which makes things very often much simpler.

 

hope this gives some hints :)

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

dalekirkwood

[GLOBAL: userInfoPane.html]
dalekirkwood
  • Observer

  • 7 posts
  • Corona SDK

Hi All

 

I hope I can explain this ok.

 

I have an app, when I change scenes I want it to delete everything, but the text stays.

 


  • Because text cannot be added to a group like an image, it doesn't adhere to normal rules and as such needs to be removed manually and always sits on top


So, I get it, I can't insert to a group and I have to remove it manually.

local function gotohome()
	if (ranknum) then
	ranknum:removeSelf()
	ranknum = nil
	end  

	

	composer.gotoScene("menu", {time = 600, effect = "slideRight"})
		

end 

but, this is displaying a table, so the variable 'ranknum' has been used several time in a do....while loop. So this just removes the last text added, not all the text.

 

Please see the loop below

local function loadday()

	display.remove (today)
	display.remove (week)
	display.remove (month)

		for i = 1, #feedstable do
			if (feedstable[i]) then 
				local yPos = 150 + (i * 56)

				print (feedstable[i].dateday)
				print ("i"..i)
				print ("legnth"..#feedstable)
				

				local headertext = display.newText("Date          Time     ml     FeedType",
				display.contentCenterX-300,display.contentCenterY-400,native.systemFont,36)
				headertext.anchorX = 0
				headertext:setFillColor(0,0,0)
				
				local datetime = os.date ("*t") --get date and time and store in a table
				
				if (feedstable[i].dateyear == datetime.year and 
				feedstable[i].datemonth == datetime.month and
				feedstable[i].dateday == datetime.day
				) then
			
					ranknum = display.newText(i,
					display.contentCenterX-320, yPos,"Arial", 36)
					ranknum:setFillColor (0.8)
					ranknum.anchorX = 1


					local thisscore = display.newText(feedstable[i].dateday.."."..
					feedstable[i].datemonth..
					"."..feedstable[i].dateyear..
					"    "..feedstable[i].timehour..
					":"..feedstable[i].timemin..
					"    "..feedstable[i].feedammount..
					"    "..feedstable[i].feedtype,
					display.contentCenterX-300, yPos, native.systemFont, 36)
					thisscore.anchorX = 0
					thisscore:setFillColor (0,0,0)
					




				end
	
						
			end
		end
	end
 

 I have attached an image where you can see the text has been removed, but 2 & 3 are still there.

 

And of course, that makes sense. How would Corona know to remove them also, they are no longer the ranknum variable.

 

So my question is, how, in the simplest way possible, can I clear the screen. In other scenes I do not have this issue with text, but here I do.

 

Please someone help me. I spent 2 or 3 hours Googling this, I am not the only one who has had this problem but it was never the exact same.

 

All the code below, if it helps


local composer = require( "composer" )

local scene = composer.newScene()

-- -----------------------------------------------------------------------------------
-- Code outside of the scene event functions below will only be executed ONCE unless
-- the scene is removed entirely (not recycled) via "composer.removeScene()"
-- -----------------------------------------------------------------------------------




-- -----------------------------------------------------------------------------------
-- Scene event functions
-- -----------------------------------------------------------------------------------

-- create()

local today
local week
local month

local ranknum

local function gotohome()
	if (ranknum) then
	ranknum:removeSelf()
	ranknum = nil
	end  

	

	composer.gotoScene("menu", {time = 600, effect = "slideRight"})
		

end

local function addfeed()
	composer.gotoScene("AddFeed",{time=600,effect = "crossFade"})
	end

local function gotocalc()
	composer.gotoScene("calculator",{time=400, effect ="crossFade"})
end
	
local function gotoabout()
	composer.gotoScene("about",{time=400, effect ="crossFade"})
end

local json = require ("json")

local filePath = system.pathForFile ("feeds.json",system.DocumentsDirectory)


local file = io.open (filePath, "r") --open file as read only

local contents = file:read("*a") -- load contents into variable 'contents'
		io.close(file) -- closes the file
		feedstable = json.decode (contents) -- decode the file into LUA table

local function loadday()

	display.remove (today)
	display.remove (week)
	display.remove (month)

		for i = 1, #feedstable do
			if (feedstable[i]) then 
				local yPos = 150 + (i * 56)

				print (feedstable[i].dateday)
				print ("i"..i)
				print ("legnth"..#feedstable)
				

				local headertext = display.newText("Date          Time     ml     FeedType",
				display.contentCenterX-300,display.contentCenterY-400,native.systemFont,36)
				headertext.anchorX = 0
				headertext:setFillColor(0,0,0)
				
				local datetime = os.date ("*t") --get date and time and store in a table
				
				if (feedstable[i].dateyear == datetime.year and 
				feedstable[i].datemonth == datetime.month and
				feedstable[i].dateday == datetime.day
				) then
			
					ranknum = display.newText(i,
					display.contentCenterX-320, yPos,"Arial", 36)
					ranknum:setFillColor (0.8)
					ranknum.anchorX = 1


					local thisscore = display.newText(feedstable[i].dateday.."."..
					feedstable[i].datemonth..
					"."..feedstable[i].dateyear..
					"    "..feedstable[i].timehour..
					":"..feedstable[i].timemin..
					"    "..feedstable[i].feedammount..
					"    "..feedstable[i].feedtype,
					display.contentCenterX-300, yPos, native.systemFont, 36)
					thisscore.anchorX = 0
					thisscore:setFillColor (0,0,0)
					




				end
	
						
			end
		end
	end




function scene:create( event )

	local sceneGroup = self.view
	local ranknum 

	local background = display.newImageRect (sceneGroup, "Background.png",800,1400)
	background.x = display.contentCenterX
	background.y = display.contentCenterY

	local footer = display.newImageRect(sceneGroup,"Footer.png",793,120)
	footer.x = display.contentCenterX
	footer.y = 1150

	local header = display.newImageRect(sceneGroup,"Header.png",794,140)
	header.x = display.contentCenterX
	header.y = -100

	local mainbutton = display.newImageRect (sceneGroup, "Button.png",120,120)
	mainbutton.x = display.contentCenterX
	mainbutton.y = 1080

	local logo = display.newImageRect(sceneGroup,"pumpit.png",282,122)
	logo.x = display.contentCenterX
	logo.y = -50

	local circle = display.newImageRect (sceneGroup, "circle.png",95,95)
	circle.x = 232
	circle.y = 1140

	local homebutton = display.newImageRect (sceneGroup,"home.png",69,69)
	homebutton.x = 70
	homebutton.y = 1140

	local feeds = display.newImageRect (sceneGroup,"feeds.png",69,69)
	feeds.x = 230
	feeds.y = 1140

	local calc = display.newImageRect (sceneGroup,"calculator.png",69,69)
	calc.x = 550
	calc.y = 1140

	local info = display.newImageRect (sceneGroup,"info.png",69,69)
	info.x = 700
	info.y = 1140

	today = display.newImageRect (sceneGroup,"today.png",598,158)
	today.x = display.contentCenterX
	today.y = 300

	 week = display.newImageRect (sceneGroup,"week.png",598,158)
	week.x = display.contentCenterX
	week.y = 500

	 month = display.newImageRect (sceneGroup,"month.png",598,158)
	month.x = display.contentCenterX
	month.y = 700

	

	homebutton:addEventListener ("tap", gotohome)
	mainbutton:addEventListener ("tap", addfeed)
	calc:addEventListener("tap", gotocalc)
	info:addEventListener("tap", gotoabout)
	today:addEventListener("tap", loadday)
	-- Code here runs when the scene is first created but has not yet appeared on screen

end


-- show()
function scene:show( event )

	local sceneGroup = self.view
	local phase = event.phase

	if ( phase == "will" ) then
		-- Code here runs when the scene is still off screen (but is about to come on screen)

	elseif ( phase == "did" ) then
		-- Code here runs when the scene is entirely on screen

	end
end


-- hide()
function scene:hide( event )

	local sceneGroup = self.view
	local phase = event.phase

	

		if ( phase == "will" ) then
			-- Code here runs when the scene is on screen (but is about to go off screen)
			
			composer.removeScene("viewfeeds")
			


	
		elseif ( phase == "did" ) then
			composer.removeScene("viewfeeds")
	end
end


-- destroy()
function scene:destroy( event )
	local sceneGroup = self.view
	-- Code here runs prior to the removal of scene's view

end


-- -----------------------------------------------------------------------------------
-- Scene event function listeners
-- -----------------------------------------------------------------------------------
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-- -----------------------------------------------------------------------------------

return scene

Thanks in advanced 

 

Dale

Attached Thumbnails

  • Sketch (7).png


[TOPIC: post.html]
#2

anaqim

[GLOBAL: userInfoPane.html]
anaqim
  • Contributor

  • 770 posts
  • Corona SDK

  Best Answer

Hi,

 

Text objects can be added to display groups just like any other display objects.

 

local dg=display.newGroup()

local mytext=display.newText(blablabl)

dg:insert(mytext) -- insert the text object into the display group

 

to get rid of the display group and all objects inside it:

display.remove(dg)

dg=nil

 

and just as easily to get rid of a text object:

display.remove(mytext)

mytext=nil

 

 

it is in general better to use display.remove() instead of object:removeSelf()

 

if you use the same variable in a loop to create objects, you will lose control over them (unless you also add them to a display group!

its would then be better create a table to hold those loop objects like this:

 

myobjects={}

for n=1,10 do

   myobjects[n]=display.newText(/blablabla)

end

 

this way you can reference each of the looped objects directly, unless you put all in a parent display group of course, which makes things very often much simpler.

 

hope this gives some hints :)



[TOPIC: post.html]
#3

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,810 posts
  • Corona SDK

Yes, think there may be some confusion with native.newTextField objects, which must be removed manually, and display.newText objects which can be added to the scene group and will be removed automaticallly along with everything else.

But still worth reading up on scope and tables. As Anaqim says, if you want to later access objects created in a loop, add them to a table.
  • dalekirkwood likes this

[TOPIC: post.html]
#4

dalekirkwood

[GLOBAL: userInfoPane.html]
dalekirkwood
  • Observer

  • 7 posts
  • Corona SDK

Wow, Thanks for the fast replies everyone.

 

@anaqim - yes, I agree, a table would be better. I am going to refine the code :-)

 

@nick_sherman - I also thought this, I understand native.newTextField can only be removed manually but I was having an issue adding display.newText to a group.

 

I managed to fix this yesterday. After hours of trying, I actually found I couldn't add any display.newText to sceneGroup while it is nested so deeply. Local Function > For - Do > If > If. 

 

What I did, was create a new scene, added the code there and it works perfectly.

 

In my search I found this forum post which I think explains it better what my problem is https://forums.coronalabs.com/topic/48540-scenegroup-a-nil-valuehow-is-that-possible/

 

Anyway, Thank you so much again for your fast replies. :-D



[TOPIC: post.html]
#5

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,810 posts
  • Corona SDK

I don't think the nesting will be the issue. The only thing to be aware of is you must add it to sceneGroup while you have the reference to it...i.e, in the same scope, immediately after it is created.

If it were nesting this would have cropped up in the 8-9 years the framework has been available. That post just shows you can't add an object that isn't a display object to a scene group.

If your text object was out of scope and you tried to add it, then it would be nil and the effect would be similar to what that poster saw.

Make liberal use of print statements while developing. At each stage confirm that variables, objects etc. are what you think they are.
  • nick_sherman and dalekirkwood like this

[TOPIC: post.html]
#6

dalekirkwood

[GLOBAL: userInfoPane.html]
dalekirkwood
  • Observer

  • 7 posts
  • Corona SDK

local mldaytext = 1

local mlperfeedtext = 1
sceneGroup:insert (mldaytext)
sceneGroup:insert (mlperfeedtext)

 

 

 

So for example, this should work?



[TOPIC: post.html]
#7

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,810 posts
  • Corona SDK

No, because those aren't display objects. They are ordinary variables that just happen to have the word text in them.
  • nick_sherman and dalekirkwood like this

[TOPIC: post.html]
#8

dalekirkwood

[GLOBAL: userInfoPane.html]
dalekirkwood
  • Observer

  • 7 posts
  • Corona SDK

I think the penny has dropped.

 

So, I take a variable such as mldaytext=display.newText ("Text") , and then add that to a display group which I can then remove but I can't add the variable to the display group before, I must add it once it is already a text object.

 

Thank you for your help in understanding this :-)



[TOPIC: post.html]
#9

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,810 posts
  • Corona SDK

You would use the variable mldaytext within your call to display.newText. Then add the display.newText to sceneGroup, or another display group that is itself added to sceneGroup.

EDIT: I see you edited your post, we're on the same page.
  • nick_sherman likes this


[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: composer, error, removeself()