Corona SDK新手教程:tap、touch和multitouch的区别

本教程主要讲解一下tap、touch和multitouch的区别,以及如何处理每种方式。

如果是新手的话,可以先复习一下之前的文章 CoronaSDK之交互系统和事件检测

1 Tap检测

Tap事件是用户和触屏之间交互最基础的一种。本质上来说,一个tap就是表示用户用手指接触到屏幕,然后在差不多附近的位置再抬起的过程。这个tap事件只有在用户在同一个点接触和放开屏幕,才作为tap事件成功的发生。

在Corona里,对于大部分display object,你可以注册一个tap事件监听器来监听tap事件:

local function myTapListener( event )

    --code executed when the button is tapped
    print( "object tapped = "..tostring(event.target) )  --'event.target' is the tapped object
    return true
end

local myButton = display.newRect( 100, 100, 200, 50 )
myButton:addEventListener( "tap", myTapListener )  --add a "tap" listener to the object

 

来自tap事件的event参数包括下列字段:

  • event.target:被触碰的对象(display object)引用
  • event.name:"tap"字符串
  • event.numTaps:屏幕上tap的个数。默认两次tap序列之间的延迟为0,但是这个事件可以通过system.setTapDelay()函数来调整。
  • event.x / event.y:在屏幕坐标系里,tap发生的位置的x和y

不像touch event,tap事件并不包括一个phase属性 -- 这个tap就是一个单一的动作,同时包括了接触和放开。所以你不需要以任何形式处理阶段信息(phase)。

1.1 过滤双击(double tap):

使用event.numTaps属性,你可以轻易确定一个对象是否被双击,并且区别在这个对象上的多次单击。

为了做到这一点,只要确保event.numTaps等于2,并且忽略其他所有情况(return true):

local function myTapListener( event )

    if ( event.numTaps == 2 ) then
        print( "object double-tapped = "..tostring(event.target) )
    else
        return true
    end
end

local myButton = display.newRect( 100, 100, 200, 50 )
myButton:addEventListener( "tap", myTapListener )

 

2 Touch检测

Touch事件提供了更高级的屏幕交互。通过touch事件,你可以检测到用户首次接触到屏幕,以及什么时候放开屏幕。你也可以跟踪用户手指在屏幕上的滑动轨迹。为了完成这个目标,Corona提供了event.phase属性的四种状态:

  • "began":表示手指刚接触到屏幕
  • "moved":表示手指正在屏幕上移动
  • "ended":表示手指刚从屏幕上放开
  • "cancelled":表示系统取消了对这次接触的跟踪(不要和ended混淆了)

在Corona里,你可以监听在大多数显示对象上注册的touch事件监听器:

local function myTouchListener( event )

    if ( event.phase == "began" ) then
        --code executed when the button is touched
        print( "object touched = "..tostring(event.target) )  --'event.target' is the touched object
    elseif ( event.phase == "moved" ) then
        --code executed when the touch is moved over the object
        print( "touch location in content coordinates = "..event.x..","..event.y )
    elseif ( event.phase == "ended" ) then
        --code executed when the touch lifts off the object
        print( "touch ended on object "..tostring(event.target) )
    end
    return true  --prevents touch propagation to underlying objects
end

local myButton = display.newRect( 100, 100, 200, 50 )
myButton:addEventListener( "touch", myTouchListener )  --add a "touch" listener to the object

这里event的属性包括下列字段:

  • event.id:一个唯一的标识,用来区别透过不同的touch事件传来的多处触摸。详见后下节multitouch。
  • event.target:被接触到的对象(display object)
  • event.name:"touch"字符串
  • event.phase:上面所描述的触摸的阶段
  • event.time:自从应用开始运行到现在的毫秒数,用来累计时间间隔
  • event.x / event.y:在屏幕坐标系里,接触位置的x和y
  • event.xStart / event.yStart:在屏幕坐标系里,触摸序列发生的began阶段时的位置的x和y

 

3 Multitouch检测

在应用中启动多点触控(multitouch)可以让检测和处理,同时在屏幕上多处发生的用户触摸事件。

因为默认情况下,multitouch是被关闭的,所以你必须通过system.activate()函数来启动它。注意,多点触控功能只能在实际真机上测试,而不能在Corona模拟器里测试。

system.activate( "multitouch" )

开启多点触控以后,注册到对象上的touch事件监听器,和前面描述touch检测一样。然后,通过比较event.id属性,来确定touch事件序列中哪一个触碰被返回。

system.activate( "multitouch" )

local myRectangle = display.newRect( 0, 0, 320, 480 )

local function multitouchListener( event )
    print( "Phase: "..event.phase )
    print( "Location: "..event.x..","..event.y )
    print( "Unique touch ID: "..tostring(event.id) )
    print( "----------" )
    return true
end

myRectangle:addEventListener( "touch", multitouchListener )

 

posted @ 2015-01-21 14:40  弯弓射月  阅读(1642)  评论(0编辑  收藏  举报