Best way to change images on touch?

umm, that didn’t work either, still get same error … what version are you on Brent?

 local options = {   
 frames = {   
 { x=0, y=0, width=40, height=34},  
 { x=0, y=35, width=40, height=34}   
 },   
 sheetContentWidth=40,  
 sheetContentHeight=68   
 }  
  
 local chest\_imageSheet = graphics.newImageSheet( "media/images/chest\_imageSheet.png", options )  
 -- oldChest = display.newImageRect(scene.view, chest\_imageSheet, 1, 40, 34 )  
 oldChest = display.newImageRect(chest\_imageSheet, 1, 40, 34 )  
 oldChest:setReferencePoint( display.TopLeftReferencePoint )  
 oldChest.x, oldChest.y = display.contentWidth \* 0.55, display.contentHeight \* 0.75  
 oldChest:setFrame(2) -- TODO: testing - delete afterwards  

[import]uid: 140210 topic_id: 18670 reply_id: 106467[/import]

I feel like an idiot. :slight_smile: You still need to define the image as a “sprite”, just not in the same method as the old sprite API. Then you should be able to change its frames.

Scroll down and read about sprites in the new imageSheet system here:

http://blog.anscamobile.com/2012/03/image-sheets-image-groups-and-sprites/

I think this should do the trick!
Brent
[import]uid: 9747 topic_id: 18670 reply_id: 106468[/import]

no worries :slight_smile:

I’ll look at this - the tutorial however does give you the impression that image sheets would be good for this purpose, in that it doesn’t allocate the overhead required for animation…

[import]uid: 140210 topic_id: 18670 reply_id: 106470[/import]

all good - this seems to work fine:

[code]
local options = {
width = 40,height = 34, numFrames = 2,
sheetContentWidth = 40,sheetContentHeight = 68 }
local chest_imageSheet = graphics.newImageSheet( “media/images/chest_imageSheet.png”, options )
oldChest = display.newSprite( chest_imageSheet, {name=“manual”,start=1, count=2} )
scene.view:insert(oldChest)
oldChest.x, oldChest.y = display.contentWidth * 0.55, display.contentHeight * 0.75

oldChest:setFrame(2)
[/code] [import]uid: 140210 topic_id: 18670 reply_id: 106474[/import]

Great to hear! And you’re right… despite the fact that you must make the image a “sprite” to swap its frames, you don’t need to include (require) the big sprite module like in the past. ImageSheets are a core element of Corona now, and they’re far more efficient than previous graphical methods, both static and animated. We should all be using them, and the sooner the better. :slight_smile:

Brent Sorrentino
Ignis Design
[import]uid: 9747 topic_id: 18670 reply_id: 106476[/import]

In my case I am using several large “background” images that are 1024x768. I was thinking of creating an object for each and then setting them to .visible = true or false when needed. While the non-used ones are set to false, will they still take up processing or memory? Just wondering if this is a bad way to handle it? [import]uid: 19620 topic_id: 18670 reply_id: 110371[/import]

It’s not necessarily a “bad” way to do it, but even images set to invisible take up *texture* memory, the threshold of which varies by device (and if the threshold is crossed, the app will likely crash).

Considering you’re talking about full-screen images at iPad 1&2 resolution, they probably are fairly big in file size. What I would recommend, if possible, is to have just TWO of these images loaded into memory at a given time… the visible one and the “next” one. If the app behaves like a book, for example, when you move to the next page, you delete the page that is visible, make the previously hidden image visible, then load (behind it) the next image that you plan to show.

Not sure what kind of app you’re building, however, so maybe this isn’t the best approach. The point is, making images invisible doesn’t free up the texture memory they occupy.

Also, note that you should still save these images as .PNG files, not JPEG. The temptation for full-screen images, especially if they’re “painted” in style, is to use JPEG. However iOS has a built-in PNG compression algorithm which actually makes PNG files more efficient than JPEG, and faster loading. At least those are the reports that I’ve read from various sources. :slight_smile:

Brent Sorrentino [import]uid: 9747 topic_id: 18670 reply_id: 110380[/import]

Great, thanks for the response brent. What I am creating is some what of a “Scene Creator”. Meaning the user can choose a background image, and then they can choose “stickers” to place in the scene, rotate them, size them, etc. So right now I’m trying to figure out changing the background image on the fly. Ill see what I can figure out. Good to know about the PNG thing and that non visible is still held in texture memory. [import]uid: 19620 topic_id: 18670 reply_id: 110383[/import]

Sounds good! As long as you don’t exceed (or come close to exceeding) the texture memory, you can absolutely load all of your backgrounds into memory so the user can quickly choose from among them. But then perhaps, when they choose the background they desire, you can remove all of the others so there is more free memory for the “stickers” and other things.

Brent Sorrentino
[import]uid: 9747 topic_id: 18670 reply_id: 110518[/import]

I want to add timer for this function. How can I do? Please give me any advice…

[import]uid: 147090 topic_id: 18670 reply_id: 116463[/import]

When the image change to img1 to img2, I want to add timer between them. Please give me advice.

[import]uid: 147090 topic_id: 18670 reply_id: 116464[/import]

Hi Namdeoo,
Any function can be triggered by a timer. In your case, you would just perform the “swap” after the timer executes. Search the docs for “timer.performWithDelay” to see the specific usage. It’s very, very easy. :slight_smile:

Brent [import]uid: 9747 topic_id: 18670 reply_id: 116468[/import]