0

我有一个NSDictionary(allSprites)有很多精灵(spriteX),在我的触摸方法中,我想检查精灵是否被触摸。

我的问题是它对boundingBox 没有反应。我没有看到我的错误!有问题NSDictionary吗?我没有得到任何错误或任何东西......但它不起作用。

是否有另一种方法来检查边界框NSDictionary?有人可以帮助我吗?

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  for (UITouch *touch in [event allTouches]) {
    for (NSValue* value in allSprites) {
      CGPoint location = [touch locationInView:touch.view];
      location = [[CCDirector sharedDirector] convertToGL:location];
      if(CGRectContainsPoint([spriteX boundingBox], location)){
         NSLog(@"sprite was touched");
      }
    }
  }
}
4

1 回答 1

3

除了边界框的测试之外,您似乎根本没有在循环中引用 spriteX,它可能未初始化。也许你打算这样做:

for (CCSprite* spriteX in [allSprites allValues]) {
  CGPoint location = [touch locationInView:touch.view];
  location = [[CCDirector sharedDirector] convertToGL:location];
  if(CGRectContainsPoint([spriteX boundingBox], location)){
     NSLog(@"sprite was touched");
  }
}

如果你需要精灵的钥匙:

for (NSString*key in [allSprites allKeys]) {
  spriteX = [allSprites objectForKey:key];
  CGPoint location = [touch locationInView:touch.view];
  location = [[CCDirector sharedDirector] convertToGL:location];
  if(CGRectContainsPoint([spriteX boundingBox], location)){
     NSLog(@"sprite with key %@ was touched",key);
  }
}
于 2013-06-02T12:36:20.293 回答