silentsudo
silentsudo

Reputation: 6963

Corona Touch Listener

I am learning corona sdk This is the following code when the two objects overlaps it becomes 1 objects(Moves together), i have no clue whats going on...

-----------------------------------------------------------------------------------------
--
-- main.lua
--
-----------------------------------------------------------------------------------------

-- Your code here
local physics = require( "physics" )
physics.start()

local crate1 = display.newImage( "crate.png", display.contentCenterX, 100 )
crate1.name = "crate1"
local crate2 = display.newImage( "crate.png", display.contentCenterX + 100, 100 )
crate2.name = "crate2"
crate1:scale(0.2, 0.2)
crate2:scale(0.2, 0.2)

local bodyTouched = function(event)

            if(crate1.name == event.target.name and event.phase == "moved")
                then
                    print("Moved " , event.target.name )
                    crate1.x = event.x
                    crate1.y = event.y
            elseif (crate2.name == event.target.name and event.phase == "moved")
                then
                    print( "Moved ", event.target.name )
                    crate2.x = event.x
                    crate2.y = event.y
            end 


end 



physics.addBody( crate1, "static")
physics.addBody( crate2, "static")

crate1:addEventListener( "touch", bodyTouched )
crate2:addEventListener( "touch", bodyTouched )

Upvotes: 0

Views: 61

Answers (1)

Piglet
Piglet

Reputation: 28950

Please refer to the Corona SDK documentation. https://docs.coronalabs.com/daily/guide/events/detectEvents/index.html#TOC

Section: Understanding Hit Events

In general you should read the documentation on anything you use. Your touch event will propagate through all display objects that intersect with the event coordinate. The event will trigger all registered event listeners on its way.

To avoid having multiple listeners triggered your event listener bodyTouch has to return true which will tell Corona that you handled the event and no further listeners should be invoked.

Upvotes: 2

Related Questions