A simple question

Hi, I have recently bought Codea on iPad and played with it a little bit. It is really a great learning tool for programming. I hope I can design and write some simple game for my little daughter sometime in future.

Anyway, I have only very basic programming knowledge and very limited programming experiences. So I start from learning the sample project that came with Codea. Here is one question that I have with the “Multi Touch” example. It looks like the program use a table variant touches, and store the touch information at the location specified via touch.id. For example, when I touch the screen, a touch with the id 12345 (I made up the number) generated. Does this touch information store as the 12345th element of the table touches? If so, the for loop in the draw() function would executed 12345 times? And what about the first 12344 elements?

You are correct, it does store the items in a table with the index being whatever the touch ID is. However, in Lua/ Codea, when a new element is created with the index 12345, it does not make the table that large. Consider the following code:

MyTable = {}
MyTable[1] = 1
MyTable[450] = 1
MyTable["hello"]  = "hi"
print ( "The table has " .. #MyTable .. " elements.") -- #tablename gives the table size

The code would print 3. And the code in the multitouch example basically executes a foreach loop and only runs as many times as there are touches in the table.

Thanks for the reply. Here is another question: in the “Handling Touches” example, when I touch the screen and move my finger, the ellipse generated from previous frame gradually fade. Why does this happen? The backingMode is set to RETAINED in the setup() function, why wouldn’t the ellipse be retained forever? I also noticed that when I just touch the screen without moving, the ellipse never fade. Could anyone please explain why this happens? Thanks!

The reason for the fade is that the fill() on line 16 is black with a low alpha value of 10 (out of 255). So it’s just adding a slight tint each frame which gradually darkens everything that has been drawn previously. As for the second question, when you keep your finger on the screen the following at line 33 will always be true:

elseif CurrentTouch.state == MOVING then

… even if you’re not actually moving your finger. I guess MOVING is just the name for the thing that comes between BEGAN and ENDED

Hmm, not quite. MOVING means: has moved.

It’s really hard to touch the screen without moving, but if you really manage it then you’ll see that the ellipse stays blue. This means that the state is still BEGAN and not MOVING. The reason the ellipse doesn’t fade is because it is drawn every loop regardless. The test only sets the colour: blue for BEGAN, red for MOVING, and yellow for ENDED. The loop always draws an ellipse at the last known touch point. So if you touch the screen and then take your finger away, you’ll see that the yellow circle doesn’t fade. Not until you touch somewhere else, whereupon it starts to fade as it is no longer being replenished.

@Vega about the # operator, it does not return the table size. It returns a integral index such that (for a table t)

t[#t] ~= nil and t[#t + 1] == nil

So in your example it returns 1. Also note, it does not return the largest index with this property nor the smallest, but just some index with this property. So basically it is only really useful for tables used as arrays without nils in them.

just some index

I believe it is the first positive integer such that that condition is true.

Lua tables have an “array” part and a “table” part. The array part has various useful bits and pieces that are optimised and it works by iterating through the indices until it reaches a nil value.

Thank you, @gunnar_z, I actually learned that recently and forgot. Sorry for the defective sample code, @Muteday. The concept is still correct, but the # operator was the incorrect way to show it.

@Andrew_Stacey look here:


it can definitely be any index for which said condition is true.

@gunnar_z Yup, that settles it. I had in mind things like ipairs which iterate through until they find a nil value.

I guess I’m still a little uncomfortable with the table that Codea/Lua set-up.
If I type in the following code:
function setup()


for i,j in pairs(test) do


What I got printed is 6 4 2 2 8 5 10 6 4 3
Why in this order? (I thought it would print 2 2 4 3 6 4 8 5 10 6)


@Muteday you dont have an initial value. Its looking for a 1 and the first time it sees 1 is when it gets to the 10th index. So it starts with 10 and works backwards from there.

try putting a 0th index in. It will be put at the end of the table. test[0] = 0

ask at the beginning “test [1] = 0”

The iterator pairs loops over all the keys in the table in an unspecified order (most likely the order that they are stored in memory). To get an ordered loop, use ipairs. However, ipairs on this table will fail as it starts at 1 and keeps going until it reaches a nil value so as test[1] is nil, it will stop straight away.