An event gets triggered when object is in range

It still didnt work

Hmm that’s odd, I ran the code myself replacing the event listener with the new one and the collision detection seemed to work fine. Is this code part of a composer scene which could be causing other issues with how it was setup?

It doesn’t look like you are using physics and I think that collision code is used with physics engine.  I never use the physics engine so I cannot be sure, but here is one way to do it, not using physics.  (using your code with a few changes)

local composer = require ( "composer" ) local physics = require("physics") local objTrans physics.start() local function point\_in\_rect( pointX, pointY, img ) local left = img.x - (img.width \* .5) local top = img.y - (img.height \* .5) if pointX \>= left and pointX \<= left + img.width and pointY \>= top and pointY \<= top + img.height then return true else return false end end local square = display.newRect(display.contentCenterX,126,55,50) square:setFillColor(1,0,0) local rectangle = display.newRect(17,270,39,141) rectangle:setFillColor(0,0,1) local shade = display.newRect(display.contentCenterX,rectangle.y,display.actualContentWidth-60,rectangle.height) shade:setFillColor( 0.196078, 0.803922, 0.196078) local function listener1( obj ) print( "Transition 1 completed on object: " .. tostring( obj ) ) end objTrans = transition.to( square, {x=display.contentCenterX, y=display.contentCenterY}) --, onComplete=listener1 } ) local function gameLoop() if point\_in\_rect(square.x, square.y, shade) then print("Colliding") -- MUST END (REMOVE) THE GAMELOOP EVENT LISTENER transition.cancel(objTrans) Runtime:removeEventListener("enterFrame", gameLoop) composer.gotoScene("scene1") end end Runtime:addEventListener("enterFrame", gameLoop)

Hope it is helpful

Bob

remove the 2 lines about physics… I had them in when testing your code.  They are not needed in this example.

here it is with those 2 lines removed

local composer = require ( "composer" ) local objTrans local function point\_in\_rect( pointX, pointY, img ) local left = img.x - (img.width \* .5) local top = img.y - (img.height \* .5) if pointX \>= left and pointX \<= left + img.width and pointY \>= top and pointY \<= top + img.height then return true else return false end end local square = display.newRect(display.contentCenterX,126,55,50) square:setFillColor(1,0,0) local rectangle = display.newRect(17,270,39,141) rectangle:setFillColor(0,0,1) local shade = display.newRect(display.contentCenterX,rectangle.y,display.actualContentWidth-60,rectangle.height) shade:setFillColor( 0.196078, 0.803922, 0.196078) local function listener1( obj ) print( "Transition 1 completed on object: " .. tostring( obj ) ) end objTrans = transition.to( square, {x=display.contentCenterX, y=display.contentCenterY}) --, onComplete=listener1 } ) local function gameLoop() if point\_in\_rect(square.x, square.y, shade) then print("Colliding") -- MUST END (REMOVE) THE GAMELOOP EVENT LISTENER transition.cancel(objTrans) Runtime:removeEventListener("enterFrame", gameLoop) composer.gotoScene("scene1") end end Runtime:addEventListener("enterFrame", gameLoop)

A simplified version that is much more CPU efficient (i.e. only a single int compare in enterFrame() )

local square = display.newRect(display.contentCenterX,0,55,50) square:setFillColor(1,0,0) local rectangle = display.newRect(17,270,39,141) rectangle:setFillColor(0,0,1) local rectangleBounds = rectangle.contentBounds local objTrans = transition.to( square, {x=display.contentCenterX, y=display.contentCenterY}) local function gameLoop() if square.y \> rectangleBounds.yMin then print("Colliding") transition.cancel(objTrans) Runtime:removeEventListener("enterFrame", gameLoop) end end Runtime:addEventListener("enterFrame", gameLoop)

Sphere’s code works for me. I removed :

transition.cancel(objTrans)

because it would pause the square just when the collision is sensed.