Checking touch event

Hi, Im having some problems trying to check if a touch event was made or not.

[lua]local points = 0
local function pressChest (event)
if event.phase == “began” then
local tex = display.newText(“nah”,100,140,nil,40)
points = points +1
print (points)
return 1
end
end
if pressChest == 1 then
local pointsString = display.newText(points,200,200,nil,40)
end[/lua]
I also tried

[lua]local points = 0

local pointsString = display.newText(points,200,200,nil,40)

local function pressChest (event)
if event.phase == “began” then
local texto2 = display.newText(“nah”,100,140,nil,40)
pointsString.text = points +1
print (points)
return 1
end
end[/lua]

But it doesnt update, i have to click another button to update the score in the screen.

Any advice?
Thanks [import]uid: 108461 topic_id: 20469 reply_id: 320469[/import]

Something like this:

[lua]local function f_test(event)
local tex = display.newText(“hello”,20,20,nil,20)
return 1 – to call it later?
end
if f_test() == 1 then
local imag = display.newImage(“ok.png”)
f_test = 0 – to reset it? and then the next time it does it again
end[/lua]

An easy way to do something IF the function was completed (lets say it was handled already). [import]uid: 108461 topic_id: 20469 reply_id: 80292[/import]

Need help with this. [import]uid: 108461 topic_id: 20469 reply_id: 80897[/import]

Its hard to believe nobody knows how to check if a function was executed. [import]uid: 108461 topic_id: 20469 reply_id: 81779[/import]

@undecode - From your first post, This is the correct way of handling the event

local points = 0  
   
local pointsString = display.newText(points,200,200,nil,40)  
   
local function pressChest (event)  
 if event.phase == "began" then  
 local texto2 = display.newText("nah",100,140,nil,40)  
 pointsString.text = points +1  
 print (points)  
 return 1  
 end  
end  
  

What issues are you facing with this code. This should update the pointsString. If you are trying to increment the points then save the update points and then update the string as show below:

points = points + 1  
pointsString.text = points  
  

Also, please read about the events and listener from the following page…This should give you a good idea about how lua engine executes functions.

http://developer.anscamobile.com/content/events-and-listeners

If you have any questions, feel free to post on the forums…

Hope this helps. [import]uid: 84539 topic_id: 20469 reply_id: 81787[/import]

Bejoy, thanks a lot for the answer.

What if i want to do something like this?:
[lua]local function test()
local _text = display.newText(“testing”,100,100,nil,40)
return 1
end

if test == 1 then
display.newText(“hello”,150,150,nil,40)
end[/lua]
It doens’t seem to work… [import]uid: 108461 topic_id: 20469 reply_id: 81804[/import]

it will work if you do call the function and then compare like shown below:

local function test()  
 local \_text = display.newText("testing",100,100,nil,40)  
 return 1  
end  
   
if test() == 1 then -- Note it is test() and not test  
 display.newText("hello",150,150,nil,40)  
end  

Now, Event is a something that happens in the future…The whole lua file is executed from top to down when the file is loaded first time…All event definitions are then executed when it is triggered through user interaction…hence events is something you cannot call manually…

Hope this helps. [import]uid: 84539 topic_id: 20469 reply_id: 81866[/import]

Then im going in the wrong direction. How can I say “if this event was completed, then do this”? outside the function [import]uid: 108461 topic_id: 20469 reply_id: 81936[/import]

event phases define if the event is complete or not…A touch event has the following phase:

  1. Began
  2. Ended
  3. Cancelled
  4. Moved

For a touch event.phase == “ended” could confirm that the touch event has been completed by the user…You will have to put ur execution logic within the “ended” block in touch event declaration…

local listener = function(event)  
 if(event.phase == "ended") then  
 -- do something.  
 end  
end  
  

Would strongly suggest that you spent sometime reading about the events. Would make ur job much easier as you move forward with your project.

Hope this helps. [import]uid: 84539 topic_id: 20469 reply_id: 81959[/import]