what is my wrong?

Hi all,

I want increase my count variable when click to stick. But i touching once time but count increasing two or third times… (i am seeing print values)

What is my fault??  :frowning:

[lua]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
[/lua]

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:

  • began
  • moved
  • ended

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

Hi  AlanPlantPot,

Thank you very much your answer. I cant paste the code multi line and i didnt understand my fault.

I use BBCode (Lua Code) but my code pasted 1 row  :frowning:

Thank you again your answer…

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:

  • began
  • moved
  • ended

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

Hi  AlanPlantPot,

Thank you very much your answer. I cant paste the code multi line and i didnt understand my fault.

I use BBCode (Lua Code) but my code pasted 1 row  :frowning:

Thank you again your answer…