Select and display a number of images from array list

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