joining random image with explanation text

Hello folks, let´s see if someone can give me some light with a doubt…

I have four cards: paper, rock, scissors and atomic bomb… 

I placed all in a list and I´m picking up the objects randomically when the user clicks a button…

All is working perfect.

Now my doubt is… in case i want to place a text above the card with the object name, ie:(Rock or paper or scissors or atomic bomb) how to proceed? 

I know the “piggy” solution for that would be to verify through “if then else” conditional, the number of the index in the cards list and return a string with the name. It can works really well for 4 cards, but it will be terrible if i have a deck with about 40 cards and their names.

How to join the random card and display it name on screen in case i have a big deck of cards?

Thanks!

Add your text as a property of your objects.  i.e object.text = “Scissors”

Do you mean on my list?

Like:

objlist={“img1.jpg”,img1.text=“scissors”, “img2.jpg”,img2.text=“rocks”…} and so on?

More like

cards = { {image="img1.jpg",name="scissors"}, {image="img2.jpg",name="rocks"} }

Just to add to the above, you’d access an entry like this:

[lua]

local thisCard = cards[1]

print (thisCard.image)   – img1.jpg

print (thisCard.name)   – scissors

[/lua]

The suggested structure means you can loop through, sort, or shuffle the deck of cards really easily and have access to all the properties. You could even assign the visual aspect of the card to the same table:

[lua]

local g = display.newGroup()

g:insert(cardImage)  – assuming you’ve created this already…

g:insert(cardText)  – assuming you’ve created this already…

g.image = cardImage  – assign the image as a property of the card group, for easy access

g.text = cardText   – assign the text object as a property of the card group

thisCard.cardGroup = g

— LATER ON

thisCard.cardGroup.image:setFillColor(1,0,0)   – make the card image have a red hue

thisCard.cardGroup.text.text = “DEAD!”  – update the card text

thisCard.cardGroup.alpha = 0  – hide the entire card

[/lua]

Add your text as a property of your objects.  i.e object.text = “Scissors”

Do you mean on my list?

Like:

objlist={“img1.jpg”,img1.text=“scissors”, “img2.jpg”,img2.text=“rocks”…} and so on?

More like

cards = { {image="img1.jpg",name="scissors"}, {image="img2.jpg",name="rocks"} }

Just to add to the above, you’d access an entry like this:

[lua]

local thisCard = cards[1]

print (thisCard.image)   – img1.jpg

print (thisCard.name)   – scissors

[/lua]

The suggested structure means you can loop through, sort, or shuffle the deck of cards really easily and have access to all the properties. You could even assign the visual aspect of the card to the same table:

[lua]

local g = display.newGroup()

g:insert(cardImage)  – assuming you’ve created this already…

g:insert(cardText)  – assuming you’ve created this already…

g.image = cardImage  – assign the image as a property of the card group, for easy access

g.text = cardText   – assign the text object as a property of the card group

thisCard.cardGroup = g

— LATER ON

thisCard.cardGroup.image:setFillColor(1,0,0)   – make the card image have a red hue

thisCard.cardGroup.text.text = “DEAD!”  – update the card text

thisCard.cardGroup.alpha = 0  – hide the entire card

[/lua]