任何想法如何处理 cocos2d 中的点击持续时间?
在用户将他或她的手指放在某个精灵上大约 1-2 秒后,我需要做一些事情。
谢谢。
任何想法如何处理 cocos2d 中的点击持续时间?
在用户将他或她的手指放在某个精灵上大约 1-2 秒后,我需要做一些事情。
谢谢。
您需要以手动方式执行此操作:
update
ortick
中,将 float ivar 值增加dt
数量。如果该浮点 ivar 值大于您的阈值(1.0 或 2.0 秒),请检查它是否执行您的逻辑。如果您想处理多个触摸,您可能需要一种方法来附加和区分 BOOL 标志和浮动 ivar 组合到每个触摸。
我建议在 CCLayer 和您的实现子类之间创建一个中间子类,以便您可以从实现子类中隐藏该机制并允许轻松重用。
为自己节省大量手动工作,并使用 UIGestureRecognizers 来处理这些事情。在这种特殊情况下,您将需要使用UILongPressGestureRecognizer。
顺便说一句,手势识别器是内置的,如果你使用Kobold2D就可以使用。
要使用 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];