In my game, the player users their finger to drag the character around. I want it so that when the player is hit by an enemy, they are no longer controllable and fall off the stage.
I can’t seem to get it to work however. When I remove the event listener from the player object, the player just stays still, instead of being affected by gravity forces (I have tried setting the player to dynamic but it did not help).
Alternatively, I don’t remove the event listener, I use the “isDragAllowed” flag to detect when the collision happens (it’s set to false when player collides with enemy). This produces the desired effect (player object is released from the drag and falls off the stage due to gravity) if the user is dragging the player around, but doesn’t work if the user is holding the player still (player object stays still, until user tries to move again).
How do I make it work so that in the moment the player is hit by an enemy, the touch/drag listener on him stops?
This is my drag handler:
isDragAllowed = true local function handleDrag( event ) local t = event.target local phase = event.phase if "began" == phase then isDragAllowed = true display.getCurrentStage():setFocus( t ) --t.isFocus = true -- Store initial position t.x0 = event.x - t.x t.y0 = event.y - t.y -- Make body type temporarily "kinematic" (to avoid gravitional forces) event.target.bodyType = "kinematic" event.target.isSensor = true -- Stop current motion, if any event.target:setLinearVelocity( 0, 0 ) event.target.angularVelocity = 0 end --elseif t.isFocus then if "moved" == phase then t.x = event.x - t.x0 t.y = event.y - t.y0 end if "ended" == phase or "cancelled" == phase or isDragAllowed == false then display.getCurrentStage():setFocus( nil ) --t.isFocus = false t.bodyType = "dynamic" t.isSensor = false isDragAllowed = true end --end -- Stop further propagation of touch event! return true end