Changing an objects image during runtime

So here is a little issue I’ve run in to.

The idea was to touch the screen and the players image would change.
So to start out we have something like;

player = display.newImage("rock.png")  

But if we want to change the player’s image, there is really nothing in the API explaining how to do this easily.

What we are currently doing is having 3 player objects, and when we switch them, destroying one and creating a new one and passing all attributes (linear velocity, etc) over to the new object.

There must be an easier way to do this. Any tips?

The only thing that comes to mind is using a sprite sheet, and it has like a event.next command when you change the image? Could that work? To be honest, never used it before. [import]uid: 81846 topic_id: 16154 reply_id: 316154[/import]

You can set player.isVisible = true when you want to see it and set player.isVisible = false when you don’t want to see it.

Do the same for the other 2 players based on which one you want to be seen.

[import]uid: 67839 topic_id: 16154 reply_id: 60119[/import]

Yeah that was the first thing we tried, however the player physics objects continue to bump in to each other even when not visible. [import]uid: 81846 topic_id: 16154 reply_id: 60120[/import]

I think sprite sheets are definitely the best way to achieve this.

Haven’t played with them much yet myself, but pretty sure you can set which frame is displayed and wire that up to change on touch.

Sorry I don’t have any code for you.

I’ve a few things to iron out in the project I’m surrently working on before I start actually animating my hero, but will post some code in a few days if you haven’t solved it by then.

[import]uid: 67933 topic_id: 16154 reply_id: 60126[/import]

Perhaps you could use a movieclip for the image and simply change the movie clip frame.

See http://developer.anscamobile.com/content/movieclips

  
player = movieclip.newAnim{ "playerImage1.png", "playerImage2.png", "playerImage3.png" }  
  
...  
  
-- Change the players image to "playerImage2.png"  
  
player:stopAtFrame(2)  
  

You will need to require the movieclip module in your main.lua file. I cannot recall off the top of my head where to get this module but you should have no problem finding it. Note there is also a retina version.

[code]

– main.lua

local movieclip = require(“movieclip”)

[/code] [import]uid: 7863 topic_id: 16154 reply_id: 60128[/import]

If you want to use an Image, you have to remove it and load a new one, there is no easy way to *swap* objects/images

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16154 reply_id: 60175[/import]

Alright, well i’m working with another guy, so I had a go with sprite sheets and he used movieclips.

Movieclip was much easier, and had much better performance, so we stuck with using that.

Its now working infinitely better than deleting/remaking the player objects. The game runs much smoother, and it also resolved the scene change issues that we were having that started the need to change how it was done in the first place.

The forum posters once again are a huge credit to the success of Corona. Thanks all who contributed. [import]uid: 81846 topic_id: 16154 reply_id: 60313[/import]