cocos2d 中的点击持续时间

发布于 2024-12-19 07:11:14 字数 88 浏览 5 评论 0 原文

有什么想法如何处理 cocos2d 中的点击持续时间吗?

我需要在用户将手指放在某个精灵上大约 1-2 秒后执行某些操作。

谢谢。

Any ideas how to handle tap duration in cocos2d?

I need to do something after the user holds his or her finger on a certain sprite for about 1-2 secs.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如若梦似彩虹 2024-12-26 07:11:14

您需要手动执行此操作:

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

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

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

You need to do it the manual way:

  1. Add a BOOL flag ivar and a float ivar in your CCLayer subclass.
  2. On touch began, set the flag to TRUE and reset the float ivar to 0.0
  3. On touch moved, ended or cancelled, set the flag to FALSE.
  4. In the update or tick, increase the float ivar value by the dt amount. Check if that float ivar value to perform your logic if it is larger than your threshold value (1.0 or 2.0 seconds).

If you want to handle multiple touches, you might need a way to attach and differentiate the BOOL flag and float ivar combination to each touch.

I'd suggest creating an intermediate subclass between CCLayer and your implementation subclass so that you can hide the mechanism from the implementation subclass and also to allow easy reuse.

漫漫岁月 2024-12-26 07:11:14

使用 UIGestureRecognizers 来完成类似的事情,可以节省大量的手动工作。在这种特殊情况下,您将需要使用 UILongPressGestureRecognizer

顺便说一句, 手势识别器是内置的,准备好如果您使用 href="http://www.kobold2d.com" rel="nofollow">Kobold2D

Save yourself a lot of manual work and use the UIGestureRecognizers for things like these. In this particular case you will want to use the UILongPressGestureRecognizer.

Btw, gesture recognizers are built-in, ready to use if you use Kobold2D.

°如果伤别离去 2024-12-26 07:11:14

要使用 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];

To use a UILongPressGestureRecognizer, you can do something like this:

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

Your long press handler could look like this:

-(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.
    }
}

When you're finished, don't forget to remove it.

AppDelegate* appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate.viewController.view removeGestureRecognizer:recognizer];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文