An event gets triggered when object is in range

Let say I have an object(square) and I can move it up and down. Then there is another object(rectangle) at the bottom-side.

Diagram below:

When the square is in the green shaded area, you are then taken to another scene.

How can I do this in Corona.

I have tried to find ways to do this but I couldn’t find anything online.

Here is my code:

----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- -- Your code here 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)

https://docs.coronalabs.com/api/library/composer/index.html

I know how to do the composer part but I do not know how to make it go to the scene when the square is within the the green area. I added my code for anyone to try out.

I’m not certain if this is the part you’re having a problem with but you would need to write a small amount of collision detection code I think.

I would try something like this. I believe that the Y anchor point will affect where the collision is detected so if you set that to 0 it will detect it when it first makes contact.

You’ll need an event listener to keep calling this function to check for that collision and make sure you destroy it after changing scenes or it remains in memory. I think you can also call the function each time you move the red square but it would depend on how your movement works.

[lua]local function checkCollision()

  if redSquare.y >= greenRect.y and redSquare.y <= greenRect.height + greenRect.y then

    print(“Colliding”)

    composer.gotoScene( “sceneName” )

  end

end

[/lua]

ahh i missunderstood the problem.

collision can be done like infinite.replay says, by y coordinate comparison and logic, or physic if you prefer though i suspect you are more interested in the first option.

i’m sure there are more clever ways to do it.

I tried your code and it didnt work. I didnt have the green shaded box before so I added it in.

Here is my code:

----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- -- Your code here 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 transition.to( square, {x=display.contentCenterX, y=display.contentCenterY, onComplete=listener1 } ) local function checkCollision() if square.y \>= shade.y and square.y \<= shade.height + shade.y then print("Colliding") composer.gotoScene("scene1") end end shade:addEventListener("collision", checkCollision)

Any idea what I did wrong here.

The event listener should be written like this and I think it will work better.

[lua]Runtime:addEventListener(“enterFrame”, checkCollision)[/lua]

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.

https://docs.coronalabs.com/api/library/composer/index.html

I know how to do the composer part but I do not know how to make it go to the scene when the square is within the the green area. I added my code for anyone to try out.

I’m not certain if this is the part you’re having a problem with but you would need to write a small amount of collision detection code I think.

I would try something like this. I believe that the Y anchor point will affect where the collision is detected so if you set that to 0 it will detect it when it first makes contact.

You’ll need an event listener to keep calling this function to check for that collision and make sure you destroy it after changing scenes or it remains in memory. I think you can also call the function each time you move the red square but it would depend on how your movement works.

[lua]local function checkCollision()

  if redSquare.y >= greenRect.y and redSquare.y <= greenRect.height + greenRect.y then

    print(“Colliding”)

    composer.gotoScene( “sceneName” )

  end

end

[/lua]

ahh i missunderstood the problem.

collision can be done like infinite.replay says, by y coordinate comparison and logic, or physic if you prefer though i suspect you are more interested in the first option.

i’m sure there are more clever ways to do it.

I tried your code and it didnt work. I didnt have the green shaded box before so I added it in.

Here is my code:

----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- -- Your code here 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 transition.to( square, {x=display.contentCenterX, y=display.contentCenterY, onComplete=listener1 } ) local function checkCollision() if square.y \>= shade.y and square.y \<= shade.height + shade.y then print("Colliding") composer.gotoScene("scene1") end end shade:addEventListener("collision", checkCollision)

Any idea what I did wrong here.

The event listener should be written like this and I think it will work better.

[lua]Runtime:addEventListener(“enterFrame”, checkCollision)[/lua]