[Resolved] How do I keep my function from running twice in the "ended" or "submitted" phase?

I know I must be doing something silly but no matter what I try I can’t seem to get the function to only run once when the user enters the “ended” or “submitted” phase. I also experimented by completely removing them from the same line and doing if/ elseif/ end but I got the same results. Any input? Here’s the skeleton of my code:

[lua]local function objectFunction (event)
if ( “began” == event.phase) then
transition.to (object, {time = 1000, x = display.contentWidth/2 , y = display.contentHeight/2})

end

if ( “ended” == event.phase) or (“submitted” == event.phase) then
print (“ended or submitted”)

transition.to (object, {time = 1000, x = event.target.originalxLocation , y = event.target.originalyLocation})

native.setKeyboardFocus( nil )
end

end

object:addEventListener (“userInput”, objectFunction)[/lua]
Thanks in advance! [import]uid: 35535 topic_id: 33278 reply_id: 333278[/import]

Hi there.

When you say you can’t get it to run only once, what do you mean exactly? For example, does the terminal output show your print statement “ended or submitted” multiple times in a row?

  • Andrew [import]uid: 109711 topic_id: 33278 reply_id: 132220[/import]

I don’t think this solves the cause of your problem, but it should stop the effect:

[lua]local hasSubmitted = false
local function objectFunction (event)
if ( “began” == event.phase) then
transition.to (object, {time = 1000, x = display.contentWidth/2 , y = display.contentHeight/2})
end

if (( “ended” == event.phase) or (“submitted” == event.phase)) and not hasSubmitted then
print (“ended or submitted”)
transition.to (object, {time = 1000, x = event.target.originalxLocation , y = event.target.originalyLocation})
native.setKeyboardFocus( nil )
hasSubmitted = true
timer.performWithDelay(1000,function() hasSubmitted = false end,1)
end
end

object:addEventListener (“userInput”, objectFunction)[/lua] [import]uid: 8271 topic_id: 33278 reply_id: 132224[/import]

Have you read: http://docs.coronalabs.com/api/event/userInput/phase.html

It states that the TextField object will fire a “submitted” even when the user presses the return key. It also says that this does not happen with TextBox objects. [import]uid: 8271 topic_id: 33278 reply_id: 132225[/import]

Good response @horacebury [import]uid: 19626 topic_id: 33278 reply_id: 132241[/import]

Thanks, I worked off your idea and added a true/false statement and it works great! [import]uid: 35535 topic_id: 33278 reply_id: 132263[/import]

Hi there.

When you say you can’t get it to run only once, what do you mean exactly? For example, does the terminal output show your print statement “ended or submitted” multiple times in a row?

  • Andrew [import]uid: 109711 topic_id: 33278 reply_id: 132220[/import]

I don’t think this solves the cause of your problem, but it should stop the effect:

[lua]local hasSubmitted = false
local function objectFunction (event)
if ( “began” == event.phase) then
transition.to (object, {time = 1000, x = display.contentWidth/2 , y = display.contentHeight/2})
end

if (( “ended” == event.phase) or (“submitted” == event.phase)) and not hasSubmitted then
print (“ended or submitted”)
transition.to (object, {time = 1000, x = event.target.originalxLocation , y = event.target.originalyLocation})
native.setKeyboardFocus( nil )
hasSubmitted = true
timer.performWithDelay(1000,function() hasSubmitted = false end,1)
end
end

object:addEventListener (“userInput”, objectFunction)[/lua] [import]uid: 8271 topic_id: 33278 reply_id: 132224[/import]

Have you read: http://docs.coronalabs.com/api/event/userInput/phase.html

It states that the TextField object will fire a “submitted” even when the user presses the return key. It also says that this does not happen with TextBox objects. [import]uid: 8271 topic_id: 33278 reply_id: 132225[/import]

Good response @horacebury [import]uid: 19626 topic_id: 33278 reply_id: 132241[/import]

Thanks, I worked off your idea and added a true/false statement and it works great! [import]uid: 35535 topic_id: 33278 reply_id: 132263[/import]