两个独立的侦听器用于相同的对象Corona

时间:2013-08-01 11:11:23

标签: events listener corona drag slide

我创建了一个显示组并在其中放入了一些项目并添加了一个用于滑动功能的监听器,该功能可以滑动显示组以到达屏幕外的项目并返回。我还想为那些调用拖动项目的函数的项添加一个触摸监听器。那么如何在不干扰这些听众的情况下实现这一目标呢?

这是图片:

enter image description here

enter image description here

我将所有这些圈子和数字放在蓝色矩形上的显示组中,我为该组添加了一个触摸事件监听器:

ballGroup.touch=slide
ballGroup:addEventListener("touch", ballGroup) 

我还希望能够为球添加另一个触摸事件监听器:

function createBall()

        local ball = display.newCircle(x, H/2-25, 50)
        local label = display.newText(""..count, ball.x, ball.y, nil , 35)
        label:setTextColor ( 255, 0, 0 )
        ball.no = count
        ball.touch=chooseBall
        ball:addEventListener("touch", ball)
        ballGroup:insert(ball)
        ballGroup:insert(label)
        count=count+1
        x=x+120
end

然而,它只是在监听我先写的函数的事件。你有什么建议我实现我想要的?当我尝试滑动球时,我只是想让它听滑动事件,当我试图拖动球时,我希望它能够听到拖动事件。我怎样才能做到这一点?

Okey,我在Rob的建议之后分享了我提出的整个代码,但它仍然无效,Outlaw IDE给出了错误:

尝试对x0(nil值)执行算术,并且该行是滑动函数中移动的相位。

以下是整个代码:

W=display.contentWidth
H=display.contentHeight
local ballGroup = display.newGroup()--balls and numbers will be added
local x=50 --for ball's locating
local count=1 -- little ball's number starting from 1


local rect --background rect for balls
--big circle at the bottom
local circle = display.newCircle(W/2, H-90, 70) 
local circleTxt = display.newText("", 0, 0, nil, 50 )
circleTxt:setTextColor ( 255, 0, 0 )
circleTxt.x=circle.x; circleTxt.y = circle.y


--Dragging ball and checking if  it is inside big circle if it is so, remove ball and   show the number of ball on big circle
function dragBall(self, event)
if event.phase=="began" then
    display.getCurrentStage ( ):setFocus(self,  event.id)
    self.isFocus=true
    self.x0= self.x; self.y0=self.y
elseif event.phase=="moved" then
        local dx = math.abs( event.x - event.xStart ) -- Get the x-  transition of the touch-input
        local dy = math.abs( event.y - event.yStart ) -- Get the y-transition of the touch-input
        if dy < 5 then --I changed it to less than, because if  y is bigger,then focus should stay on the ball which will be dragged 
            display.getCurrentStage():setFocus( nil )
            event.target.isFocus = false
            return false
        end           
        self.x = self.x0+(event.x-event.xStart); self.y = self.y0+(event.y-event.yStart) --drag ball
elseif event.phase=="cancelled" or event.phase=="ended" then
        checkArea(self)
        display.getCurrentStage():setFocus(self,nil)

end             
return true
end


function createBall()
local ball = display.newCircle(x, H/2-25, 50)
local label = display.newText(""..count, ball.x, ball.y, nil , 35)
label:setTextColor ( 255, 0, 0 )
ball.no = count
ball.touch=dragBall
ball:addEventListener("touch", ball)
ballGroup:insert(ball)
ballGroup:insert(label)
count=count+1
x=x+120
end


for i=1,8 do
createBall()
end


rect = display.newRect(0,0, ballGroup.width, ballGroup.height); rect.y=H/2-25
rect:setFillColor(0,0,255)
rect:toBack()

function slide(self, event)

if event.phase=="began"  then
    self.x0=self.x 
    self.y0=self.y
    display.getCurrentStage():setFocus(self, event.id)
    self.isFocus=true
elseif event.phase=="moved" then
    local dif = event.x-event.xStart

    self.x = self.x0+dif
    if ballGroup.contentBounds.xMax < W then
        ballGroup.x = ballGroup.x+(W-ballGroup.contentBounds.xMax)
    elseif ballGroup.contentBounds.xMin > 0 then
        ballGroup.x = 0
    end
elseif event.phase=="cancelled" or event.phase=="ended" then
    display.getCurrentStage():setFocus(nil)
    self.isFocus=false 
end
return true 
end

ballGroup.touch=slide
ballGroup:addEventListener("touch", ballGroup)

local bounds = circle.contentBounds
local xMax = bounds.xMax
local xMin = bounds.xMin
local yMax = bounds.yMax
local yMin = bounds.yMin

function checkArea(self)    
    if self.x>xMin and self.x<xMax and self.y>yMin and self.y<yMax then
        circleTxt.text=""..self.no
        self:removeSelf()
        self=nil
    end 

end

2 个答案:

答案 0 :(得分:2)

我的解决方案是运行时触摸列表:

    if event.phase == "began" then
        startx = event.x

    elseif event.phase == "ended" then

        local endx = event.x
        result_postion = endx-startx
            if result_postion >50 then
                 print("right swipe")
            elseif result_postion <50 then
                  print("left swipe")
            end
     end

对象(球)触摸功能

local function ball(event)
if event.phase == "began" then
startpos = event.x
display.getCurrentStage():setFocus(event.target)
elseif event.phase == "moved" then
endpos = event.x
result = endpos-startpos
if result<30 and result>=-30 then
print("correct")
    end
    end
end

答案 1 :(得分:0)

您可以在滚动区域上有一个监听器,在圆圈上有一个监听器。圆圈的处理程序需要测试以查看event.phase是否被“移动”,如果你移动说超过5px,那么你想要将焦点放在圆圈上并返回false,让事件传播到潜在的对象。

        elseif event.phase == "moved" then -- Check if you moved your finger while touching
            local dx = math.abs( event.x - event.xStart ) -- Get the x-transition of the touch-input
            local dy = math.abs( event.y - event.yStart ) -- Get the y-transition of the touch-input
            if dx > 5 or dy > 5 then
                display.getCurrentStage():setFocus( nil )
                event.target.isFocus = false
                return false
            end
        end

或类似的东西。