Cocos2d iphone cocos2d中的点击持续时间
你知道如何在cocos2d中处理点击持续时间吗 我需要在用户用手指按住某个精灵大约1-2秒后做一些事情Cocos2d iphone cocos2d中的点击持续时间,cocos2d-iphone,uigesturerecognizer,tap,Cocos2d Iphone,Uigesturerecognizer,Tap,你知道如何在cocos2d中处理点击持续时间吗 我需要在用户用手指按住某个精灵大约1-2秒后做一些事情 谢谢。您需要手动操作: 在CCLayer子类中添加BOOL标志ivar和float ivar 触摸开始时,将标志设置为TRUE,并将浮点ivar重置为0.0 触摸移动、结束或取消时,将标志设置为FALSE 在更新或勾选中,将浮点ivar值增加dt量。检查浮点ivar值是否大于阈值(1.0或2.0秒),以执行逻辑 如果要处理多个触摸,可能需要一种方法将BOOL标志和float ivar组合附加到
谢谢。您需要手动操作:
更新
或勾选
中,将浮点ivar值增加dt
量。检查浮点ivar值是否大于阈值(1.0或2.0秒),以执行逻辑我建议在CCLayer和您的实现子类之间创建一个中间子类,这样您就可以对实现子类隐藏机制,并允许轻松重用。省去您大量的手工工作,并使用UIgestureRecognitors来处理类似的事情。在这种特殊情况下,您需要使用
顺便说一句,如果您使用。要使用UILongPressGestureRecognitor,您可以执行以下操作:
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.
}
}
您的长按处理程序可能如下所示:
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];
为什么不使用UILongPressGestureRecognizer?有关于如何在cocos2d中使用UIGestureRecognizer的教程吗?关于UIGestureRecognizers的任何教程都可以,例如官方文档:。你不需要一个特定于Cocos2D的。