Basics of the basic - #1

I have about 4 or 5 really basic things I am trying to do and forgive me if this is obvious. Below is the application I have. The networkListener gets called and I get a runtime error in the second print line or anytime I try to access the image variable. I get the error of attempt to index upvalue ‘img’ (a nil value).

Once it returns in the networkListener, shouldn’t the img variable be initialized and have values?

--hide the statusbar  
display.setStatusBar( display.HiddenStatusBar )  
  
local img = nil  
local dotimage = nil  
  
local function networkListener( event )  
 -- dotimage =   
 print("HELLO")  
 print(img.contenWidth)  
  
 img:setDrag ( {drag=true})  
 img.xScale = .4  
 img.yScale = .4  
  
 if ( event.isError ) then  
 print ( "Network error - download failed" )  
 else  
 event.target.alpha = 0  
 transition.to( event.target, { alpha = 1.0 } )  
  
 end  
  
 print ( "RESPONSE: " .. event.response )  
end  
  
local function main()  
 --add the image  
 local params = {}  
 params.headers = {} -- contains header values to send with your request  
 params.body = ""  
 img = display.loadRemoteImage("http://127.0.0.1:88/images/Map5000.png","GET", networkListener, "helloCopy.png", system.TemporaryDirectory, -200, -200);  
  
end  

main() [import]uid: 6286 topic_id: 32382 reply_id: 332382[/import]

img is still nil at the moment.

Your downloaded image is in event.target, so if you add a line like this:

 print("HELLO")  
  
 img = event.target  
  
 print(img.contenWidth)  

And you should start working. [import]uid: 19626 topic_id: 32382 reply_id: 128831[/import]

Use event.target instead of img in your listener.

Something like this:

local function networkListener( event )  
  
local obj = event.target --correction  
  
 -- dotimage =   
 print("HELLO")  
 print(obj.contenWidth)  
  
obj:setDrag ( {drag=true})  
obj.xScale = .4  
obj.yScale = .4  
  
 if ( event.isError ) then  
 print ( "Network error - download failed" )  
 else  
 event.target.alpha = 0  
 transition.to( event.target, { alpha = 1.0 } )  
  
 end  
   
 print ( "RESPONSE: " .. event.response )  
end  

RoosterBee [import]uid: 177091 topic_id: 32382 reply_id: 128827[/import]

img is still nil at the moment.

Your downloaded image is in event.target, so if you add a line like this:

 print("HELLO")  
  
 img = event.target  
  
 print(img.contenWidth)  

And you should start working. [import]uid: 19626 topic_id: 32382 reply_id: 128831[/import]

Use event.target instead of img in your listener.

Something like this:

local function networkListener( event )  
  
local obj = event.target --correction  
  
 -- dotimage =   
 print("HELLO")  
 print(obj.contenWidth)  
  
obj:setDrag ( {drag=true})  
obj.xScale = .4  
obj.yScale = .4  
  
 if ( event.isError ) then  
 print ( "Network error - download failed" )  
 else  
 event.target.alpha = 0  
 transition.to( event.target, { alpha = 1.0 } )  
  
 end  
   
 print ( "RESPONSE: " .. event.response )  
end  

RoosterBee [import]uid: 177091 topic_id: 32382 reply_id: 128827[/import]