我创建了一个显示组并将一些项目放入其中,并添加了一个用于滑动功能的侦听器,该功能可以滑动显示组以到达屏幕外的项目并返回。我还想为那些调用拖动项目的函数的项目添加一个触摸监听器。那么如何在不干扰这些听众的情况下实现这一目标呢?
这是图片:
我将所有这些圆圈和数字放在蓝色矩形上的显示组中,并为该组添加了一个触摸事件侦听器:
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
但是,它只是在监听我首先编写的函数的事件。你建议我做什么来实现我想要的?当我尝试滑动球时,我只想让它监听滑动事件,当我尝试拖动球时,我希望它监听拖动事件。我怎样才能做到这一点?
好的,我正在分享我在 Rob 的建议之后提出的整个代码,但它仍然无法正常工作,并且 Outlaw IDE 给出了该错误:
尝试对 x0(nil value) 执行算术运算,该行是移动相位在滑动函数中的位置。
这是整个代码:
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