Functions

Just wondering what the difference between “function ()” and “function (event” was, as I get the same results using both, and what add “return true” to the end of a function does, as I have been told that I should add it but my functions work fine without it [import]uid: 116264 topic_id: 24774 reply_id: 324774[/import]

When you call a function, the stuff inside the parentheses “()” is stuff that gets passed “into” the function. For example,

[lua]local dudeBro = function(aVariable)
print(aVariable)
end
dudeBro() – prints “nil”
dudeBro(“hi”) – prints “hi”[/lua]

“event” is a special variable covered in the Corona online docs. The most common use for it is a touch event - in that case it passes all sorts of useful information, like:

[lua]event.x, event.y – where your finger pressed
event.target – the display object you pressed
event.target.id – the id field of the display object you pressed[/lua]

[lua]return true[/lua] is a fun little piece of code you can add to the end of your touch functions. When you do that, it prevents you from being able to touch “through” that object (say you had two objects with listeners, one on top of the other? Normally it would trigger both!) [import]uid: 41884 topic_id: 24774 reply_id: 100475[/import]

thanks that has cleared things up a bit. One more question about event - what would be the difference between this:

[code]local function printText ()
print(“hello”)
end

printButton:addEventListener(“touch”, printText)
[/code]

and this:

[code]local function printText (event)
if event.phase == “began” then
print(“hello”)
end
end

printButton:addEventListener(“touch”, printText)[/code] [import]uid: 116264 topic_id: 24774 reply_id: 100793[/import]

In the first example, something occurs “whenever”.

Corona SDK: Events API
Touching a screen is actually a complex thing.

  1. Maybe you just started touching the screen (event.phase == “began”)
  2. Maybe you are dragging your finger (event.phase == “moved”)
  3. Maybe you just lifted your finger from the screen (event.phase == “ended”)
  4. Maybe you’re touching with more than one finger (way more complex)

If you just want to detect that first tap and nothing else, you should be using “tap” instead of “touch”. (It’s a bit tricky though, you usually have to code something to ignore the fact that you can tap several times in one shot)

Because “touch” has so many possibilities, Corona actually passes a table whenever you do one of those four things. It includes the phase, the target, where your finger was at the time, and so on.

Going back to your first example , because you don’t take in “event”, you can’t check for event.phase. That means the function will occur anytime you do ANY of those four things. (For example, on a single screen tap you’ll get the print statement 2-3 times)

On your second example, you take the event and check for a specific phase, so it only happens once per press.

Here’s a common way to setup a touch button with a simple object.

[lua]local function onPress(event)
if event.phase == “began” then
print(“started pressing!”)
button:setFillColor(30,30,30) – make the button dark
display.getCurrentStage():setFocus(event.target) – sets focus! This prevents anything else from being touchable until you release focus.

elseif event.phase == “ended” then
print(“finished pressing!”)
button:setFillColor(150,150,150) – make the button light again
– call whatever code or function you like here
display.getCurrentStage():setFocus(nil) – releases focus. Now you can press anything again.
end
end

local button = display.newRect(0,0,48,48)
button:setFillColor(150,150,150)
button:addEventListener(“touch”, onPress)[/lua]
[import]uid: 41884 topic_id: 24774 reply_id: 100800[/import]