Firstly, can you please ensure that your code is formatted correctly when posting? You have pasted one long sentence of code, instead of this:
local stick = {} local count = 0 for i = 1, 3 do stick[i] = display.newImage( "stick.png" ) stick[i].x=150 + (i\*50) end local function changeP() count=count + 1 print(count) end local function selectStick(event) target=event.target transition.to( target, { time=1000, delay=100, alpha=1.0, x=200 , y=300, onComplete=changeP} ) end for i = 1, 3 do stick[i]:addEventListener("touch", selectStick) end
To answer your question, your count is increasing more than once because the “touch” event has multiple “phases” which are called depending on your actions:
So when you touch and release the object without moving, you call 2 of those phases “began” and “ended”. If you touch, move and then release, then you call all 3 phases.
To fix your problem in the short term, change your touch function as so:
local function selectStick(event) if event.phase == "ended" then target=event.target transition.to( target, { time=1000, delay=100, alpha=1.0, x=200 , y=300, onComplete=changeP} ) end end
I suggest looking at the event detection guide to help you understand how this all works:
http://docs.coronalabs.com/guide/events/detectEvents/index.html