i can't remove image from image random

This code

i can’t remove image after touch function backTitle

thanks

[lua]local function backTitle()

storyboard.gotoScene( “menu”, “fade”, 500 )

randomCharecter:removeSelf()

checkTrue = true

return true
end

function scene:createScene( event )
local group = self.view

local backBtn = display.newImage( “picMain/btn_menu.png” )
backBtn.x = display.contentWidth /2 + 280
backBtn.name = “backButton”
local function listener(event)
if event.target.name == “backButton” then
storyboard.gotoScene( “menu”, “fade”, 500 )
storyboard.removeScene( “GameFind”)
end
end
backBtn:addEventListener( “touch” , listener )

group:insert( backBtn )

end

local function randomCharecter ()

math.randomseed( os.time() )

local imageToDisplay = “picGame/find/Charecter” … math.random(4) … “.png”

local randomCharecter = display.newImageRect(imageToDisplay, 100, 200)

randomCharecter.x = display.contentWidth*0.5
randomCharecter.y = display.contentHeight - 200

end

local function removeCharecter ()

if checkTrue == true then

local randomCharecter = stage[1]
randomCharecter:removeSelf()
randomCharecter = nil
end

end

end

randomCharecter ()
removeCharecter ()[/lua] [import]uid: 169163 topic_id: 29649 reply_id: 329649[/import]

try to helps
[lua]local player

local function randomCharecter ()
math.randomseed( os.time() )

local imageToDisplay = “picGame/find/Charecter” … math.random(4) … “.png”

local randomCharecter = display.newImageRect(imageToDisplay, 100, 200)

randomCharecter.x = display.contentWidth*0.5
randomCharecter.y = display.contentHeight - 200
return randomCharecter
end

local function removeCharecter ()

if checkTrue == true then

–local randomCharecter = stage[1]
–i dont know this means
player:removeSelf()
player= nil
end

end

end

player=randomCharecter ()
removeCharecter () [import]uid: 160941 topic_id: 29649 reply_id: 119002[/import]

Do you mean you want your character image to be removed when you click back and go to the menu scene?

The reason the character stays on screen is you haven’t added it to a display group so it is not cleaned up automatically, and in your code your backTitle() function is never called to do this manually.

What I do is put this at the top of each scene:
[lua]local localGroup = display.newGroup()[/lua]

Within create scene, instead of:

[lua]local group = self.view[/lua]

I use:

[lua]localGroup = self.view[/lua]

This way, every object is added to a display group (localGroup) that I can reference from other functions in the scene, so I can manipulate the alpha value, position, rotation etc of the whole scene. I might group my objects into hudGroup, popupGroup, finishGroup etc and then insert those to localGroup, so I can manipulate common groups of objects in one go (i.e. move a popup that I initialised off screen into view).

I can also then add objects to localGroup outside of create scene if I need to (i.e. changing images on the fly dependent on game status).

When I perform storyboard.loadScene to go elsewhere, all these objects are automatically removed from screen and memory for me. [import]uid: 93133 topic_id: 29649 reply_id: 119007[/import]