Well I’m trying your suggestion but I’m not sure I’ve got the counter working right. Here’s a simple example where I’ve made a rectangle be a sensor object with a crate being dragged into the sensor region. But my counter isn’t working. Please advice.
local physics = require(“physics”)
physics.start()
display.setStatusBar( display.HiddenStatusBar )
local counter = 0
– A basic function for dragging physics objects
local function startDrag( event )
local t = event.target
local phase = event.phase
if “began” == phase then
counter = counter + 1
display.getCurrentStage():setFocus( t )
t.isFocus = true
– Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y
– Stop current motion, if any
event.target:setLinearVelocity( 0, 0 )
event.target.angularVelocity = 0
elseif t.isFocus then
if “moved” == phase then
t.x = event.x - t.x0
t.y = event.y - t.y0
elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
counter = counter - 1
end
end
print (counter)
– Stop further propagation of touch event!
return true
end
local crate = display.newImage( “crate.png” )
crate.x = 90; crate.y = 90
physics.addBody( crate, “kinematic”, { friction=0.7} )
local rect = display.newRect( 50, 50, 100, 100 )
rect.x = 150; rect.y = 200
rect:setFillColor( 255, 255, 255, 100 )
rect.isVisible = true
physics.addBody( rect, “kinematic”, { friction=0.7, isSensor = true } )
crate:addEventListener( “touch”, startDrag ) [import]uid: 168506 topic_id: 24523 reply_id: 121597[/import]