I have a problem removing objects (images) in my app. I don’t know how to fix this.
I create and display two images based on two API calls. The idea is: when one image is clicked, all objects should be removed and the user goes to a new page/scene. Depending on the image clicked, the new page is different.
So far, I can remove the image the user clicked and move to another page (event.target:removeSelf()). But the other image cannot be removed. The easiest solution, I think, is to put the two images (each event.target of each API call) into one sceneGroup and remove it, when one of the two images is clicked, but I don’t know how to do it. Or, maybe there is a simpler approach to manipulate two images outside two API call functions.
Do you have any good idea?
--1st API call to fetch 1st image local json = require( "json" ) local function networkListener( event ) local res = json.prettify( event.response ) local decoded = json.decode( res ) if ( event.isError ) then print( "--Network error-- ", ( res ) ) else local edmPreview = decoded.items[lot20].edmPreview[1] -- Display image from web local function networkListener1image( event ) if ( event.isError ) then print ( "Network error - download failed" ) else event.target.alpha = 0 transition.to( event.target, { alpha = 1.0 } ) end print ( "event.response.fullPath: ", event.response.fullPath ) print ( "event.response.filename: ", event.response.filename ) print ( "event.response.baseDirectory: ", event.response.baseDirectory ) --Set an event to move to another scene when the image object is clicked local function onTap(event) event.target:removeSelf() composer.gotoScene("NewPageA",{time=800,effect="crossFade"}) end local myImage = event.target myImage:addEventListener("tap", onTap) end local loadedimage = display.loadRemoteImage(edmPreview, "GET", networkListener1image, "image1.png", system.TemporaryDirectory, 200, display.contentCenterY ) end end network.request("https://www.europeana.eu/api/v2/search.json?wskey="..apikey.europeana.."&query="..lotkeyword1.."&qf=proxy\_edm\_year:"..lotyear1.."&start="..lot20.."&reusability=open&media=true", "GET", networkListener, params) -- 2nd API call to fetch 2nd image local json2 = require( "json" ) local function networkListener2( event ) local res2 = json.prettify( event.response ) local decoded2 = json2.decode( res2 ) if ( event.isError ) then print( "--Network error-- ", ( res2 ) ) else local edmPreview2 = decoded2.items[lot20].edmPreview[1] -- Display image from web local function networkListener2image( event ) if ( event.isError ) then print ( "Network error - download failed" ) else event.target.alpha = 0 transition.to( event.target, { alpha = 1.0 } ) end print ( "event.response.fullPath: ", event.response.fullPath ) print ( "event.response.filename: ", event.response.filename ) print ( "event.response.baseDirectory: ", event.response.baseDirectory ) --Set an event to move to another scene when the image object is clicked local function onTap(event) event.target:removeSelf() composer.gotoScene("NewPageB",{time=800,effect="crossFade"}) end local myImage = event.target myImage:addEventListener("tap", onTap) end local loadedimage2 = display.loadRemoteImage(edmPreview2, "GET", networkListener2image, "image2.png", system.TemporaryDirectory, 900, display.contentCenterY ) end end network.request("https://www.europeana.eu/api/v2/search.json?wskey="..apikey.europeana.."&query="..lotkeyword2.."&qf=proxy\_edm\_year:"..lotyear2.."&start="..lot20.."&reusability=open&media=true", "GET", networkListener2, params)
The above code is of course inserted in the scene template below:
-- ----------------------------------------------------------------------------------- -- Scene event functions -- ----------------------------------------------------------------------------------- -- create() -- :create( event ) indicates that this function will be associated with the Composer create scene event and that a table of data that we reference with event will be passed to the function. function scene:create(event) --This creates a local reference to the scene's view group, automatically created by Composer, which should contain all of the display objects used in the scene. Essentially, any display object which should be part of the scene must be inserted into the scene's view group, and that group is referenced by sceneGroup inside each of the template's default scene: functions. local sceneGroup = self.view -- 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 -- Start the music loop=-1 means indefinete loop audio.play(menuMusic,{channel=1,loop=-1}) -- 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) elseif ( phase == "did" ) then -- Stop the music audio.stop(1) -- Code here runs immediately after the scene goes entirely off screen end end -- destroy() function scene:destroy( event ) local sceneGroup = self.view -- We effectively release the memory taken up by the audio file audio.dispose(menuMusic) -- 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