obj.name or function to make images random

I have a game where I need to change images in a random order.

Imagine a face of a person.

where the face is one object, and the hair it’s another object.

What I would like to do, is tap on the hair, and I will get a different image hair (long, short, yellow, brown and so on) in the same place, so kids can change the look of the person

every time I tap.

and a different image face (happy, sad, scared…) – you get the idea.

I guess I need a table with many images – something like this – correct?

local hairTable {"long.png", "short.png", "yellow.png", "brown.png",} local faceTable {"happy.png", "sad.png", "scared.png", }

then I need a function like this to touch the object

local touchObject = function (event)     local obj = event.target       if event.phase == "ended" then           if obj.name == "face" then            --here I don't know how to do it         end           if obj.name == "hair" then            --here I don't know how to do it         end     end     return true end

But because both of them are --obj-- I don’t know how to distinguish them.

and also how to get the random images from the table.


Do I need something like this?

hair.brown.isVisible = false

hair.yellow.isVisible = true

but how do I get them from the table, in a random order


or like this

Do I need something like this?

hair[3].isVisible = false

hair[1].isVisible = true

I’m very confused


I have been working with tables for about 2 months

but I just copy and paste functions

I still don’t really get it.

I just hear words like – the 4th element, like table[4]

or thinks like that, but still not clear – I guess I’m close

but I need help

I would do this:

local hairTable {"long.png", "short.png", "yellow.png", "brown.png",} local hairIndex = 1 local faceTable {"happy.png", "sad.png", "scared.png", } local faceIndex = 1 local myHairObject local myFaceObject local touchObject = function (event) local obj = event.target if event.phase == "ended" then if obj.name == "face" then --first I remove the previous face if myFaceObject then myFaceObject:removeSelf() myFaceObject = nil end --I increase the index by 1 so that --I can get the next image name from the table faceIndex = faceIndex + 1 --if faceIndex gets too high then I reset it to 1 if faceIndex \> #faceTable then faceIndex = 1 end --and then add a new face, using the next image myFaceObject = display.newImageRect(faceTable[faceIndex], 128, 128) myFaceObject.name = "face" myFaceObject:addEventListener("touch", touchObject) end if obj.name == "hair" then if myHairObject then myHairObject:removeSelf() myHairObject = nil end hairIndex = hairIndex + 1 if hairIndex \> #hairIndex then hairIndex = 1 end myHairObject = display.newImageRect(hairTable[hairIndex], 128, 128) myHairObject.name = "hair" myHairObject:addEventListener("touch", touchObject) end end return true end myHairObject = display.newImageRect(hairTable[hairIndex], 128, 128) myHairObject.name = "hair" myHairObject:addEventListener("touch", touchObject) myFaceObject = display.newImageRect(faceTable[faceIndex], 128, 128) myFaceObject.name = "face" myFaceObject:addEventListener("touch", touchObject)

This may be a bit more difficult to explain seeing as you are not too sure about tables and indices, but I’ll try and explain it.

At the start you have your tables with the names of the images. I have then added 2 new variables “faceIndex” and “hairIndex”. Let’s just pay attention to the face one for now, to make things simpler.

The faceIndex states which entry in the faceTable I want to use when I create an image. I start at 1, which is the first item in your faceTable: “happy.png”

When I create the image, I use the current value of faceIndex to select which png file to use. I then add a “name” property to allow me to distinguish between the hair and face objects. However I am not creating all of the hair objects at once, since I only see one at any time, I only create them as I need them.

In the touchObject function, I check whether I am touching a face or hair object. Again for this example let’s assume I’ve touched the face. I then remove the existing face, and replace it with a new one. This should happen quickly enough that it appears instant, unless you have thousands of faces on screen. Before creating the new face, I increase the value of the faceIndex variable. If this is 2 then the image will be “sad.png”, if it is 3 then it will be “scared.png”. If it increases to 4, then there would be a problem. There are only 3 entries in the faceTable, so trying to access faceTable[4] would return nil and cause an error. So I have added a check which tests to see if faceIndex is higher than the number of entries in the faceTable. If it is, then I reset the value to 1.

You could use some other method using the .isVisible property, but this would involve you having a better understanding of how tables work.

Hopefully this method will work for now :slight_smile:

Edit: If you want to access a random image name, then you need to use math.random()

local randomIndex = math.random(1, #faceTable) myFaceObject = display.newImageRect(faceTable[randomIndex], 128, 128)

math.random takes 2 arguments: the first and last numbers you want your random number to be between. So using:

math.random(1, 100)

will give you a number from 1 to 100.

I have used:

math.random(1, #faceTable)

This will give me a random number between 1, and however many entries there are in the faceTable. In your code there were 3 entries in that table, so this will give us a random number between 1 and 3.

Thank you Alan, for taking the time to help me understand this.

I copy the code, just like yours.

I put it in a blank main.lua file

When I run the code, I see – the first “element” of the hairTable (conejo)

and the first element of the faceTable(cereza)

when I “tap” on --cereza image

I get this

File: assertion failed!

Assertion failed!

---------When I tap on --conejo image

I get this

File: …s/victormbarba/Desktop/How To Make An App 2/main.lua
Line: 49

Attempt to get length of upvalue ‘hairIndex’ (a number value)


I just change the name of the images, to actually see something in my code

And I did not get where I should use the math.random

thanks for your help. this is my code

-- this is to practice local background = display.newImage ("bg.png") local hairTable = {"conejo.png", "gallina.png", "vaca.png", "chimeney.png",} --long,short,yellow,brown local hairIndex = 1 local faceTable = {"cereza.png", "fresa.png", "limon.png", } -- happy,sad,scared local faceIndex = 1 local myHairObject local myFaceObject local touchObject = function (event)     local obj = event.target       if event.phase == "ended" then           if obj.name == "face" then                          --first I remove the previous face             if myFaceObject then                 myFaceObject:removeSelf()                 myFaceObject = nil             end             --I increase the index by 1 so that             --I can get the next image name from the table             faceIndex = faceIndex + 1             --if faceIndex gets too high then I reset it to 1             if faceIndex \> #faceTable then                 faceIndex = 1             end                          --and then add a new face, using the next image             myFaceObject = display.newImage(faceTable[faceIndex])             myFaceObject.name = "face"             myFaceObject:addEventListener("touch", touchObject)         end           if obj.name == "hair" then             if myHairObject then                 myHairObject:removeSelf()                 myHairObject = nil             end             hairIndex = hairIndex + 1             if hairIndex \> #hairIndex then                 hairIndex = 1             end                          myHairObject = display.newImage(hairTable[hairIndex])             myHairObject.name = "hair"             myHairObject:addEventListener("touch", touchObject)         end     end     return true end myHairObject = display.newImage(hairTable[hairIndex]) myHairObject.x = 300 myHairObject.y = 400 myHairObject.name = "hair" myHairObject:addEventListener("touch", touchObject) myFaceObject = display.newImage(faceTable[faceIndex]) myFaceObject.name = "face" myFaceObject:addEventListener("touch", touchObject)

Ah, I have a typo.

The line that says if
hairIndex > #hairIndex then
should say
if hairIndex > #hairTable then

The # character let’s you get the number of entries in a table. If you see it before any other type of variable it will give you this error.

There is typo

Where you have ‘if hairIndex > #hairIndex then’ change it to ‘if hairIndex > #hairTable then’

I change that to this

-- this is to practice local background = display.newImage ("bg.png") local hairTable = {"conejo.png", "gallina.png", "vaca.png", "chimeney.png",} --long,short,yellow,brown local hairIndex = 1 local faceTable = {"cereza.png", "fresa.png", "limon.png", } -- happy,sad,scared local faceIndex = 1 local myHairObject local myFaceObject local touchObject = function (event)     local obj = event.target       if event.phase == "ended" then           if obj.name == "face" then                          --first I remove the previous face             if myFaceObject then                 myFaceObject:removeSelf()                 myFaceObject = nil             end             --I increase the index by 1 so that             --I can get the next image name from the table             faceIndex = faceIndex + 1             --if faceIndex gets too high then I reset it to 1             if faceIndex \> #faceTable then                 faceIndex = 1             end                          --and then add a new face, using the next image             myFaceObject = display.newImage(faceTable[faceIndex])             myFaceObject.name = "face"             myFaceObject:addEventListener("touch", touchObject)         end           if obj.name == "hair" then             if myHairObject then                 myHairObject:removeSelf()                 myHairObject = nil             end             hairIndex = hairIndex + 1             if hairIndex \> #hairTable then                 hairIndex = 1             end                          myHairObject = display.newImage(hairTable[hairIndex])             myHairObject.name = "hair"             myHairObject:addEventListener("touch", touchObject)         end     end     return true end myHairObject = display.newImage(hairTable[hairIndex]) myHairObject.x = 300 myHairObject.y = 400 myHairObject.name = "hair" myHairObject:addEventListener("touch", touchObject) myFaceObject = display.newImage(faceTable[faceIndex]) myFaceObject.name = "face" myFaceObject:addEventListener("touch", touchObject)

Now when I click on the images

I get “assertion fail” on both of them

It should tell you which line number of your code is causing the error. Can you find that, and then tell us which line is causing the problem?

This is from the terminal when I click on the conejo image the first element from hair Table


2013-09-23 14:01:14.302 Corona Simulator[37140:f03] Runtime error
assertion failed!
stack traceback:
    [C]: ?
    [C]: in function ‘assert’
    ?: in function ‘getOrCreateTable’
    ?: in function ‘addEventListener’
    ?: in function ‘addEventListener’
    …s/victormbarba/Desktop/How To Make An App 2/main.lua:56: in function <…s/victormbarba/Desktop/How To Make An App 2/main.lua:13>
    ?: in function <?:218>
2013-09-23 14:01:14.338 Corona Simulator[37140:f03] RuntimeErrorNotification: Failed to parse error message: assertion failed!

-----------------when I click on the cereza the first element from faceTable

2013-09-23 14:01:55.792 Corona Simulator[37140:f03] Runtime error
assertion failed!
stack traceback:
    [C]: ?
    [C]: in function ‘assert’
    ?: in function ‘getOrCreateTable’
    ?: in function ‘addEventListener’
    ?: in function ‘addEventListener’
    …s/victormbarba/Desktop/How To Make An App 2/main.lua:38: in function <…s/victormbarba/Desktop/How To Make An App 2/main.lua:13>
    ?: in function <?:218>
2013-09-23 14:01:55.823 Corona Simulator[37140:f03] RuntimeErrorNotification: Failed to parse error message: assertion failed!


Ok, so this tells you exactly which line is causing your error:

 …s/victormbarba/Desktop/How To Make An App 2/main.lua:56: in function <…s/victormbarba/Desktop/How To Make An App 2/main.lua:13>

main.lua: line 13.

Since I don’t know if the code you pasted is the entire code for that file, can you look at your code and find line 13, then paste it here please? Also, can I just check that the code you last posted was copy and pasted directly from your project?

Here is the complete code in main.lua

-- this is to practice local background = display.newImage ("bg.png") local hairTable = {"conejo.png", "gallina.png", "vaca.png", "chimeney.png",} --long,short,yellow,brown local hairIndex = 1 local faceTable = {"cereza.png", "fresa.png", "limon.png", } -- happy,sad,scared local faceIndex = 1 local myHairObject local myFaceObject local touchObject = function (event) &nbsp;&nbsp;&nbsp; local obj = event.target &nbsp; &nbsp;&nbsp;&nbsp; if event.phase == "ended" then &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if obj.name == "face" then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --first I remove the previous face &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if myFaceObject then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myFaceObject:removeSelf() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myFaceObject = nil &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --I increase the index by 1 so that &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --I can get the next image name from the table &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; faceIndex = faceIndex + 1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --if faceIndex gets too high then I reset it to 1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if faceIndex \> #faceTable then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; faceIndex = 1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --and then add a new face, using the next image &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myFaceObject = display.newImage(faceTable[faceIndex]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myFaceObject.name = "face" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myFaceObject:addEventListener("touch", touchObject) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if obj.name == "hair" then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if myHairObject then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myHairObject:removeSelf() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myHairObject = nil &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; hairIndex = hairIndex + 1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if hairIndex \> #hairTable then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; hairIndex = 1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myHairObject = display.newImage(hairTable[hairIndex]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myHairObject.name = "hair" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myHairObject:addEventListener("touch", touchObject) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; return true end myHairObject = display.newImage(hairTable[hairIndex]) myHairObject.x = 300 myHairObject.y = 400 myHairObject.name = "hair" myHairObject:addEventListener("touch", touchObject) myFaceObject = display.newImage(faceTable[faceIndex]) myFaceObject.name = "face" myFaceObject:addEventListener("touch", touchObject)

since I delete a few blank lines now this is on terminal


2013-09-24 11:44:04.967 Corona Simulator[37140:f03] Runtime error
assertion failed!
stack traceback:
    [C]: ?
    [C]: in function ‘assert’
    ?: in function ‘getOrCreateTable’
    ?: in function ‘addEventListener’
    ?: in function ‘addEventListener’
    …s/victormbarba/Desktop/How To Make An App 2/main.lua:54: in function <…s/victormbarba/Desktop/How To Make An App 2/main.lua:12>
    ?: in function <?:218>
2013-09-24 11:44:04.971 Corona Simulator[37140:f03] RuntimeErrorNotification: Failed to parse error message: assertion failed!


2013-09-24 11:44:28.910 Corona Simulator[37140:f03] Runtime error
assertion failed!
stack traceback:
    [C]: ?
    [C]: in function ‘assert’
    ?: in function ‘getOrCreateTable’
    ?: in function ‘addEventListener’
    ?: in function ‘addEventListener’
    …s/victormbarba/Desktop/How To Make An App 2/main.lua:37: in function <…s/victormbarba/Desktop/How To Make An App 2/main.lua:12>
    ?: in function <?:218>
2013-09-24 11:44:28.914 Corona Simulator[37140:f03] RuntimeErrorNotification: Failed to parse error message: assertion failed!


I have nothing else in the code.

Thanks for your help

I would do this:

local hairTable {"long.png", "short.png", "yellow.png", "brown.png",} local hairIndex = 1 local faceTable {"happy.png", "sad.png", "scared.png", } local faceIndex = 1 local myHairObject local myFaceObject local touchObject = function (event) local obj = event.target if event.phase == "ended" then if obj.name == "face" then --first I remove the previous face if myFaceObject then myFaceObject:removeSelf() myFaceObject = nil end --I increase the index by 1 so that --I can get the next image name from the table faceIndex = faceIndex + 1 --if faceIndex gets too high then I reset it to 1 if faceIndex \> #faceTable then faceIndex = 1 end --and then add a new face, using the next image myFaceObject = display.newImageRect(faceTable[faceIndex], 128, 128) myFaceObject.name = "face" myFaceObject:addEventListener("touch", touchObject) end if obj.name == "hair" then if myHairObject then myHairObject:removeSelf() myHairObject = nil end hairIndex = hairIndex + 1 if hairIndex \> #hairIndex then hairIndex = 1 end myHairObject = display.newImageRect(hairTable[hairIndex], 128, 128) myHairObject.name = "hair" myHairObject:addEventListener("touch", touchObject) end end return true end myHairObject = display.newImageRect(hairTable[hairIndex], 128, 128) myHairObject.name = "hair" myHairObject:addEventListener("touch", touchObject) myFaceObject = display.newImageRect(faceTable[faceIndex], 128, 128) myFaceObject.name = "face" myFaceObject:addEventListener("touch", touchObject)

This may be a bit more difficult to explain seeing as you are not too sure about tables and indices, but I’ll try and explain it.

At the start you have your tables with the names of the images. I have then added 2 new variables “faceIndex” and “hairIndex”. Let’s just pay attention to the face one for now, to make things simpler.

The faceIndex states which entry in the faceTable I want to use when I create an image. I start at 1, which is the first item in your faceTable: “happy.png”

When I create the image, I use the current value of faceIndex to select which png file to use. I then add a “name” property to allow me to distinguish between the hair and face objects. However I am not creating all of the hair objects at once, since I only see one at any time, I only create them as I need them.

In the touchObject function, I check whether I am touching a face or hair object. Again for this example let’s assume I’ve touched the face. I then remove the existing face, and replace it with a new one. This should happen quickly enough that it appears instant, unless you have thousands of faces on screen. Before creating the new face, I increase the value of the faceIndex variable. If this is 2 then the image will be “sad.png”, if it is 3 then it will be “scared.png”. If it increases to 4, then there would be a problem. There are only 3 entries in the faceTable, so trying to access faceTable[4] would return nil and cause an error. So I have added a check which tests to see if faceIndex is higher than the number of entries in the faceTable. If it is, then I reset the value to 1.

You could use some other method using the .isVisible property, but this would involve you having a better understanding of how tables work.

Hopefully this method will work for now :slight_smile:

Edit: If you want to access a random image name, then you need to use math.random()

local randomIndex = math.random(1, #faceTable) myFaceObject = display.newImageRect(faceTable[randomIndex], 128, 128)

math.random takes 2 arguments: the first and last numbers you want your random number to be between. So using:

math.random(1, 100)

will give you a number from 1 to 100.

I have used:

math.random(1, #faceTable)

This will give me a random number between 1, and however many entries there are in the faceTable. In your code there were 3 entries in that table, so this will give us a random number between 1 and 3.

Thank you Alan, for taking the time to help me understand this.

I copy the code, just like yours.

I put it in a blank main.lua file

When I run the code, I see – the first “element” of the hairTable (conejo)

and the first element of the faceTable(cereza)

when I “tap” on --cereza image

I get this

File: assertion failed!

Assertion failed!

---------When I tap on --conejo image

I get this

File: …s/victormbarba/Desktop/How To Make An App 2/main.lua
Line: 49

Attempt to get length of upvalue ‘hairIndex’ (a number value)


I just change the name of the images, to actually see something in my code

And I did not get where I should use the math.random

thanks for your help. this is my code

-- this is to practice local background = display.newImage ("bg.png") local hairTable = {"conejo.png", "gallina.png", "vaca.png", "chimeney.png",} --long,short,yellow,brown local hairIndex = 1 local faceTable = {"cereza.png", "fresa.png", "limon.png", } -- happy,sad,scared local faceIndex = 1 local myHairObject local myFaceObject local touchObject = function (event) &nbsp;&nbsp;&nbsp; local obj = event.target &nbsp; &nbsp;&nbsp;&nbsp; if event.phase == "ended" then &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if obj.name == "face" then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --first I remove the previous face &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if myFaceObject then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myFaceObject:removeSelf() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myFaceObject = nil &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --I increase the index by 1 so that &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --I can get the next image name from the table &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; faceIndex = faceIndex + 1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --if faceIndex gets too high then I reset it to 1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if faceIndex \> #faceTable then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; faceIndex = 1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --and then add a new face, using the next image &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myFaceObject = display.newImage(faceTable[faceIndex]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myFaceObject.name = "face" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myFaceObject:addEventListener("touch", touchObject) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if obj.name == "hair" then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if myHairObject then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myHairObject:removeSelf() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myHairObject = nil &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; hairIndex = hairIndex + 1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if hairIndex \> #hairIndex then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; hairIndex = 1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myHairObject = display.newImage(hairTable[hairIndex]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myHairObject.name = "hair" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myHairObject:addEventListener("touch", touchObject) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; return true end myHairObject = display.newImage(hairTable[hairIndex]) myHairObject.x = 300 myHairObject.y = 400 myHairObject.name = "hair" myHairObject:addEventListener("touch", touchObject) myFaceObject = display.newImage(faceTable[faceIndex]) myFaceObject.name = "face" myFaceObject:addEventListener("touch", touchObject)

Ah, I have a typo.

The line that says if
hairIndex > #hairIndex then
should say
if hairIndex > #hairTable then

The # character let’s you get the number of entries in a table. If you see it before any other type of variable it will give you this error.

There is typo

Where you have ‘if hairIndex > #hairIndex then’ change it to ‘if hairIndex > #hairTable then’

I change that to this

-- this is to practice local background = display.newImage ("bg.png") local hairTable = {"conejo.png", "gallina.png", "vaca.png", "chimeney.png",} --long,short,yellow,brown local hairIndex = 1 local faceTable = {"cereza.png", "fresa.png", "limon.png", } -- happy,sad,scared local faceIndex = 1 local myHairObject local myFaceObject local touchObject = function (event) &nbsp;&nbsp;&nbsp; local obj = event.target &nbsp; &nbsp;&nbsp;&nbsp; if event.phase == "ended" then &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if obj.name == "face" then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --first I remove the previous face &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if myFaceObject then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myFaceObject:removeSelf() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myFaceObject = nil &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --I increase the index by 1 so that &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --I can get the next image name from the table &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; faceIndex = faceIndex + 1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --if faceIndex gets too high then I reset it to 1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if faceIndex \> #faceTable then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; faceIndex = 1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --and then add a new face, using the next image &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myFaceObject = display.newImage(faceTable[faceIndex]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myFaceObject.name = "face" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myFaceObject:addEventListener("touch", touchObject) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if obj.name == "hair" then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if myHairObject then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myHairObject:removeSelf() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myHairObject = nil &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; hairIndex = hairIndex + 1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if hairIndex \> #hairTable then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; hairIndex = 1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myHairObject = display.newImage(hairTable[hairIndex]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myHairObject.name = "hair" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myHairObject:addEventListener("touch", touchObject) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; return true end myHairObject = display.newImage(hairTable[hairIndex]) myHairObject.x = 300 myHairObject.y = 400 myHairObject.name = "hair" myHairObject:addEventListener("touch", touchObject) myFaceObject = display.newImage(faceTable[faceIndex]) myFaceObject.name = "face" myFaceObject:addEventListener("touch", touchObject)

Now when I click on the images

I get “assertion fail” on both of them

It should tell you which line number of your code is causing the error. Can you find that, and then tell us which line is causing the problem?

This is from the terminal when I click on the conejo image the first element from hair Table


2013-09-23 14:01:14.302 Corona Simulator[37140:f03] Runtime error
assertion failed!
stack traceback:
    [C]: ?
    [C]: in function ‘assert’
    ?: in function ‘getOrCreateTable’
    ?: in function ‘addEventListener’
    ?: in function ‘addEventListener’
    …s/victormbarba/Desktop/How To Make An App 2/main.lua:56: in function <…s/victormbarba/Desktop/How To Make An App 2/main.lua:13>
    ?: in function <?:218>
2013-09-23 14:01:14.338 Corona Simulator[37140:f03] RuntimeErrorNotification: Failed to parse error message: assertion failed!

-----------------when I click on the cereza the first element from faceTable

2013-09-23 14:01:55.792 Corona Simulator[37140:f03] Runtime error
assertion failed!
stack traceback:
    [C]: ?
    [C]: in function ‘assert’
    ?: in function ‘getOrCreateTable’
    ?: in function ‘addEventListener’
    ?: in function ‘addEventListener’
    …s/victormbarba/Desktop/How To Make An App 2/main.lua:38: in function <…s/victormbarba/Desktop/How To Make An App 2/main.lua:13>
    ?: in function <?:218>
2013-09-23 14:01:55.823 Corona Simulator[37140:f03] RuntimeErrorNotification: Failed to parse error message: assertion failed!


Ok, so this tells you exactly which line is causing your error:

 …s/victormbarba/Desktop/How To Make An App 2/main.lua:56: in function <…s/victormbarba/Desktop/How To Make An App 2/main.lua:13>

main.lua: line 13.

Since I don’t know if the code you pasted is the entire code for that file, can you look at your code and find line 13, then paste it here please? Also, can I just check that the code you last posted was copy and pasted directly from your project?

Here is the complete code in main.lua

-- this is to practice local background = display.newImage ("bg.png") local hairTable = {"conejo.png", "gallina.png", "vaca.png", "chimeney.png",} --long,short,yellow,brown local hairIndex = 1 local faceTable = {"cereza.png", "fresa.png", "limon.png", } -- happy,sad,scared local faceIndex = 1 local myHairObject local myFaceObject local touchObject = function (event) &nbsp;&nbsp;&nbsp; local obj = event.target &nbsp; &nbsp;&nbsp;&nbsp; if event.phase == "ended" then &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if obj.name == "face" then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --first I remove the previous face &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if myFaceObject then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myFaceObject:removeSelf() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myFaceObject = nil &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --I increase the index by 1 so that &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --I can get the next image name from the table &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; faceIndex = faceIndex + 1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --if faceIndex gets too high then I reset it to 1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if faceIndex \> #faceTable then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; faceIndex = 1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --and then add a new face, using the next image &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myFaceObject = display.newImage(faceTable[faceIndex]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myFaceObject.name = "face" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myFaceObject:addEventListener("touch", touchObject) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if obj.name == "hair" then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if myHairObject then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myHairObject:removeSelf() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myHairObject = nil &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; hairIndex = hairIndex + 1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if hairIndex \> #hairTable then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; hairIndex = 1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myHairObject = display.newImage(hairTable[hairIndex]) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myHairObject.name = "hair" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myHairObject:addEventListener("touch", touchObject) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; return true end myHairObject = display.newImage(hairTable[hairIndex]) myHairObject.x = 300 myHairObject.y = 400 myHairObject.name = "hair" myHairObject:addEventListener("touch", touchObject) myFaceObject = display.newImage(faceTable[faceIndex]) myFaceObject.name = "face" myFaceObject:addEventListener("touch", touchObject)

since I delete a few blank lines now this is on terminal


2013-09-24 11:44:04.967 Corona Simulator[37140:f03] Runtime error
assertion failed!
stack traceback:
    [C]: ?
    [C]: in function ‘assert’
    ?: in function ‘getOrCreateTable’
    ?: in function ‘addEventListener’
    ?: in function ‘addEventListener’
    …s/victormbarba/Desktop/How To Make An App 2/main.lua:54: in function <…s/victormbarba/Desktop/How To Make An App 2/main.lua:12>
    ?: in function <?:218>
2013-09-24 11:44:04.971 Corona Simulator[37140:f03] RuntimeErrorNotification: Failed to parse error message: assertion failed!


2013-09-24 11:44:28.910 Corona Simulator[37140:f03] Runtime error
assertion failed!
stack traceback:
    [C]: ?
    [C]: in function ‘assert’
    ?: in function ‘getOrCreateTable’
    ?: in function ‘addEventListener’
    ?: in function ‘addEventListener’
    …s/victormbarba/Desktop/How To Make An App 2/main.lua:37: in function <…s/victormbarba/Desktop/How To Make An App 2/main.lua:12>
    ?: in function <?:218>
2013-09-24 11:44:28.914 Corona Simulator[37140:f03] RuntimeErrorNotification: Failed to parse error message: assertion failed!


I have nothing else in the code.

Thanks for your help