4

任何想法如何处理 cocos2d 中的点击持续时间?

在用户将他或她的手指放在某个精灵上大约 1-2 秒后,我需要做一些事情。

谢谢。

4

3 回答 3

1

您需要以手动方式执行此操作:

  1. 在 CCLayer 子类中添加一个 BOOL 标志 ivar 和一个浮点 ivar。
  2. 在触摸开始时,将标志设置为 TRUE 并将浮点 ivar 重置为 0.0
  3. 在触摸移动、结束或取消时,将标志设置为 FALSE。
  4. updateortick中,将 float ivar 值增加dt数量。如果该浮点 ivar 值大于您的阈值(1.0 或 2.0 秒),请检查它是否执行您的逻辑。

如果您想处理多个触摸,您可能需要一种方法来附加和区分 BOOL 标志和浮动 ivar 组合到每个触摸。

我建议在 CCLayer 和您的实现子类之间创建一个中间子类,以便您可以从实现子类中隐藏该机制并允许轻松重用。

于 2011-12-02T11:55:27.100 回答
1

为自己节省大量手动工作,并使用 UIGestureRecognizers 来处理这些事情。在这种特殊情况下,您将需要使用UILongPressGestureRecognizer

顺便说一句,手势识别器是内置的,如果你使用Kobold2D就可以使用。

于 2011-12-02T19:22:36.043 回答
0

要使用 UILongPressGestureRecognizer,您可以执行以下操作:

UILongPressGestureRecognizer* recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressFrom:)];
recognizer.minimumPressDuration = 2.0; // seconds
AppDelegate* appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate.viewController.view addGestureRecognizer:recognizer];

您的长按处理程序可能如下所示:

-(void)handleLongPressFrom:(UILongPressGestureRecognizer*)recognizer
{
    if(recognizer.state == UIGestureRecognizerStateEnded)
    {
        CCLOG(@"Long press gesture recognized.");

        // Get the location of the touch in Cocos coordinates.
        CGPoint touchLocation = [recognizer locationInView:recognizer.view];
        CCDirector* director = [CCDirector sharedDirector];
        touchLocation = [director convertToGL:touchLocation];
        touchLocation = [[director runningScene] convertToNodeSpace:touchLocation];

        // Your stuff.
    }
}

完成后,不要忘记将其删除。

AppDelegate* appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate.viewController.view removeGestureRecognizer:recognizer];
于 2011-12-05T10:50:50.070 回答