add/remove event listener chicken/egg scenario

Hi All,

I promise, this is the last post on this puzzle I am building.

So I scrapped the physics route and I am working on a logic/math approach for managing the sliding of the key, past bricks and out of the game.
 

My objective is to prevent the key from being dragged too far upwards, so I essentially want to stop the drag event.

When the key reaches the edge of the grid, regardless if player is still dragging (touching).

I want something else to prevent the dragging motion.

My only semi-workable solution is to currently remove the Event Listener.

But this is permanent, and then stops me from re-adding it as Lua is linear code and not OO, so reads down the code, and invokes/actions on the way back up.

So I’ve no way to recall back down the code.

There are no pause/re-start abilities with event listeners.

So now I am stuck.

Is there a way to then re-invoke the event listener?
or am I still using the wrong approach.

Is there a way to force the event.phase to cancel other then with the release of the mouse/finger touch being listed off of the device? But if there is a way I can not find it.

 local function dragKey(event) itemKey = event.target local phase = event.phase if ( "began" == phase ) then display.currentStage:setFocus( itemKey ) itemKey.touchOffsetY = event.y - itemKey.y elseif ( "moved" == phase ) then itemKey.y = event.y - itemKey.touchOffsetY itemKey.topEdge = itemKey.y - itemKey.height/2 if (itemKey.topEdge \<= bounds.yMin ) then print("key reached top edge of grid") itemKey:removeEventListener( "touch", dragKey ) elseif (itemKey.topEdge \>= bounds.yMax ) then puzzleCompleted() end elseif ( "ended" == phase or "cancelled" == phase ) then display.currentStage:setFocus( nil ) end -- itemKey:addEventListener( "touch", dragKey ) return true -- Prevents touch propagation to underlying objects end

Again thanks in advance.
Angela

Don’t worry, I suddenly stumbled on the solution…
During the “MOVED PHASE” I needed to ADD a switch to test against, thus if TRUE, then write the code that drags the key. Else if MOVED AND FALSE do nothing. This now allows me to drag the key up and down, stop it when it reaches the edge and then redrag it! 
 

 local function dragKey(event) itemKey = event.target local phase = event.phase if ( "began" == phase ) then display.currentStage:setFocus( itemKey ) itemKey.touchOffsetY = event.y - itemKey.y itemKey.isFocus = true elseif ( "moved" == phase and itemKey.isFocus == true ) then itemKey.y = event.y - itemKey.touchOffsetY itemKey.topEdge = itemKey.y - itemKey.height/2 if (itemKey.topEdge \<= bounds.yMin and itemKey.isFocus == true ) then print("key reached top edge of grid and has focus") itemKey.isFocus = false elseif (itemKey.topEdge \>= bounds.yMax ) then puzzleCompleted() end elseif ( "ended" == phase or "cancelled" == phase ) then display.currentStage:setFocus( nil ) end return true -- Prevents touch propagation to underlying objects end