Remove a text

I’m trying to build an app which should show a set of questions with multiple choices.
Although this is my first application I succeeded to arrive at the scene where I have to show a question with its multiple choices. Unfortunately, when I have to show the next question it overlaps the previous! How to remove the previous one? All the solutions I found in this forum and on youtube don’t work for me!
Thanks in advance

If you have the habit of adding objects to groups, there is an easy way.

local Group = display.newGroup()

local function ClearGroup()
	for i = Group.numChildren,1,-1 do
	return true

You can just remove the group, you don’t have to iterate over each child.


1 Like

Hi, @kbradford this way will the whole group be removed? I have to update only the part containing the question and its multiple choice.

@vb66r55 I tried that way but it didn’t work. I should show you my code, but it isn’t very short!

display.remove() will remove an image, display group, or vector. Just pass in what you want removed.

So yes, if you put everything in a display group called Group, then that’s how you’d remove it. Just make sure you put your questions and answers all in that group, then before you call off to the next round of questions remove that group.

Also use a better variable name than Group, that’ll get confusing fast.

If you just want to change the “text”, then you really don’t need to delete anything.

local TextBox = {text = “”,width=1300,font = native.newFont(“Huninn.ttf”),fontSize = 28,align = “left”}
local Text = display.newText( TextBox )
Text.text = “AQuestion”

if XXXX then
Text.text = “BQuestion”

This is just an example, you only need to use the bottom method, you can easily change the text, and activate it under the conditions you need.

1 Like

I tried you solution but the text is not shown at the center of the screen. I’ve seen the last letters of a long string at the top left corner. How can I choice the coordinates of the text? I’ve tried to add a “height=” parameters but nothing happened!

You should not use the top paragraph, that is the custom font I use.
The width depends on the value you need, align if you want to use the middle, please use “center”.
In addition, “height” also depends on the value you need, you can put them in.

@vb66r55 Hi, I’m confused! If I use your code as it is it display only the last character on the top of the screen, but if I change the alignment to “center” or “right” I don’t see anything!
If I restore your values and relaunch the project, it stop to display the text!
I don’t understand!

@vb66r55 I tested your code in a small and simple app. It works correctly, so I think it does work well in my application because I’m using “composer” and “groups”.

In General you have to declare objects as local objects at the top of the screen
and my advice is to declare a local display group as well

add this group to the scene group

add objects to this group in your code

so when the scene is removed this group will be removed and all objects added to it

if you don’t follow this … you will get problems

@kakula I did so! I’m lerning so I follow the tutorials I found. But I still have problems. I’m sure that there is some mistakes in my file. I’m not able to find. :sob: :sob:

@vb66r55 Hi, I’ve found a tutorial which solved my problem. Now I would like to know how can I set the font colors in TextBox? Thank you for you kindness.

Hi Saleh,

It’s very easy for anyone to make such mistakes, even for senior Solar2D developers
back in the days, when i faced such problems, and when i got frustrated trying to solve them, i used to think that Solar2D has bugs in its compilation and i was thinking that Solar2D is the one to blame … but as you work more with it and gain more experience you will begin to trust it more and more and make almost no bugs at all.

The things i told you about in my previous reply took me years to comprehend … although i read them in my first days many times … and i thought i comprehended them … but i was far away from that… i was reading but not properly applying what i read

and always remember some advices you get will not be written anywhere, because it will be from personal experience on various projects, while the tutorials will only guide you on how to do separate things each time in small blocks of code

So the easiest way for you as a new Solar2D developer is to always post your entire code, and it will be very easy for anyone to help you immediately … instead of sending tens of messages back and forth … and you will benefit from other notes as well regarding other things along the way


1 Like

Let me give you an important example which i faced weeks ago and again i was sure it is a bug in Solar2D but it wasen’t … this example is not really important for people who make apps in one language … but for those who make apps as multi or Bi lingual

if you place to words beside each other one English and another Arabic in two different fonts, and if you set yAnchor to 0 for both, and set y value the same for both, you will be surprised that they will not be aligned on the phone but aligned on the simulator

the solution is to make yAnchor=1 for both and it will work!! and set the same y value for both … so this is a very good example and what makes you sure it is a bug is that it is working properly on simulator

1 Like

@kakula Thank you for sharing your personal experience. I did post the entire code because I thought you could be annoyed. Sorry.


You can use this to change the color.
Also I think you should read↓

1 Like