Corona 触摸监听器

Corona Touch Listener

我正在学习corona sdk 这是下面的代码 当两个对象重叠时,它变成了 1 个对象(一起移动),我不知道发生了什么...

-----------------------------------------------------------------------------------------
--
-- 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 )

请参考Corona SDK文档。 https://docs.coronalabs.com/daily/guide/events/detectEvents/index.html#TOC

部分:了解命中事件

一般来说,您应该阅读有关您使用的任何内容的文档。 您的触摸事件将通过与事件坐标相交的所有显示对象传播。该事件将触发所有已注册的事件侦听器。

为了避免有多个侦听器触发您的事件侦听器 bodyTouch 必须 return true 这将告诉 Corona 您处理了该事件并且不应调用更多的侦听器。