0

我的问题是我没有找到通过 UIScrollView “穿透”的解决方案,因此 ccLayer 可以识别 ccTouch 事件

   self.isTouchEnabled = YES;
    [[NSBundle mainBundle] loadNibNamed:@"myLayer" owner:self options:nil];

...

- (void) registerWithTouchDispatcher {
    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:INT_MIN swallowsTouches:NO];
    }

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
        CGPoint location = [self convertToWorldSpace:[self convertTouchToNodeSpace:touch]];

任何想法如何创建代表或其他解决方案来绕过 UI 并与 cc 交谈?

4

1 回答 1

2

今天早上我在使用 Cocos2D v1.0.0 时遇到了这个问题。我的解决方案是在该层的 init 方法中包含 CCTouchDispatcher 方法调用,然后在 UIView 中的该层将识别触摸。

-(id) init
{
  if ((self = [super init]) != nil) {
    // do stuff
    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
  }
  return self;
}

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
  CGPoint location = [self convertToNodeSpace:[[CCDirector sharedDirector] convertToGL:[touch locationInView:[touch view]]]];
  NSLog(@"TouchBegan at x:%0.2f, y:%0.2f", location.x, location.y);

  return YES;
}

另一种解决方案是使用 ccTouchesBegan 方法:

-(id) init
{
  if ((self = [super init]) != nil) {
    // do stuff
    self.isTouchEnabled = YES;
  }
  return self;
}


-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{   
  for (UITouch *thisTouch in touches) {
    CGPoint location = [self convertToNodeSpace:[[CCDirector sharedDirector] convertToGL:[thisTouch locationInView:[thisTouch view]]]];
    NSLog(@"TouchesBegan at x:%0.2f, y:%0.2f", location.x, location.y); 
  }
}

请注意,这两种触摸方法有不同的方法让您的应用程序知道它应该响应触摸。您无法混合搭配您想要如何响应触摸,以及您想要观察哪些触摸。

于 2011-08-17T05:55:25.003 回答