Switching images

Hi,

I have an image (created using newImageRect) that’s part of a group. Based on an event I want to change that image to a different file (that’s the same size). Is it possible to do this without removing the original object and creating a new, reinserting it into the group?

Thanks,
Nathan. [import]uid: 27228 topic_id: 35376 reply_id: 335376[/import]

I would create a 2nd image, then insert to group. Then position it exactly where the 1st one is.
This 2nd one, as it is inserted after the 1st one, will cover over the 1st one. The 1st one is still there (in the group), just covered by this second one.

If your second image has some transparent areas, the 1st image will show through from under the 2nd one, just in those transparent spots; if that is what you are trying to do.

If you want the 1st image just to be in place under this 2nd image for some reason, and your 2nd image has transparent areas and you don’t want any of the 1st image to show, you should make the 1st image invisible, once you have inserted the 2nd image. So then you have both images in the group, in the same physical location.

I am guessing you are going to flip back and forth showing the images, or maybe you are going to slowly fade out the top image revealing the 1st again, or some other plan; but in any case you can just control the visibility of these 2 images set to true or false.

Hope I understood your question correctly and this is at least somewhat helpful.

[import]uid: 148857 topic_id: 35376 reply_id: 140629[/import]

Try not to create new objects at runtime - I believe the optimal way to do this is to use an imageGroup and refer to whatever image in that group you need.

Somebody shoot me down if I’m wrong. [import]uid: 33275 topic_id: 35376 reply_id: 140632[/import]

Hi @beernathan,

If you just want to swap an image, and not worry about re-inserting a new or existing image into the group, positioning it, etc., you could set up the image as a sprite with 2 frames. This really isn’t a slap on performance anymore, since sprites are now integrated into the display library like all images (in the old days, you had to require the “sprite” library and such, but no longer). Then, once you have your sprite set up, you can change the image by just setting its frame to a value of either 1 or 2.

As a side note, @SegaBoy is 100% correct that you shouldn’t create new images in Runtime, unless they’re really tiny in file size (like 2 KB or something). Preloading images into texture memory is almost always the better method, and you can hide them off screen or just make them invisible until you need to use them.

Brent [import]uid: 200026 topic_id: 35376 reply_id: 140645[/import]

OK thanks everyone - looks like I need to learn about sprites :wink: [import]uid: 27228 topic_id: 35376 reply_id: 140680[/import]

I would create a 2nd image, then insert to group. Then position it exactly where the 1st one is.
This 2nd one, as it is inserted after the 1st one, will cover over the 1st one. The 1st one is still there (in the group), just covered by this second one.

If your second image has some transparent areas, the 1st image will show through from under the 2nd one, just in those transparent spots; if that is what you are trying to do.

If you want the 1st image just to be in place under this 2nd image for some reason, and your 2nd image has transparent areas and you don’t want any of the 1st image to show, you should make the 1st image invisible, once you have inserted the 2nd image. So then you have both images in the group, in the same physical location.

I am guessing you are going to flip back and forth showing the images, or maybe you are going to slowly fade out the top image revealing the 1st again, or some other plan; but in any case you can just control the visibility of these 2 images set to true or false.

Hope I understood your question correctly and this is at least somewhat helpful.

[import]uid: 148857 topic_id: 35376 reply_id: 140629[/import]

Try not to create new objects at runtime - I believe the optimal way to do this is to use an imageGroup and refer to whatever image in that group you need.

Somebody shoot me down if I’m wrong. [import]uid: 33275 topic_id: 35376 reply_id: 140632[/import]

Hi @beernathan,

If you just want to swap an image, and not worry about re-inserting a new or existing image into the group, positioning it, etc., you could set up the image as a sprite with 2 frames. This really isn’t a slap on performance anymore, since sprites are now integrated into the display library like all images (in the old days, you had to require the “sprite” library and such, but no longer). Then, once you have your sprite set up, you can change the image by just setting its frame to a value of either 1 or 2.

As a side note, @SegaBoy is 100% correct that you shouldn’t create new images in Runtime, unless they’re really tiny in file size (like 2 KB or something). Preloading images into texture memory is almost always the better method, and you can hide them off screen or just make them invisible until you need to use them.

Brent [import]uid: 200026 topic_id: 35376 reply_id: 140645[/import]

OK thanks everyone - looks like I need to learn about sprites :wink: [import]uid: 27228 topic_id: 35376 reply_id: 140680[/import]