我創建了一個顯示組,並在其中放入了一些項目,並添加了一個滑動功能的偵聽器,用於滑動顯示組以觸及屏幕外的項目並返回。我還想爲那些調用拖動項目的函數的項目添加一個觸摸偵聽器。那麼我怎樣才能做到這一點,而不會干擾彼此的聽衆?兩個獨立的同一個對象的偵聽器Corona
這裏是圖:
我把所有這些圈子和號碼的藍色矩形顯示組,我增加了一個觸摸事件偵聽器,該組:
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
但是,它只是監聽我先寫的函數的事件。你有什麼建議我實現我想要的?當我嘗試滑動球時,我只想讓它聽到滑動事件,當我嘗試拖動球時,我想讓它聽着拖動事件。我怎樣才能做到這一點?
歐凱,我分享,我搶的建議後,想出了整個代碼,但它仍然沒有工作和奧特洛IDE給出了錯誤:
嘗試執行對X0(零值)的算術和線是移動相位在滑動功能中的位置。
這裏是整個代碼:
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
可能很難,但試圖進一步描述它。 Paint a pic – Eyeball
在同一時間滑動和拖動它們似乎不太合適。程序將如何決定,何時滑動以及何時拖動?他們是非常相似的動作。你打算在不同的應用階段使用這個動作嗎? –