How to change an image to a different image...?

This should be a simple question, but I haven’t been able to find an answer to it anywhere. How would I go about changing an image to a different image without destroying and recreating?

For example, if I have:

image1=display.newImage(“image1.png”,50,50)

How do I go about changing image1 to displaying “image2.png”.

Again, I don’t want to destroy and recreate or create two images and flip visibility. [import]uid: 8776 topic_id: 2095 reply_id: 302095[/import]

you can’t. you will need to have them both in memory…

local theImage;  
  
local image1 = display.newImage(image1...);  
image1.isVisible = false;  
local image2 = display.newImage(image2...);  
image2.isVisible = false;  
  
local function someFooFunctionToDisplayImage( displayImage, onOff )  
  
theImage = displayImage;  
theImage.isVisible = onOff;  
  
end  
someFooFunctionToDisplayImage(image1,false);  
someFooFunctionToDisplayImage(image2,true);  

** Please note, he above code has not been tested on simulator, just a pseudo code.

Carlos [import]uid: 24 topic_id: 2095 reply_id: 6251[/import]

I was thinking that might be the case when I didn’t find anything about it in the documentation or the forums. I guess I’m too used to developing in xcode and java where it would be as single line.

Not interested in flipping visibility settings. If I were to work with a grid of images that’d be a nightmare. I’ll have to play around and see if I can create a function that will destroy and recreate on top of the original image or something similar. Would hate to have to do it manually every time it needed to be done. [import]uid: 8776 topic_id: 2095 reply_id: 6255[/import]

What exactly are you trying to do?

You want to flip images on a grid? or stack them?

You can always set the image to nil and the garbage collector will destroy the image referenced. We don’t have image.destroy() or similar. So that chunk of memory will linger until the CG kicks in. Once the CG sees the image is not being referenced, it will destroy it.

If you are dealing with several images and on a loop for a game, you will want to avoid stacking up images to destroy so that when the CG kicks in, there is no discerning pause due to the CG doing its chore.

But you knew that since you are used to ref objects and java’s GC.

C. [import]uid: 24 topic_id: 2095 reply_id: 6256[/import]

Ultimately, yes, I want to be able to flip images on a grid.

It sounds like it is going to be a matter of destroying the original and recreating it with the new image file.

[import]uid: 8776 topic_id: 2095 reply_id: 6257[/import]

Wait, we implemented object.removeSelf() to get rid of an object. Doh!

You may want to see that API as well.

Thanks Tom for pointing that out to me.

C
[import]uid: 24 topic_id: 2095 reply_id: 6258[/import]

But if i have on this image registered event in eventListener etc. “tap” ?
Can I removeSelf() this image and load newImage() with same name ?

… if tap on this image then change this image

(sorry for my english…)
[import]uid: 14016 topic_id: 2095 reply_id: 25676[/import]

Assuming no transparency and just 2 images to swap:

Just create 2 images (exact same size/location), making them both visible and only toggle the visibility of the one on top for every tap of the lower image.

[import]uid: 167583 topic_id: 2095 reply_id: 119128[/import]