How to change image?

Hello, I’m new to using Corona and the Lua programming language. I have much experience using JavaScript and PHP so I already understand the basic concepts of programming already.

Here, I create an image.
example = display.newImage(“example.png”);

Is there a line of code that would change the image to “example2.png”?

Something like this (this is not the actual code):

example:changeImage(“example2.png”);

Is there a function that would let me do something like that?

I do not want to use sprite sheets or anything like that.

I have already thought of a solution which is to remove the image or change it’s X/Y coordinates out of the screen and basically swap coordinates with another image. I just don’t want to do something this complicated for something so simple.

Thank you!

What I do is load both images at the same time at the same coordinates, and set the one you don’t want to see invisible via myImage.isVisible = false.   Then in the event code I swap the isVisible properties respectively. 

Thanks, this is a great idea, it’s what I had in mind but I didn’t know there was a “isVisible” function, thanks! I wish there was a proper function that changed image resources though. I guess this will do though! :slight_smile:

You can also use a sprite sheet. It depends a little, I would say, on how many images you are going to swap. If it’s only one I would just do a transition but if there is a lot of images it might be more efficient with a sprite sheet, try it out and see if it works well for you:

http://www.coronalabs.com/blog/2013/05/14/sprites-and-image-sheets-for-beginners/

Especially look under the “Image Swapping” section.

Best regards,

Tomas

What I do is load both images at the same time at the same coordinates, and set the one you don’t want to see invisible via myImage.isVisible = false.   Then in the event code I swap the isVisible properties respectively. 

Thanks, this is a great idea, it’s what I had in mind but I didn’t know there was a “isVisible” function, thanks! I wish there was a proper function that changed image resources though. I guess this will do though! :slight_smile:

You can also use a sprite sheet. It depends a little, I would say, on how many images you are going to swap. If it’s only one I would just do a transition but if there is a lot of images it might be more efficient with a sprite sheet, try it out and see if it works well for you:

http://www.coronalabs.com/blog/2013/05/14/sprites-and-image-sheets-for-beginners/

Especially look under the “Image Swapping” section.

Best regards,

Tomas