2

我正在尝试使用 cocos2d 开发游戏。我现在卡住了。我不知道如何检测双击事件,就像 windows 中的双击一样。我尝试使用

NSArray * allTouches = [touches allObjects];
int count = [allTouches count];

ccTouchesEnded

但这似乎在同时发生双重触摸时起作用。我想要它在 Windows 中的样子。

谁能给我一些想法?提前致谢。

4

2 回答 2

3

如果您使用targetedTouchDelegate,您可以这样做:

- (void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
    if(touch.tapCount==1) MPLOG(@"ONE TAP");
    if(touch.tapCount==2) MPLOG(@"TWO TAPS");
    return;
}

当双击发生时,您将获得两次触摸,即,当双击时,这将记录“ONE TAP”和“TWO TAPS”。由你来弄清楚你的状态并做你的事。

于 2013-02-14T03:09:52.850 回答
2

您在谈论多点触控 2 指点击或双击,就像在 mac 和 windows 中一样?

如果它像在 mac 和 windows 中一样双击,那么这里是解决方案。

你可以通过两种方式做到这一点。

  1. 使用 UITapGestureRecognizer(设置为检测双击)LearnCocos2D 在这个问题中建议。

  2. 通过使用时间差使用手动双击跟踪。

//在接口文件中声明这个

 NSTimeInterval      mLastTapTime;

在实施文件中:

-(id)init
{
    if(self = [super init])
    {
         mLastTapTime = [NSDate timeIntervalSinceReferenceDate];
    }
    return self;
}

//在触摸方法中

NSTimeInterval currentTime = [NSDate timeIntervalSinceReferenceDate];
NSTimeInterval diff = currentTime - mLastTapTime;

if(diff < 0.5 ) //0.5 or less
{
     //double tap
}

mLastTapTime = [NSDate timeIntervalSinceReferenceDate];
于 2013-02-15T17:53:19.140 回答