Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c cocos2d v3:如何使用基于场景的触摸和UILongPressGestureRecognitor_Objective C_Cocos2d Iphone - Fatal编程技术网

Objective c cocos2d v3:如何使用基于场景的触摸和UILongPressGestureRecognitor

Objective c cocos2d v3:如何使用基于场景的触摸和UILongPressGestureRecognitor,objective-c,cocos2d-iphone,Objective C,Cocos2d Iphone,在这个网站上使用各种各样的例子来说明如何实现objective c,我想创建一个应用程序,它需要识别长时间的按下和单击。我的应用程序的代码具有以下功能,为简洁起见省略了部分,实际代码编译良好,下面的代码是手工键入的,因此无需指出语法错误: 从CCScene派生的基类,我们称之为LongPressHandlingScene A.此类在初始化期间设置UILongPressGestureRecognitor,并在退出时将其删除 B这个类有一个方法,它充当长按手势的事件处理程序,当自己使用时,它可以正常

在这个网站上使用各种各样的例子来说明如何实现objective c,我想创建一个应用程序,它需要识别长时间的按下和单击。我的应用程序的代码具有以下功能,为简洁起见省略了部分,实际代码编译良好,下面的代码是手工键入的,因此无需指出语法错误:

从CCScene派生的基类,我们称之为LongPressHandlingScene A.此类在初始化期间设置UILongPressGestureRecognitor,并在退出时将其删除 B这个类有一个方法,它充当长按手势的事件处理程序,当自己使用时,它可以正常工作 B该类的其余部分与任何其他CCScene类相同

//LongPressHandlingScene.h
@interface LongPressHandlingScene : CCScene {  
......  
}  
..........  
@end  

//LongPressHandlingScene.m  
@implementation LongPressHandlingScene{  
.............  
}  

-(id)init{  
{  
   if(!(self = [super init]))  
      return nil;  
.........  
   UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longTouchAction:)];  

   return self;  
}  
..............  
-(void)longTouchAction:(UILongPressGestureRecognizer *)longTouch{  
..........  
}  

-(void)onExit{  
   NSArray *grs = [[[CCDirector sharedDirector] view] gestureRecognizers];  

   for(UIGestureRecognizers *gesture in grs){  
      if([gesture isKindOfClass:[UILongPressGestureRecognizer class]]){  
         [[[CCDirector sharedDirector] view] removeGestureRecognizer:gesture];  
}  
@end  
从CCSprite派生的基类,我们称之为DragHandlingSprite A.此类重写TouchBegind、touchMoved和touchEnded。在CCScene或CCScene派生类中自己使用时,所有这些都可以正常工作 C该类的其余部分与任何其他CCSprite类相同

//DragHandlingSprite.h  
@interface DragHandlingSprite : CCSprite {  
..........  
}  
@end  

//DragHandlingSprite.m  
@implementation DragHandlingSprite {  
..............  
}  

-(void)onEnter{  
   self.userInteractionEnabled = YES;  
..............  
}  

-(void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event{  
............  
}  

-(void)touchMoved:(UITouch *)touch withEvent:(UIEvent *)event{  
   CGPoint touchLocation = [touch locationInNode:self.parent];  
   self.position = touchLocation;  
}  

-(void)touchEnded:(UITouch *)touch withEvent:(UIEvent *)event {  
.............  
}  

// MainScene.h  
@interface MainScene : LongPressHandlingScene{  
.............  
}  

+(MainScene *)scene;  
@end  

// MainScene.m  
@implementation MainScene  

+(MainScene) scene {  
   MainScene *scene = [self node];  

   return scene;  
}  

-(id) init {  
   if(!(self = [super init]))  
      return nil;  

   self.userInteractionEnabled = YES;  
...............
   NSArray *itemsArray = ProperlyPopulatedArrayFromSomewhereElse;  
   for(id obj in itemsArray){  
      if([obj isKindOfClass:[NSDictionary class]]){  
         NSDictionary *vDict = (NSDictionary *)obj;  
         float posX = [[vDcit objectForKey:@"posXKey"] floatValue];  
         float poxY = [[vDict objectForKey:@"posYKey"] floatValue];  
         NSString *name = [vDict objectForKey:@"imageName"];  

         DragHandlingSprite *newImage = [DragHandlingSprite spriteWithImageNamed:name];  
         newImage.name = name;  
         newImage.position = CGPointMake(posX, posY);  

         [self addChild:newImage];  
    }  

@end  
在向LongPressHandling场景添加一个或多个DraghandingSprite后,我遇到的问题如下:

单击任何DragHandlingSprite即可工作。在第一个DragHandlingSprite导致应用程序崩溃并出现错误后,单击该DragHandlingSprite或任何其他DragHandlingSprite:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary setObject:forKey:]: attempt to insert nil key'
LongPressRecognition在100%的情况下工作,但只要单击一次,就会出现与上述相同的异常

有什么想法吗


提前感谢。

对于那些偶然发现这个问题并寻求答案的人,这就是我的想法

在正常情况下,UILongPressGestureRecognitor cocoa和onTouch事件Cocos2d都可以共存,而不会出现任何问题。在同一个CCScene中声明这两个选项后,将在触摸生命周期中调用它们各自的选择器/事件处理程序。这导致了我上面提到的错误,因为它允许运行所有基于触摸/手势的代码。在我的例子中,一些事件处理程序正在删除UILongPressGestureRecognitizer操作选择器需要使用的信息,反之亦然,因此在某个时候尝试使用“nil”键,这导致了错误

出于这个原因,我决定实现一个基于枚举的状态变量,该变量将跟踪应用程序当前处于哪个触摸状态,以防止代码不按顺序执行

enum TouchStates {DRAG_BEGAN, DRAG_MOVED, DRAG_ENDED, LONG_TOUCH_BEGAN, LONG_TOUCH_ENDED, DEFAULT};  
enum TouchStates touchState;
........  

-(id)init{  
   ................  
   UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longTouchAction:)];  
   ............  
}

-(void)longTouchAction:(UILongPressGestureRecognizer *)longTouch{  
   if(/*[touchState is the correct one to do something here]*/){  
      if(longTouch.state == UIGestureRecognizerStateBegan){  
         touchState = LONG_TOUCH_BEGAN;
         .............
         //DO WHAT'S NECESSARY HERE
         .............  
   }
   // Do something similar for the "UIGestureRecongizerEnded" flag  
}

-(void)onTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{  
   if(/*[touchState is the correct one to do something here]*/){  
      touchState = DRAG_BEGAN;
      ..............  
      //DO WHAT'S NECESSARY HERE  
      ...............
}  

// Do something similar for the onTouchMoved and onTouchEnded event handlers.

抱歉格式化。我试图让它看起来漂亮,但我试图使用指示的四个空格或ctrl+k或菜单上的“代码”按钮,但没有效果。您试图将代码格式添加到编号列表1。。。这就是为什么它没有格式化code@LearnCocos2D. Thx用于编辑。你知道为什么代码的最后一部分没有出现吗?编辑问题时,代码的文本是可见的,但它不会显示在问题本身中。您可以滚动代码窗口,谢谢。顺便说一句,我的问题的答案有可能在你的书里吗?我已经拥有它了,已经在那里寻找它了,但也许我错过了什么。。。。大概