Replacing the variable's image

Hello

I’m currently working on something small, and I’d like to know if it’s possible to change the image of a variable.

Let’s say I have:

local image = display.newImage("images/image1.png", 100, 100)  

can I change that to image2.png?

Thanks.

You mean something like this?

[lua]

local image = display.newImage(“images/image1.png”, 100, 100)

–< Later on…

image = display.newImage(“images/image2.png”, 100, 100)

[/lua]

If so, the answer is yes, but you won’t have a reference to image1 any longer.  Which may not be needed as long as you don’t plan on manipulating it later in your code.

Well I do plan on using it afterwards. You see, it moves forward and backward, but when it needs to move backwards, it needs to face to the left. Using the rotation field and setting it to 180 won’t help, as it also flips the image. If you know another way on how to do please, please tell me :slight_smile:

Thanks anyhow though!

You will probably want to spend some time looking into sprites and sprite sheets.

http://www.coronalabs.com/blog/2012/10/02/animated-sprites-and-methods/

This is also an amazing tool for sprite sheets.

http://www.codeandweb.com/texturepacker/coronasdk

Hope that helps.

Cheers.

Well, I kind of did it my own way, by doing something like

x, y = image.x, image.y image = display.newImage("images/image2.png", 100, 100) image.x, image.y = x, y &nbsp;

Maybe not the best way to do it, or the most efficient way, but it works :slight_smile:

Whatever works.  :slight_smile:

Cheers.

You mean something like this?

[lua]

local image = display.newImage(“images/image1.png”, 100, 100)

–< Later on…

image = display.newImage(“images/image2.png”, 100, 100)

[/lua]

If so, the answer is yes, but you won’t have a reference to image1 any longer.  Which may not be needed as long as you don’t plan on manipulating it later in your code.

Well I do plan on using it afterwards. You see, it moves forward and backward, but when it needs to move backwards, it needs to face to the left. Using the rotation field and setting it to 180 won’t help, as it also flips the image. If you know another way on how to do please, please tell me :slight_smile:

Thanks anyhow though!

You will probably want to spend some time looking into sprites and sprite sheets.

http://www.coronalabs.com/blog/2012/10/02/animated-sprites-and-methods/

This is also an amazing tool for sprite sheets.

http://www.codeandweb.com/texturepacker/coronasdk

Hope that helps.

Cheers.

Well, I kind of did it my own way, by doing something like

x, y = image.x, image.y image = display.newImage("images/image2.png", 100, 100) image.x, image.y = x, y &nbsp;

Maybe not the best way to do it, or the most efficient way, but it works :slight_smile:

Whatever works.  :slight_smile:

Cheers.