2

我如何使用事件来找出手指何时离开图像区域?例如,当您解锁 iphone,但将手指移离滑块太远(触摸并移动一点后 - 手指仍在触摸屏幕,但未触摸滑块),它会跳回到开头。当我在 if 语句中使用 event.phase=="ended" 时,图像不会返回到指定位置,除非我在手指仍然“在”图像上时松开屏幕。基本上,当手指离开图像区域时,如何将图像返回到某个点?

4

2 回答 2

0

您需要将图像设置为焦点,即使在他们的手指离开对象之后,触摸事件也会继续在对象上触发。

这是我使用的示例代码:

local function switchScreenListener(event)      
    display.getCurrentStage():setFocus( event.target )

    if event.phase == "moved" then      
        local xBoundry = event.target.x + event.target.width/2 -- remember the reference point!
        print ("X Boundry: " .. xBoundry .. ", Current X: " .. event.x);
        if event.x > xBoundry then
            print ("We swiped out.")
            display.getCurrentStage():setFocus( nil )
        end
    elseif event.phase == "ended" then
        display.getCurrentStage():setFocus( nil )
        print ("Start: (" .. event.xStart .. ", " .. event.yStart .. "), End: (" .. event.x .. ", " .. event.y .. ")");

        local options = {
            effect = "slideRight",
            params = {
                isMuted = isMuted
            }
        }
        storyboard.gotoScene( "view_alphabet", options )
    end
end
于 2013-01-15T02:53:24.453 回答
0

您需要指定触摸事件的像素范围。如果触摸事件超出此像素范围,则重置图片。

所以在你的触摸事件中做这样的事情,触摸的范围是 x = 320 -> 480 和 y = 80 -> 280

local function onTouch(event)    
 local t = event.target    
 if (t.x < 320 || t.x > 480) || (t.y > 280 || t.y < 80) then 
      //reset image
end

//你也可以试试

local function onTouch(event)    
 local t = event.target
 local phase = event.phase
 if phase == "moved" then
  t.x = event.x - t.x0
  t.y = event.y = t.y0
  if (t.x < 320 || t.x > 480) || (t.y > 280 || t.y < 80) then
     //reset image
  end
 end
 return true
end

objectName:addEventListener("touch", onTouch)
于 2012-07-20T15:59:22.260 回答