当我的ccLayer具有loadNibNamed时,如何识别ccTouches事件

时间:2011-07-16 19:13:40

标签: uiscrollview cocos2d-iphone touch cclayer

我的问题是我没有找到通过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]];

如何创建委托或其他解决方案以绕过用户界面并与cc交谈?

1 个答案:

答案 0 :(得分: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); 
  }
}

请注意,这两种触摸方法有不同的方法让您的应用程序知道它应该响应触摸。你不能混合和匹配你想要回应的方式,以及你想要观察的接触。