2012-01-30 63 views
0

我已经将精灵添加到我的NSMutable数组中,现在我想要找到它们;我正在使用这些方法:cocos2d修改数组

- (void)selectSpriteForTouch:(CGPoint)touchLocation { 
     for (CCSprite *sprite in selectedSpritesArray) { 
      if (CGRectContainsPoint(sprite.boundingBox, touchLocation)) {    
       newSprite = sprite; 
       break; 
      } 
     } 
} 

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {  
    CGPoint touchLocation = [self convertTouchToNodeSpace:touch]; 
    [self selectSpriteForTouch:touchLocation];  
    return TRUE;  
} 

我该如何正确地做到这一点?现在我无法访问一些重叠的精灵。

谢谢!如果您要访问重叠精灵

回答

1

建议:

- (NSMutableArray*)selectSpriteForTouch:(CGPoint)touchLocation { 
     NSMutableArray *sprites = [[NSMutableArray alloc] init]; 
     for (CCSprite *sprite in selectedSpritesArray) { 
      if (CGRectContainsPoint(sprite.boundingBox, touchLocation)) {    
       [sprites addObject:sprite]; 
      } 
     } 

     // dont forget to release this array when you are done with it 
     return sprites; 
} 
+0

感谢您的代码,它的工作原理,但不是为我所有的精灵....我仍然有两个精灵这我不能给他们存取权限... – 2012-01-30 16:41:06

+0

你可以给我一些代码如何添加你的精灵?当你比较类似上面的代码的触摸位置时,如果重叠,则无关紧要。我猜这个错误是在别的地方。 – 2012-01-31 08:38:50

+0

它的工作原理!我的错... – 2012-01-31 09:48:36