2013-12-22 152 views
-1

我加入精靈到的NSMutableArray這樣的:閱讀精靈,檢測碰撞

 NSMutableArray * movableSprites; 

     NSString *image = [images objectAtIndex:3]; 
     CCSprite *sprite = [CCSprite spriteWithFile:image]; 
     sprite.position = ccp(AREA_A2_X2 - (i * 56), AREA_A3A5A4_Y); 
     [self addChild:sprite]; 
     [movableSprites addObject:sprite]; 

到目前爲止好。

現在我試圖檢測它們之間的衝突。用戶能夠移動精靈,但我想讓精靈被彼此阻擋。

因此,當我在翻譯他們時,我希望它在那裏發生。順便說一下,它是碰撞檢測的最佳去處嗎?

- (void)panForTranslation:(CGPoint)translation { 
    if (selSprite) { 

我打算做以下的循環:

CGRect rect1 = [SpriteUtilities positionRect:selSprite]; 

    CGRect rect2 = [SpriteUtilities positionRect:EVERY_OTHER_SPRITE]; 

    if (!CGRectIsNull(CGRectIntersection(rect1, rect2))) { 
     //handle collision 
    } 

的事情是...我沒有找到一個NSMutableArray的方法來檢索組圖對象。

謝謝您的幫助。

回答

0

好吧......幾個看完後我進入這個:

CCSprite *toCollide; 
for (int i = 0; i < [movableSprites count]; i++){ 

    toCollide = [movableSprites objectAtIndex:i]; 
... 

不管怎樣,謝謝!!希望它可以幫助別人!

+0

如果你使用問題的代碼,接下來你會想知道爲什麼數組是空的! – YvesLeBorg

+0

............爲什麼? – RickON

+0

你沒有創建數組對象,就像moveableSprites = [[NSMutableArray array] retain]; (保留,如果你不使用ARC,不要忘記釋放dealloc)。 – YvesLeBorg