How do I set object.isVisible to a display.loadRemoteImage?

display.loadRemoteImage( img, “GET”, networkListener, img2, system.TemporaryDirectory, 500, 270)

I’m displaying this URL image when the event phase equals submitted in my function URLtextListener( event ). It loads the image correct but how do I control if I want the image visible or not.

The object.isVisible = false doesn’t seem to be working.
I have it set up like this.

object = display.loadRemoteImage( img, “GET”, networkListener, img2, system.TemporaryDirectory, 500, 270)
object.isVisible = false

display.loadRemoteImage() is an asynchronous function. You need to wait until the image has been downloaded to do anything to it.

See: https://docs.coronalabs.com/api/library/display/loadRemoteImage.html#standard

Since you haven’t included the code for what img or img2 are, or what your networkListener does, the issue is most likely there. The sample in the documentation should help you out.

Edit: Just in case it wasn’t clear, you can’t have object.isVisible = false right below the display.loadRemoteImage(). You need to do that within the listener.

1 Like

@joshjarc,

All your questions on this topic have thus far been the same format.

“How do I modify attribute XYZ for a loadRemoteImage?”

All the answers are the same.

You must do it in the listener. i.e. Wait for the image to be downloaded and drawn, then get the handle to the display object (https://docs.coronalabs.com/api/library/display/index.html) in your listener and modify it.

You cannot pre-modify, pre-measure, pre-anything an asynchronously downloaded and displayed image.

Please re-read the docs: https://docs.coronalabs.com/api/library/display/loadRemoteImage.html#standard

Be aware, calls to display.loadRemoteImage() do not return a value. There is no handle returned to operate on, so you can’t modify it yet.

Also, be aware, all code after the call executes immediately, but the image has not been downloaded or drawn yet. That can take an arbitrary amount of time, so you can’t rely on the image being present yet in the code following a call to display.loadRemoteImage().

If you need a remote image that can immediately be used to draw a display object and immediately be modified, you need to have previously downloaded the image file earlier in your app/game. You can use this for downloading files: https://docs.coronalabs.com/api/library/network/download.html

1 Like

@joshjarc

I hope I didn’t seem like I was yelling at you above. I know that folks new to game development and or programming may be used to linear execution of their scripts and programs, but unaware of asynchronous operations.

Generally and usually, lines of code execute synchronously (in order): 1, 2, 3, 4, …
Typically, you can rely on step 1 completing before step 2, step 2 before step 3, and so on.

However, that is not always true. Point in case, the function you are calling display.loadRemoteImage() is not a synchronous feature.

It is asynchronous. https://www.merriam-webster.com/dictionary/asynchronous

Additionally, it is non-blocking. (More on both topics here: https://stackoverflow.com/questions/7931537/whats-the-difference-between-asynchronous-non-blocking-event-base-architectu)

In a nutshell, display.loadRemoteImage(), executes immediately (non-blocking), but the work done as a result will happen some time in the future (asynchronously).

As you get more familiar with game dev in particular, and if you choose to make games with any of these features:

  • multiplayer
  • ads
  • networking
  • Game Services (Google Play, iOS, Steamworks)
  • etc.

you will encounter more and more asynchronous operations. This is something you have to fully grok to handle properly and hide from your users in some cases.

Anyways, best of luck to you on your game dev journey.

PS - As a bonus, be aware, if you are not already, that Corona/Solar2D is event-based. Thus, the listener to handle the asynchronous call above.

1 Like

Not at all, I really appreciate the explanations and links.