How can i display it as a single Image?

Hi, i don’t know what to put or how to create the question I’m about to ask in the “Topic Title” i apologize for that.

So i have this declaration in one of my lua file named  “myData”

[lua]

local M = {}

M.lives = 5

[/lua]

And I’m calling it from another lua file to display from the screen by using this code:

[lua]

local life = display.newText(“Lives x” …myData.lives,0,0,Arial,18)

life:setTextColor(50)

life.x = 75;life.y = 150

[/lua]

I want it to become an image so I’ve tried doing something like:

[lua]

local life = display.newImageRect(“Images/Lives.png” …myData.lives,50,50)

[/lua]

But it will create an error because it will look for an image file which is named “Lives.png5” when i do this.

What code should i use for it to display 5 images of Hearts(example). or 6 Images of Hearts when i changed the value to “M.lives = 6”

Sorry for the title i really don’t know what/how to construct the question for this.

This tutorial would be a good starting point.  Now I’ve been lazy in other games and I make an array of 5 hearts:

local healthImages = {}

for i = 1, 5 do

     healthImages[i] = display.newImageRect(“Images/Lives.png”, 50, 50)

     healthImages[i].x = xOffset + (50 * i)

     healthImages[i].y = yOffset

     healthImages[i].isVisible = true

end

Then you simply hide the images as you lose a life.  Get one back unhide it.

Rob

Thank you very much for replying and giving me codes. 

I’ve tried using arrays and the code you gave me its really good.

But is there any other way that i don’t need to do arrays like this and instead just call  the number of lives from another lua file and display them as an image in another lua file?

I want to use that kind of way so i can also use it from another lua file.

Example:

1stLuaFile have the number of lives

[lua]

Lives = 10

[/lua]

2ndLuaFile will call the 1stLuaFile lives and display the 10 lives as an image

then i want to call them again in

3rdLuafile call and display  another 10 lives as an image

Something like that.

Thank you again for replying

That is what the tutorial I pointed you to is for.  You have an imageSheet with your 5 images in one, define it as a sprite then you can play each sprite frame based on the value of Lives.

Rob

Rob, I think you forgot to post the “tutorial” link.

Good catch!

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

Rob

Alright, Thanks Sir Rob and Sir James. :smiley:

This tutorial would be a good starting point.  Now I’ve been lazy in other games and I make an array of 5 hearts:

local healthImages = {}

for i = 1, 5 do

     healthImages[i] = display.newImageRect(“Images/Lives.png”, 50, 50)

     healthImages[i].x = xOffset + (50 * i)

     healthImages[i].y = yOffset

     healthImages[i].isVisible = true

end

Then you simply hide the images as you lose a life.  Get one back unhide it.

Rob

Thank you very much for replying and giving me codes. 

I’ve tried using arrays and the code you gave me its really good.

But is there any other way that i don’t need to do arrays like this and instead just call  the number of lives from another lua file and display them as an image in another lua file?

I want to use that kind of way so i can also use it from another lua file.

Example:

1stLuaFile have the number of lives

[lua]

Lives = 10

[/lua]

2ndLuaFile will call the 1stLuaFile lives and display the 10 lives as an image

then i want to call them again in

3rdLuafile call and display  another 10 lives as an image

Something like that.

Thank you again for replying

That is what the tutorial I pointed you to is for.  You have an imageSheet with your 5 images in one, define it as a sprite then you can play each sprite frame based on the value of Lives.

Rob

Rob, I think you forgot to post the “tutorial” link.

Good catch!

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

Rob

Alright, Thanks Sir Rob and Sir James. :smiley: