So, to begin with, when you require libraries or other external files/modules, e.g.
local json = require("json")
This means that you can now call functions by referring to the “json” variable as in json.decode(), json.decodeFile(), json.encode() and json.prettify(). This means that creating another variable called “json2” simply creates a duplicate without providing any additional use. This is why I am not sure if there is ever a reason to require the same library/module more than once. You typically want to require all libraries like this (only once) at the beginning of your code file so that you can freely use them later on.
Now, with the functions.
local json = require( "json" ) -- just once is enough! -- local composer = require( "composer" ) -- guessing you have this already required elsewhere in the file? local myGroup = display.newGroup() -- sceneGroup:insert( myGroup ) --[[if you add myGroup to your sceneGroup, then it, as well as all display objects inserted into mygroup will be removed on composer.gotoScene( "someScene" ).]]-- 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 myGroup:insert( event.target ) -- the file was downloaded, so add it to myGroup 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) return myImage -- return myImage to the above function end local loadedimage = display.loadRemoteImage(edmPreview, "GET", networkListener1image, "image1.png", system.TemporaryDirectory, 200, display.contentCenterY ) return loadedImage -- return loadedImage to where the function was called end end local yourImage = 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) -- now you can manipulate yourImage outside of the function, but remember that since you are working with asynchronous functions so the image will take time to download
All you really need to do to return something from a function is to write “return [var]”, e.g. “return myImage” at the end of the function. If you return variables like this and have your network request set to some handle, then you can later access that variable through the handle.
I only skimmed through your code quickly, so there may be some mistakes. But, concerning your two functions, there seems to only be slight differences. What you can do is A ) pass some values to the function, e.g. “whatScene”, that you could set to “NewPageA” or “NewPageB” depending on what you need. This way, you can use the same function but just include an extra parameter. Alternatively, B ) you could have some variable that is declared before the function. So, basically the standard Lua function stuff, i.e.
-- a) local function printVarA( var ) print( var ) end printVar( "a" ) -- outputs: a printVar( "b" ) -- outputs: b -- b) local myVar local function printVarB() print( myVar ) end myvar = "a" printVar() -- outputs: a myvar = "b" printVar() -- outputs: b
Finally, if you use composer to move between scenes, then any display objects or display groups that have been inserted into the sceneGroup will automatically be removed upon scene change. If you don’t use (or don’t want to use) composer, then you can also just remove the display group that you’ve inserted the objects into and they will be removed.