.graphic? question from match the letter sample code

Hello!

I’ve been studying the Match the Letter sample code. I’m confused about this function I have inserted below.

Specifically, what is going on in this line:
local char = factory.newLetterButton(char,wordColor).graphics

and this line

wordGraphic:insert(char)

What is the .graphics thing, and is “wordGraphic” a table? I didn’t see it defined anywhere else in the code…

Maybe i’m missing something though, I’m newbie! Any help appreciated!

FROM: Match the Letter (kids’ game) by John Polacek
https://github.com/johnpolacek/Match-The-Letter-Game

[code]
function showWord()
– THIS SHOWS THE ENTIRE WORD ONCE YOU HAVE ANSWERED THE QUESTION
– get word from content data object
– see content.lua, playOrder is random. currQuestion starts at 0.
local word = content[playOrder[currQuestion]].word

– make word graphic and transition in
wordGraphic = display.newGroup()
local wordColor = factory.getRandomColors(1)[1]
– Delay is the delay after each letter pops on the screen
local animationDelay = 500

– 1 to end of word
for i=1,string.len(word) do
– store the single letter into a variable
local char = word:sub(i,i)
if char == " " then
– space (just doing nothing, and loop will go to next i)
else
local char = factory.newLetterButton(char,wordColor).graphics
wordGraphic:insert(char)
char.x = (char.width * .72) * (i-1)
local charDelay = (i-1) * animationDelay
transition.from(char, {time = 400, delay = charDelay, y=centerY+char.height, transition=easing.easeOutElastic})
end
end
wordGraphic.x = centerX - wordGraphic.width/2
wordGraphic.y = centerY - wordGraphic.height/2
timer.performWithDelay(wordGraphic.numChildren * animationDelay + 1000, onWordComplete)
end
[/code] [import]uid: 191855 topic_id: 33387 reply_id: 333387[/import]

Look in the factory.lua file - you have all the functions in that file.

Joakim [import]uid: 81188 topic_id: 33387 reply_id: 132602[/import]

This is actually a good learning exercise. Let’s look at what that newButton function is doing. If you go look at that code you will see that the function is returning a table. That table has two members in it:

  • graphic

  • letter

What this:

local char = factory.newLetterButton(char,wordColor).graphics  

is doing is calling the function newLetterButton() which returns a reference to a table. Since the result of that function is a table, you can directly reference it’s members. It’s the same as:

local letterButton = factory.newLetterButton(char,wordColor).graphics  
local char = letterButton.graphics  

[import]uid: 199310 topic_id: 33387 reply_id: 132674[/import]

Look in the factory.lua file - you have all the functions in that file.

Joakim [import]uid: 81188 topic_id: 33387 reply_id: 132602[/import]

This is actually a good learning exercise. Let’s look at what that newButton function is doing. If you go look at that code you will see that the function is returning a table. That table has two members in it:

  • graphic

  • letter

What this:

local char = factory.newLetterButton(char,wordColor).graphics  

is doing is calling the function newLetterButton() which returns a reference to a table. Since the result of that function is a table, you can directly reference it’s members. It’s the same as:

local letterButton = factory.newLetterButton(char,wordColor).graphics  
local char = letterButton.graphics  

[import]uid: 199310 topic_id: 33387 reply_id: 132674[/import]

Thanks for the input, I do appreciate it. I’m sorry to report that I actually have not been able to figure out what a table member is. I’ve checked Understanding Lua Tables in Corona SDK, but I don’t see any mention of members.

http://www.coronalabs.com/blog/2011/06/21/understanding-lua-tables-in-corona-sdk/

From the syntax it looks to be some sort of attribute attached to the table? Just as if the table is any other object?

I’ll keep researching – apologies that your advice is being missed because of such a basic point! [import]uid: 191855 topic_id: 33387 reply_id: 133838[/import]

Thanks for the input, I do appreciate it. I’m sorry to report that I actually have not been able to figure out what a table member is. I’ve checked Understanding Lua Tables in Corona SDK, but I don’t see any mention of members.

http://www.coronalabs.com/blog/2011/06/21/understanding-lua-tables-in-corona-sdk/

From the syntax it looks to be some sort of attribute attached to the table? Just as if the table is any other object?

I’ll keep researching – apologies that your advice is being missed because of such a basic point! [import]uid: 191855 topic_id: 33387 reply_id: 133838[/import]