2014-11-20 36 views
0

我有一個滾動按鈕時,scrollView的問題。問題在於事件處理不當。所有試圖將焦點傳遞給scrollView的嘗試都是徒勞的。我所做的一個觀察是,在下面的代碼中,它不會被打印回給我,即使在我按照正確的方式正確滾動之後,也是如此。電暈按鈕沒有通過焦點滾動查看

local function scrollListener(event) 

local phase = event.phase 
if (phase == "began") then print("Scroll view was touched") 
elseif (phase == "moved") then print("Scroll view was moved") 
elseif (phase == "ended") then print("Scroll view was released") 
end 

-- In the event a scroll limit is reached... 
if (event.limitReached) then 
    if (event.direction == "up") then print("Reached top limit") 
    elseif (event.direction == "down") then print("Reached bottom limit") 
    elseif (event.direction == "left") then print("Reached left limit") 
    elseif (event.direction == "right") then print("Reached right limit") 
    end 
end 

return true 

這裏被用於場景的實際代碼:

功能場景:enterScene(事件)

--local yell=audio.play(ahhh, {channel=1, loops=0, fadein=0}) 

--Creating the Background 
local background=display.newImageRect("background.png", 320, 580) 
background.x=160 
background.y=240 


--Creating the scroll view 
local scrollView = widget.newScrollView 
{ 
    top = 10, 
    left = 10, 
    width = 300, 
    height = 500, 
    scrollWidth = 300, 
    scrollHeight = 500, 
    horizontalScrollDisabled = true, 
    listener = scrollListener 
} 

local button = {} 


local yCount = 0 


for i = 1 , 11 do 
    button[i] = widget.newButton{ 
     label = "Button " .. i, 
     left = 0, 
     id = button[i], 
     top = yCount, 
     width = 300, 
     height = 100, 
     defaultFile = "menuButton.png", 
     onEvent = handleButtonEvent 
    } 

    yCount = yCount + 100 
    scrollView:insert(button[i]) 

end 



--local background=display.newRect(160, 270, 320, 510) 
--scrollView:insert(background) 

local menu=display.newImageRect("menu2.png", 90, 50) 
menu.x=50 
menu.y=-15 



function menu:touch(event) 
    if event.phase=="began" then 
     --local illBeBack=audio.play(terminator,{channel=1, loops=0, fadein=0}) 
     display.remove(menu) 
     display.remove(taskBar) 
     display.remove(background) 

     taskBar = nil 
     background = nil 
     menu = nil 
     storyboard.gotoScene("menu") 
    end 
    return true 
end 
menu:addEventListener("touch", menu) 

- - 巴頓事件H andler

本地函數handleButtonEvent(事件)

local phase = event.phase 

if (phase == "moved") then 
    local dy = math.abs((event.y - event.yStart)) 
    -- If the touch on the button has moved more than 10 pixels, 
    -- pass focus back to the scroll view so it can continue scrolling 
    if (dy > 10) then 
     scrollView:takeFocus(event) 
    end 
end 
return true 

回答

0

問題解決了通過刪除 '本地'從'handleButtonEvent'和'scrollListener'函數,以及從'scrollView'部件刪除'local'

因此看來,在corona或者至少我所做的程序中,全局變量必須比他們應該使用的要多得多,除非別人有解決方案。

0

嘗試傳遞event.phase到滾動視圖這樣的:

scrollView:takeFocus(event.phase) 
+0

不工作,仍然有像以前一樣的問題,也不會只發送階段,而不是整個事件? – MasterNoda 2014-11-21 17:06:28