2013-08-01 63 views
1

我創建了一個顯示組,並在其中放入了一些項目,並添加了一個滑動功能的偵聽器,用於滑動顯示組以觸及屏幕外的項目並返回。我還想爲那些調用拖動項目的函數的項目添加一個觸摸偵聽器。那麼我怎樣才能做到這一點,而不會干擾彼此的聽衆?兩個獨立的同一個對象的偵聽器Corona

這裏是圖:

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 

但是,它只是監聽我先寫的函數的事件。你有什麼建議我實現我想要的?當我嘗試滑動球時,我只想讓它聽到滑動事件,當我嘗試拖動球時,我想讓它聽着拖動事件。我怎樣才能做到這一點?

歐凱,我分享,我搶的建議後,想出了整個代碼,但它仍然沒有工作和奧特洛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 
+0

可能很難,但試圖進一步描述它。 Paint a pic – Eyeball

+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 
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 

或類似的東西。

+0

羅布,我試着做你說的話,但它仍然不起作用。我發佈了整個代碼。你能再看一遍嗎? –

+0

我不確定哪組x0導致你的錯誤。 但這一行:display.getCurrentStage()的setFocus(無) 大概應該是:display.getCurrentStage()的setFocus(個體經營,零)的基礎上,你是如何調用你的觸摸處理 。 –

+0

我不確定,但我認爲,在幻燈片函數中,x0沒有在開始階段分配,因爲使用dragBall函數我已經開始,當它返回false時,我仍然在移動對象,所以也許幻燈片已經從移動的階段開始,這就是爲什麼x0在幻燈片功能中無效。你怎麼看?附:我也更正了setFocus(),但它仍然給出相同的錯誤。 –

相關問題