hi, problem script

hi,

I saw this tutorial, http://wn.com/Corona_SDK_11_Custom_Fonts

but I did not understand this script:

local animation = {“a”, “A”, “S”, “S”, “T”, “T”, “T”, “S”, “A”, “a”}; – change font?

if(e.phase == “ended” or e.phase == “cancelled”) then --??

local function hello(e) – (e) is? (e) = event??
if (e.count == 1) then --???
end

alien.text = animation[e.count % #animation + 1]; – ???

if(e.count == #animation) then – ???

timer.cancel(self.timer); --???
self.timer = nil; --??
end

end

self.timer = timer.performWithDelay(20, hello, #animation) --??

end

end

alien:addEventListener(“touch”, alien); - is ok

someone is kind enough to explain step by step? the "(E), e.phase what does it mean?

Sorry for my english: (

thank you very much

Corona is fantastic [import]uid: 118312 topic_id: 20707 reply_id: 320707[/import]

Okay, let me take a try at this…

First, its really helpful if you post code using the string:
< code > without the spaces before your code block and end it with < /code > again without the spaces

local animation = {"a", "A", "S", "S", "T", "T", "T", "S", "A", "a"}; -- change font?  
  
if(e.phase == "ended" or e.phase == "cancelled") then --??  
 local function hello(e) -- (e) is? (e) = event??  
 if (e.count == 1) then --?????  
 end  
  
 alien.text = animation[e.count % #animation + 1]; -- ??????  
  
 if(e.count == #animation) then -- ?????  
 timer.cancel(self.timer); --???  
 self.timer = nil; --??  
 end  
  
 end  
 self.timer = timer.performWithDelay(20, hello, #animation) --??  
end  
  
end  
  
alien:addEventListener("touch", alien); - is ok  

So first things first. There is apparently a line missing at the top, perhaps something like:

local function alien(e)  

In this case, e is shorthand for event, which is the lua table/data structure passed to a function when it’s triggered from an event, in this case the last line is calling a function called alien when the display object alien is touched. To answer your last question “is this okay?” No, not really. One of two things needs to change to make it okay.

First, the way I would do it, is I’m assuming alien is a display object, like an image of the alien. In my case I would change the name of the function to something like:

local function alienTouch(event) -- or use the e short hand to stay consisitant  

then I would make my event handler look like:

alien:addEventListener("touch", alienTouch)  

But if the last two lines were:

alien.touch = alienTouch  
alien:addEventListener("touch", alien)  

would work. Either way, the function name and the object name have to be different.

Next the line about

local animation = {"a", "A", "S", "S", "T", "T", "T", "S", "A", "a"}; -- change font?  

The author is creating an array or table of letters. Then based on this line of code:

alien.text = animation[e.count % #animation + 1]; -- ??????  

alien is apparently not a graphic but a display.newText() object and the author is a single letter being drawn on the screen. Based on the value of e.count, he is grabbing a single letter (well its really a string with just one letter in it). Which letter is based on whatever the remainder is of e.count / the number of strings in the animation table, in this case 10. So its the remainder of e.count / 10 + 1. So every 10 times e.count goes up, the letter drawn on the screen will change. The + 1 is to adjust for the fact that the modulus operator (%) which gives us a remainder of an integer division will produce the number 0 for values 0-9 in this equation. So once e.count becomes 10, the letter “A” will show on the screen.

 if (e.count == 1) then --?????  
 end  

isn’t doing anything.

Skipping ahead a bit:

 self.timer = timer.performWithDelay(20, hello, #animation) --??  

This is creating a timer that runs after 20ms that calls a function called hello and will be run #animation number of times (10) in this case. So every 20ms, hello gets called and this happens 10 times. The timer passes the .count variable as part of the event structure.

Since this only runs 10 times the statement above where he is doing e.count % #animation + 1 will always show the first letter except for the last time when it will show the 2nd letter.

The handle for the timer is being saved in the “self” object with the name “timer”. “self” is a reference to the calling object so your function doesn’t have to know which object called the function, it provides it for you.

 if(e.count == #animation) then -- ?????  
 timer.cancel(self.timer); --???  
 self.timer = nil; --??  
 end  

and finally, if e.count equals #animation (or 10, the last time the timer fires) then it cancels the timer and sets the timer handle to nil so garage collection will free up the memory in a little bit.

[import]uid: 19626 topic_id: 20707 reply_id: 81314[/import]

Thank you for responding:)

  
local animation = {"a", "A", "S", "S", "T", "T", "T", "S", "A", "a"}; -- ok  
  
if(e.phase == "ended" or e.phase == "cancelled") then  
-- ???????  
  
local function hello(e)  
 if (e.count == 1) then ---???  
end  
  

what this code means? :S

If you know where to find a guide that explains what the symbols mean? %, #, etc. … ==

thanks a lot again

thx :slight_smile: [import]uid: 118312 topic_id: 20707 reply_id: 81416[/import]