Remove object after moving to another scene

Hi !
in the scene1, I put an object outside a scene group to say " welcome User" after login to be visible in the next scene2.
Now I need to remove it in the next scene 3 but I can’t.

Can I do it like that ?

Hi,
option 1:
create an external lua module to keep consistency of objects across different scenes. (You just need to create a table into a new lua file and then require it anywhere).

option 2: use a global variable, alias don’t put “local” in front of the variable when you declare it (this is a bad practice)

Great !!!

Thank you for your answer I’m new in solar2d I haven’t understood a lot of things yet, so I chose option 2.
it’s easier.
I declare the object in the first scene then I update it in the third scene. it works very well.
except I can’t change the position object in the new scene.

Of course you can change the position. The global variable has a bigger scope, but it behaves like a local variable.

If you want to show a display object in Scene2 based on some action in Scene1 (I hope I’ve understood you correctly), my recommendation will be to use a global variable to indicate to Scene2 that it should make its own display object. I believe the Composer API also allows you to pass tables and other data between scenes so you can also possible use this to communicate relevant information from Scene1 to Scene2

my problem is that i don’t know anything about creating a table. that’s why I’m happy to keep it simple.

I do as stefano.mazzotta suggested to me. create the object in scene1 then update it in scene2 and it worked. here is the code

scene1: `

Status1 = display.newText( " ", 0, 0, font, 35)
Status1.x = 500
Status1.y = -80
Status1:setFillColor( 1, 1, 0 )

update in scene1 with an event :Status1.text = "Welcome " …success.name`

in scene scene2 I update is like that : Status1.text = " "

I wonder if I can change the X or Y position of the object.

I would recommend that you create a module for something like this, like Stefano already suggested.

local M = {}

function M.createObject( … )
    ….
end

return M

You can create your display objects so that they are only accessible from the module or you could add them to the M table for easier access, etc.

With this approach, you can keep the global space free and your code cleaner, all the while having easy access to your objects from any scene by requiring the module.

Listen !
my object come from a module below the code

local m = {}

function m.init()
   m._userDB = table.load("myID.txt") or {}
end


function m.classIDExists( classID )
   for i = 1, #m._userDB do
       if( classID == m._userDB[i].classID ) then 
         return true
      end
   end
   return false
end



function m.addUser( name, classID )
   local record = { name = name, classID = classID }
   if( not m.classIDExists(classID) ) then
      table.insert(m._userDB, record )
      table.save( m._userDB, "myID.txt")
      return true, name.."has been added"
   end
   return false, classID.. "already exist !"
end




function m.getRecordByName( name )
   for i = 1, #m._userDB do
       if( name == m._userDB[i].name ) then 
         return m._userDB[i]
      end
   end
   return nil
end




function m.getRecordByclassID( classID )
   for i = 1, #m._userDB do
       if( classID == m._userDB[i].classID ) then 
         return m._userDB[i]
      end
   end
   return nil
end







in the Scene1 I call this module like that

Status1 = display.newText( " ", 0, 0, font, 35)
Status1.x = 500
Status1.y = -80
Status1:setFillColor( 1, 1, 0 )
local success, reason = mymodule.getRecordByclassID( classID )
if(  success ) then       Status1.text = "Welcome " ..success.name
else
print ("User does not exist")
end

in the scene2 I update it like that

Status1.text = " "

now I need to update it in scene3 but in a different location

Your module seems to have its own issues. For instance, your function getRecordsByclassID doesn’t actually return two values, but you’re apparently expecting two values in your Scene1. You’re also returning Booleans and nil from the functions, but if you are returning just nil, you don’t need to bother doing so, etc.

Also, and this is just a style preference, but I’d recommend sticking to just one naming style. You seem to be going for camelCase, but then you have some new words in function names starting with lowercase, and you’ve got ID fully capitalised, etc.

But, anyway, if I were you, I would go ahead and just create the display object inside your module. You could, for instance, just create it in the init function like so:

function m.init()
  m._userDB = table.load("myID.txt") or {}
  m.object = display.newText( "", 0, 0, font, 35 )
end

Now you could update, control or remove it from any scene that calls the module by accessing mymodule.object.

thank you very much everything works

---to call object  --- 
mymodule.Status1.text = "Welcome " ..success.name  

----- to change position
mymodule.Status1.x = 180
mymodule.Status1.y = 780
mymodule.Status1:setFillColor( 1, 1, 0 )

---- to  hide object
mymodule.Status1.text = " "

I try this

mymodule.Status1.size = 18

but I ignore that

mymodule.Status1.font  =   ??????

could you teach me all supported ?

You can learn all these things from the docs: