Remove objects then display new from an array

Hello All,

I am trying to do a simple educational application that display words on the screen so far so good. I can display random words using SQLite and each letter can be removed when I tap them. At this point I would like to add a function or statement that displays an image when all letters will disappear, using for  loop display another word and so on. At this stage my idea is that images are stored together with words (letters) in database.  Any constructive advice is welcome !

-- remove letter local function onTouch(self, event)   display.remove(self) end        -- display word from database   local wordSpace = 150 -- space between letters   local wordXPosition = 0   for i = 1, #words do    puzzleWord[i] = display.newImage(words[i]..".png")-- display word from database    puzzleWord[i].x = wordXPosition + (i \* wordSpace) puzzleWord[i].y = 100                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              puzzleWord[i].tap = onTouch    puzzleWord[i]: addEventListener("tap") )   end

I would strongly discourage you from trying to place images in a database (by that I presume you mean something like SQLite). Corona supports subdirectories in your resource directory. You should use them. Or download the PNG|JPG files, but it sounds like you may want your app to work without a network connection.

I concure, putting the images in the database is just extra overhead.  They are best kept as .jpg and .png files in your projects folders.  If you’re going to have a lot of words that need to be images, perhaps you should look at an image sheet.  one big image with all your words in there.  Corona can be told how to separate them.  You can use something like TexturePacker to help manage them too.

But since you want something to happen when you clear all the words, you’re going to have to have a counter or something that keeps track of the total number of words and the number cleared.  Perhaps you could start with a variable:

local wordsLeft = totalWords

then subtract one from wordsLeft each time someone clears a word.  In the code where you clear the words, check to see if wordsLeft is 0 and when so, you know you have no words left.  The other way is to count up from zero:

local clearedWords = 0

When you clear a word, do:

clearedWords = clearedWords + 1

if clearedWords > totalWords then

    – here you deal with all the words being gone.

end

Rob

Thanks You Rob for your interest. The use of image sheet make sens the only thing is that I going to have lets say 20 words that consists of 2,3,4,5,6  letters displayed randomly. In terms of that is there any chance to do this way? Is it possible to add more images sheets ? What do you think?

Lets assume you have 20 words that are in separate image files.  Using something like TexturePacker, it will optimize placement of the individual images in one large image and produce the Corona Lua code needed to pass directly to the graphics.newImageSheet() API.  Then you can reference each image by an ID.

I’m sure there are some tutorials around on using TexturePacker and image sheets.  You of course could always use a grid and hand code your sheet information, but with the variable length words, TexturePacker seems like the more logical way to go.

Rob

Thank you again.  I greatlly appreciate your input but I have a few things to implement and finish my first game, therefore I am looking for answer how I can display specific image when all images will be removed from the screen?

I have added IF statement but still no results and nothing is going happen when al letters are removed.

 local puzzleTouch = function (event)   local target = event.target   audio.play(target.letterSound) end local function onTouch(self, event)   display.remove(self) end function level1()   local wordSpace = 150 -- space between letters   local wordXPosition = 0   for i = 1, #words do    puzzleWord[i] = display.newImage(words[i]..".png")    puzzleWord[i].x = wordXPosition + (i \* wordSpace)    puzzleWord[i].y = 100                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  puzzleWord[i].letterSound = audio.loadSound(words[i].. ".wav")    puzzleWord[i]: addEventListener("tap", puzzleTouch)  --This function doesn't return anything but it doesn't do anything as well   if puzzleWord[i] == nil then       display.newImage(words[i].."pic.png")   end end

Any suggestions guys how to display picture this way?

I would strongly discourage you from trying to place images in a database (by that I presume you mean something like SQLite). Corona supports subdirectories in your resource directory. You should use them. Or download the PNG|JPG files, but it sounds like you may want your app to work without a network connection.

I concure, putting the images in the database is just extra overhead.  They are best kept as .jpg and .png files in your projects folders.  If you’re going to have a lot of words that need to be images, perhaps you should look at an image sheet.  one big image with all your words in there.  Corona can be told how to separate them.  You can use something like TexturePacker to help manage them too.

But since you want something to happen when you clear all the words, you’re going to have to have a counter or something that keeps track of the total number of words and the number cleared.  Perhaps you could start with a variable:

local wordsLeft = totalWords

then subtract one from wordsLeft each time someone clears a word.  In the code where you clear the words, check to see if wordsLeft is 0 and when so, you know you have no words left.  The other way is to count up from zero:

local clearedWords = 0

When you clear a word, do:

clearedWords = clearedWords + 1

if clearedWords > totalWords then

    – here you deal with all the words being gone.

end

Rob

Thanks You Rob for your interest. The use of image sheet make sens the only thing is that I going to have lets say 20 words that consists of 2,3,4,5,6  letters displayed randomly. In terms of that is there any chance to do this way? Is it possible to add more images sheets ? What do you think?

Lets assume you have 20 words that are in separate image files.  Using something like TexturePacker, it will optimize placement of the individual images in one large image and produce the Corona Lua code needed to pass directly to the graphics.newImageSheet() API.  Then you can reference each image by an ID.

I’m sure there are some tutorials around on using TexturePacker and image sheets.  You of course could always use a grid and hand code your sheet information, but with the variable length words, TexturePacker seems like the more logical way to go.

Rob

Thank you again.  I greatlly appreciate your input but I have a few things to implement and finish my first game, therefore I am looking for answer how I can display specific image when all images will be removed from the screen?

I have added IF statement but still no results and nothing is going happen when al letters are removed.

 local puzzleTouch = function (event)   local target = event.target   audio.play(target.letterSound) end local function onTouch(self, event)   display.remove(self) end function level1()   local wordSpace = 150 -- space between letters   local wordXPosition = 0   for i = 1, #words do    puzzleWord[i] = display.newImage(words[i]..".png")    puzzleWord[i].x = wordXPosition + (i \* wordSpace)    puzzleWord[i].y = 100                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  puzzleWord[i].letterSound = audio.loadSound(words[i].. ".wav")    puzzleWord[i]: addEventListener("tap", puzzleTouch)  --This function doesn't return anything but it doesn't do anything as well   if puzzleWord[i] == nil then       display.newImage(words[i].."pic.png")   end end

Any suggestions guys how to display picture this way?