Select and display a number of images from array list

I am quite new in LUA therefore I looking for someone who would give me a hint what will be the best way to start and how it would be achievable. Also I am trying to develop a game in which a number of images are displayed and together they make a word - the letters cannot be displayed randomly but the whole word  which consist of the images .. At the moment I know how to display single key value but I don’t know how to shuffle data and display a number of  images on the screen .

This is my example and it is not working as it should, however I hope it will disclose my general idea.

local puzzles = {}

local puzzleImages = {}

puzzleImages[1] = {“blockY.png”, “blockE.png”, “blockS.png”} – word YES

puzzleImages[2] = {“blockN.png”, “blockO.png”} – word NO

for i = 1, #puzzleImages do

puzzles[i] = display.newImage(puzzleImages[#puzzleImages][i])

puzzles[i].x = math.random( 115, display.contentWidth - 115 )

puzzles[i].y = math.random( 115, display.contentHeight - 115 )

end

Any idea , suggestion?

You may want to read up on Rob’s tutorial for shuffling table items here. He presents the concept with a deck of cards, but your idea is similar (alphabets instead of cards).

Hmm, that is only similar idea, I am looking for how it would be achivable to shuffle whole index of the array or select random array function. Thanks anyway.

Guys, any other ideas?

Hi @piotre80,

One way of doing it, if i understand you correctly, is:

a. Place all your puzzle word in a table or text file.

b. Retrieve a random word(string) from the table of words. –  e.g. “YES”

c. From the word read individual letter from the word and put in array.–wordArray={}  will become wordArray={“Y”,“E”,“S”} 

d. Create your display object (image files) from the individual letter. Assuming you name your image files as blockA.png to blockZ.png

eg.

local wordSpace = 20 – space between letters

local wordXPosition = 50

for i = 1, #wordArray do
puzzleWord[i] = display.newImageRect(“block”…wordArray[i]…".png")
puzzleWord[i].x = wordXPosition + (i * wordSpace)
puzzleWord[i].y = …and so on
end

e. get your next random word.

This is the method I use for my word game.

Good Luck!

burhan

Hi Burhan,

Thank you verym much for you fast response :slight_smile: . You have read my post from the begining to the end so thanks once again.

  I didn’t know that it is possible to to this in …this way and yes because this will an application for children I would like to display one single word a time. I would like to store this words using SQLite but at the moment  I am concentraiting how to display random words from the array.

Yesterday I had some time off and I tried to run this code like this:

local puzzleWord = {}
  local wordArray = {}
 
  wordArray= {“blockY.png”, “blockE.png”, “blockS.png”}
  wordArray= {“blockN.png”, “blockO.png”}
   
local wordSpace = 100 – space between letters
local wordXPosition = 10
for i = 1, #wordArray do
puzzleWord[i] = display.newImageRect(“block”…wordArray[i]…".png") — This one
puzzleWord[i].x = wordXPosition + (i * wordSpace)
puzzleWord[i].y  = wordXPosition + (i * wordSpace)
end
 

…and Unfortunately, it is not working ( attempt to index field? nil value) although, when I remove ("“block”, “.png”) from the line abve the images has been displayed. Probably now I need numbers or names to index words in the array and to let application “know” what it need to be displayed.

Yes, you are right. 

Anyway, the reason i put the “block”…wordArray[i]…".png" is because i retrieve my text files as whole word.

So my word in the text files will be {“YES”, “NO”, …and so on }. 

I then split my string “YES” and put in wordArray and it becomes wordArray[1] = “Y” , wordArray[2] = “E”, wordArray[3] = “S”.

In your case, your word if you save in text files or sql database will be  {“blockY.png”, “blockE.png”, “blockS.png”},  {“blockN.png”, “blockO.png”}, and so on.

Just imagine if you have hundreds or thousands of words you will then have to type a lot of text and also in my opinion difficult to read.

As for numbers to index the words can you retrieve by row number in your database?

Good Luck!

burhan

Hi Burhan ,

Excelent post my friend! I am very close to achieve the goal,  the only thing is that I must use sqlite to retrive random words , using txt file dislays a whole lenght of array with letters . :wink:

Regards

Piotr

Sorry guys, I am still having problem to retrieve these images and print them out from the array. I was working on two possible soutions using SQLite and JSON but none of them has resolved my problem yet. I got manage how to store and display images using SQLite but I found difficulties with printing random index from the array.

local words = {}
for row in db:nrows("SELECT * FROM test7  ") do
  local t = display.newImage(row. sL, fL, 50, 90*row.id, nil, 26 ) – Having two separate columns “sL” and “fL” unable to print two images at once

Here is another assumption that this can be managable using JSON format however I don’t know yet how to display a set of images and in the same time keep my files tidy and simple. Here imy question is, how I can store group of letters like in this example and display a whole index randomly.

local words = {
                “abc”: [ –
                {
                  “blockC” = “blockA.png”, –  display  this
                  “blockA” = “blockC.png”, - this
                  “blockB” = “blockB.png” –  and this
                }

                 “GMC”: [ – or
                {
                  “blockG” = “blockG.png”,  – this
                  “blockM” = “blockM.png”, – this
                  “blockC” = “blockC.png” – and this
                }
                
             
             ]}

Please help me get out of here !Thanks in advance… good people  ;)             
 

Hi,

for SQLite, maybe you can try placing in a table/array first, then display the image. 

local words = {} local puzzleWord = {} for row in db:nrows("SELECT \* FROM test7") do words = {row.content1, row.content2, row.content3} -- {"blockA","blockB", "blockC"} end -- proceed as I mentioned earlier. local wordSpace = 20 -- space between letters local wordXPosition = 50 for i = 1, #words do puzzleWord[i] = display.newImage(words[i]..".png") puzzleWord[i].x = wordXPosition + (i \* wordSpace) puzzleWord[i].y = 200 end

Good Luck!

burhan

Hah…I don’t wan’t to be too personal but you safe me a lot of tortures. Now I can display even whole dictionary! Is it possible to add sound for each letter in this way?

Some ways I can think. 

You can name your sound files the same as image files, of course with different extensions.

This is just an idea ( not tested) since i do not know how you want to call your sound . You also have to check for memory issues when choosing which way to do this.

for i = 1, #words do puzzleWord[i] = display.newImage(words[i]..".png") puzzleWord[i].x = wordXPosition + (i \* wordSpace) puzzleWord[i].y = 200 puzzleWord[i].letterSound = audio.loadSound( words[i]..".wav" ) end ... -- then later call sound audio.play( puzzleWord[i].letterSound)

or, you can populate all your letters sound first

local blockA = audio.loadSound( "blockA.wav" ) -- assume you name them block??.wav local blockB = audio.loadSound( "blockB.wav" ) ... -- then later call your audio you want to play audio.play( word[1] ) -- which is "blockA" from earlier example

Good Luck!

burhan

Thanks Burhan, I went for your first solution because it is seems to be more sensible approach, however I am trying to fit this code and I am still getting funny errors. Basically, I can add leters and display them randomly without any trouble  but I don’t know how to assign and play an audio files when the particular randomly selected letter has been tapped by the user. I hope it is clear what I am trying to achieve and here is my code snippet that I am working on.

-- Create table local tablesetup = [[CREATE TABLE IF NOT EXISTS test8 (id INTEGER PRIMARY KEY autoincrement, fL, fA);]]   db:exec( tablesetup ) local words = { { fL = "blockA", fA = "blockA" } } -- Insert data into the table for i = 1, #words do local q = [[INSERT INTO test8 VALUES (NULL, ']].. words[i].fL .. [[',']] .. words[i].fA ..[[');]] db:exec(q) end local words = {} local puzzleWord = {} --Select data from table for row in db:nrows("SELECT \* FROM test8 ") do     words = {row.fL, row.fA} -- {"blockA","blockB"} end -- Display letters and play audio for randomly displayed letter local wordSpace = 250 -- space between letters local wordXPosition = 150 for i = 1, #words do  puzzleWord[i] = display.newImage(words[i]..".png") puzzleWord[i].x = wordXPosition + (i \* wordSpace) puzzleWord[i].y = 200  puzzleWord[i] = audio.loadSound( words[i]..".wav" )                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               end local playSound = function(event) audio.play(puzzleWord[i].letterSound) end  puzzleWord: addEventListener("tap", puzzleWord)  puzzleWord: addEventListener("tap", playSound)

Is it possible to make this code running this way where each image and audio file have the same names and different extensions or it would be better off  to do this differently ?

Regards

Piotr

Hi Piotr,

You have to assign tap eventListener to each image too.

Then play the sound when user tap the letter.

Try this,

local puzzleTouch = function (event) local target = event.target audio.play(target.letterSound) -- plays the sound -- Note: you can add more things here when user click the letter like for e.g animate, reduce transparency or resize the image end -- Display letters and play audio for randomly displayed letter local wordSpace = 250 -- space between letters local wordXPosition = 100 for i = 1, #words do puzzleWord[i] = display.newImage(words[i]..".png") puzzleWord[i].x = wordXPosition + (i \* wordSpace) puzzleWord[i].y = 200 puzzleWord[i].letterSound = audio.loadSound( words[i]..".wav" ) puzzleWord[i]:addEventListener("tap", puzzleTouch) -- add event listener here which calls function puzzleTouch above end

Remember to remove all event listeners before you remove or done with the image.

Good Luck!

burhan

You may want to read up on Rob’s tutorial for shuffling table items here. He presents the concept with a deck of cards, but your idea is similar (alphabets instead of cards).

Hmm, that is only similar idea, I am looking for how it would be achivable to shuffle whole index of the array or select random array function. Thanks anyway.

Guys, any other ideas?

Hi @piotre80,

One way of doing it, if i understand you correctly, is:

a. Place all your puzzle word in a table or text file.

b. Retrieve a random word(string) from the table of words. –  e.g. “YES”

c. From the word read individual letter from the word and put in array.–wordArray={}  will become wordArray={“Y”,“E”,“S”} 

d. Create your display object (image files) from the individual letter. Assuming you name your image files as blockA.png to blockZ.png

eg.

local wordSpace = 20 – space between letters

local wordXPosition = 50

for i = 1, #wordArray do
puzzleWord[i] = display.newImageRect(“block”…wordArray[i]…".png")
puzzleWord[i].x = wordXPosition + (i * wordSpace)
puzzleWord[i].y = …and so on
end

e. get your next random word.

This is the method I use for my word game.

Good Luck!

burhan

Hi Burhan,

Thank you verym much for you fast response :slight_smile: . You have read my post from the begining to the end so thanks once again.

  I didn’t know that it is possible to to this in …this way and yes because this will an application for children I would like to display one single word a time. I would like to store this words using SQLite but at the moment  I am concentraiting how to display random words from the array.

Yesterday I had some time off and I tried to run this code like this:

local puzzleWord = {}
  local wordArray = {}
 
  wordArray= {“blockY.png”, “blockE.png”, “blockS.png”}
  wordArray= {“blockN.png”, “blockO.png”}
   
local wordSpace = 100 – space between letters
local wordXPosition = 10
for i = 1, #wordArray do
puzzleWord[i] = display.newImageRect(“block”…wordArray[i]…".png") — This one
puzzleWord[i].x = wordXPosition + (i * wordSpace)
puzzleWord[i].y  = wordXPosition + (i * wordSpace)
end
 

…and Unfortunately, it is not working ( attempt to index field? nil value) although, when I remove ("“block”, “.png”) from the line abve the images has been displayed. Probably now I need numbers or names to index words in the array and to let application “know” what it need to be displayed.

Yes, you are right. 

Anyway, the reason i put the “block”…wordArray[i]…".png" is because i retrieve my text files as whole word.

So my word in the text files will be {“YES”, “NO”, …and so on }. 

I then split my string “YES” and put in wordArray and it becomes wordArray[1] = “Y” , wordArray[2] = “E”, wordArray[3] = “S”.

In your case, your word if you save in text files or sql database will be  {“blockY.png”, “blockE.png”, “blockS.png”},  {“blockN.png”, “blockO.png”}, and so on.

Just imagine if you have hundreds or thousands of words you will then have to type a lot of text and also in my opinion difficult to read.

As for numbers to index the words can you retrieve by row number in your database?

Good Luck!

burhan

Hi Burhan ,

Excelent post my friend! I am very close to achieve the goal,  the only thing is that I must use sqlite to retrive random words , using txt file dislays a whole lenght of array with letters . :wink:

Regards

Piotr

Sorry guys, I am still having problem to retrieve these images and print them out from the array. I was working on two possible soutions using SQLite and JSON but none of them has resolved my problem yet. I got manage how to store and display images using SQLite but I found difficulties with printing random index from the array.

local words = {}
for row in db:nrows("SELECT * FROM test7  ") do
  local t = display.newImage(row. sL, fL, 50, 90*row.id, nil, 26 ) – Having two separate columns “sL” and “fL” unable to print two images at once

Here is another assumption that this can be managable using JSON format however I don’t know yet how to display a set of images and in the same time keep my files tidy and simple. Here imy question is, how I can store group of letters like in this example and display a whole index randomly.

local words = {
                “abc”: [ –
                {
                  “blockC” = “blockA.png”, –  display  this
                  “blockA” = “blockC.png”, - this
                  “blockB” = “blockB.png” –  and this
                }

                 “GMC”: [ – or
                {
                  “blockG” = “blockG.png”,  – this
                  “blockM” = “blockM.png”, – this
                  “blockC” = “blockC.png” – and this
                }
                
             
             ]}

Please help me get out of here !Thanks in advance… good people  ;)