Corona Touch Listener

时间:2016-06-25 10:43:07

标签: lua 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 )

1 个答案:

答案 0 :(得分:2)

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

部分:了解命中事件

一般情况下,您应阅读有关您使用的任何内容的文档。 触摸事件将传播到与事件坐标相交的所有显示对象。该事件将在途中触发所有已注册的事件监听器。

为避免触发多个侦听器,您的事件侦听器bodyTouch必须返回true,这将告诉Corona您处理了该事件,并且不应再调用其他侦听器。

相关问题