How can I change the image by changing the path only?

Hi all,

I actually want to change a picture displayed on a fixed position (just like what card game did…they show different cards on the same position.)

currently I am using stupid method to do that. (create all images and alpha =0…when used alpha = 1)

Let say, I have a.png and b.png.

local img = display.newImage(“a.png”)

img.x, img.y = _W/2,_H/2

that display a.png on the screen at center, but what if now I want to change the pic to b.png?

can I set something like:

img.??? = “b.png”

just like what we use     text1.text =“xxxxx” in text field?

Thanks guys.

local img = display.newImage("a.png") img.x, img.y = \_W/2,\_H/2 img:removeSelf() img = display.newImage("b.png") img.x, img.y = \_W/2,\_H/2

try this way :slight_smile:

That is actually the way I currently doing.

However, this method have a problem…

what if I have 10 img?

currently I do this by using an index, a function and a module. and I am using mutliscreen so i have to group each new image into the group

I have a module called image.lua:

local m ={}

m.char = {

“1.png”,
“2.png”,

“10.png”

}

return m


and in the imagedisplay lua:

img_sheet = require(“image”)

imgindex = 1

local img=display.newImage(img_sheet.char[imgindex])

img.x, img.y = _W/2, _H/2

group:insert(img)

function changeimg ()

img: removeSelf()

img = display.newImage(img_sheet.char[imgindex])

img.x, img.y = _W/2, _H/2

group:insert(img)

end


so everytime I want to change an image, I do this:

imgindex = 2,
changeimg()

imgindex=3,
changeimg()

…etc.

is this the best practice to do it??
I just feel uncomfortable .-. is defining a completely new image with the same name the correct way to do this?

Thanks :smiley:

I think in sdk dont have such function to change object image with path.

So the way you are use is correct if work.

Ok thanks~it is ok to let me know I am correct :smiley:

Actually this is a good use for sprite/image sheets.  You can have an image sheet with your 52 cards and then as a sprite, you can play an individual frame.

See:  http://www.coronalabs.com/blog/2013/05/14/sprites-and-image-sheets-for-beginners/

local img = display.newImage("a.png") img.x, img.y = \_W/2,\_H/2 img:removeSelf() img = display.newImage("b.png") img.x, img.y = \_W/2,\_H/2

try this way :slight_smile:

That is actually the way I currently doing.

However, this method have a problem…

what if I have 10 img?

currently I do this by using an index, a function and a module. and I am using mutliscreen so i have to group each new image into the group

I have a module called image.lua:

local m ={}

m.char = {

“1.png”,
“2.png”,

“10.png”

}

return m


and in the imagedisplay lua:

img_sheet = require(“image”)

imgindex = 1

local img=display.newImage(img_sheet.char[imgindex])

img.x, img.y = _W/2, _H/2

group:insert(img)

function changeimg ()

img: removeSelf()

img = display.newImage(img_sheet.char[imgindex])

img.x, img.y = _W/2, _H/2

group:insert(img)

end


so everytime I want to change an image, I do this:

imgindex = 2,
changeimg()

imgindex=3,
changeimg()

…etc.

is this the best practice to do it??
I just feel uncomfortable .-. is defining a completely new image with the same name the correct way to do this?

Thanks :smiley:

I think in sdk dont have such function to change object image with path.

So the way you are use is correct if work.

Ok thanks~it is ok to let me know I am correct :smiley:

Actually this is a good use for sprite/image sheets.  You can have an image sheet with your 52 cards and then as a sprite, you can play an individual frame.

See:  http://www.coronalabs.com/blog/2013/05/14/sprites-and-image-sheets-for-beginners/