在Corona上调用touch listener时如何获取对象的特定部分
我有一个团队,它展现了一个角色。它包含头部、手臂和身体的其他部分。 我想把touch Listener设置为该组,这样当我触摸头部时,手臂和身体会做一些动作。 我不想只将touch listener设置为head,而是将整个组设置为head。 我将name设置为head,并希望event.other可以工作(比如碰撞),但事实并非如此 你有什么解决办法吗? 下面是我的代码在Corona上调用touch listener时如何获取对象的特定部分,listener,coronasdk,Listener,Coronasdk,我有一个团队,它展现了一个角色。它包含头部、手臂和身体的其他部分。 我想把touch Listener设置为该组,这样当我触摸头部时,手臂和身体会做一些动作。 我不想只将touch listener设置为head,而是将整个组设置为head。 我将name设置为head,并希望event.other可以工作(比如碰撞),但事实并非如此 你有什么解决办法吗? 下面是我的代码 local touchListener= function( event ) if (event.phase
local touchListener= function( event )
if (event.phase=="began") then
local group = event.target
local head= group[1]
local arms= group[2]
local body= group[3]
if( event.other.name == "head" ) then
// do something here
end
end
return true
end
你应该首先创造身体的那些特殊部位。然后你应该给每个人都添加触控听众。例如,您有名为“头”、“臂”和“体”的对象
函数边界内(事件)
本地边界=event.target.contentBounds
如果event.x>bounds.xMin和event.xbounds.yMin和event.y
此代码还将查找对象的边界。例如,如果用户触摸屏幕,然后将手指移到对象外部,则对象将不再聚焦。
干杯^你想在每次触球时都有不同的动作吗?例如,头部的运动与手臂不同,或者所有手臂都是相同的?我想让程序准确地知道我触摸的位置:头部。因为我将listener设置为整个组(角色),而不是该组的每个元素(头部、手臂…),所以我很难得到我想要的部分。
head.id = "head"
arms.id = "arms"
body.id = "body"
head:addEventListener( "touch", touchHandler )
arms:addEventListener( "touch", touchHandler )
body:addEventListener( "touch", touchHandler )
-- Touch function here
local function touchHandler( event )
if event.phase == "began" then
display.getCurrentStage():setFocus( event.target )
event.target.isFocus = true
elseif event.target.isFocus then
if event.phase == "moved" and not ( inBounds( event ) ) then
display.getCurrentStage():setFocus( nil )
event.target.isFocus = false
elseif event.phase == "ended" then
if event.id == "head" then
-- Do stuff for head
elseif event.id == "arms" then
-- Do stuff for arms
elseif event.id == "body" then
-- Do stuff for body
end
display.getCurrentStage():setFocus( nil )
event.target.isFocus = false
end
end
return true
end
function inBounds( event )
local bounds = event.target.contentBounds
if event.x > bounds.xMin and event.x < bounds.xMax and event.y > bounds.yMin and event.y < bounds.yMax then
return true
else
return false
end
end