Collision detection with table sprites in Cocos2D

advertisements

i'm pretty new to Objective-C and Cocos2D so go easy, I know this is basic stuff.

I'm using an array to randomly place 4 gold coin sprites on the screen, and i'm using another sprite (a dragon) to fly around and collect the coins. Obviously, I want the coin to disappear and another to randomly appear (like collecting apples in Snake). My problem is that I don't know how to reference the individual sprites. I've added some handling to the update method but a) I don't think its appropriate and b) it doesn't do anything. Please help. I think I need to utilise:

[self removeChild:sprite cleanup:YES];

But I'm not sure how. This is how i'm populating the screen with rings::

@implementation MainScene {

CCSprite *_hero;
CCPhysicsNode *_physicsNode;
CCSprite *_goldRing;
NSMutableArray *_goldRings;

}

-(void)didLoadFromCCB {

self.userInteractionEnabled = TRUE;

_goldRing.physicsBody.collisionType = @"Level";
_goldRing.physicsBody.sensor = TRUE;

_physicsNode.collisionDelegate = self;
_hero.physicsBody.collisionType = @"hero";

CGFloat random = ((double)arc4random() / ARC4RANDOM_MAX);
_meteorites = [NSMutableArray array];
[self spawnNewGoldRing];
[self spawnNewGoldRing];
[self spawnNewGoldRing];
[self spawnNewGoldRing];

}

-(void)spawnNewGoldRing {

CGFloat randomX = ((double)arc4random() / ARC4RANDOM_MAX);
CGFloat randomY = ((double)arc4random() / ARC4RANDOM_MAX);
CGFloat rangeX = 200;
CGFloat rangeY = 300;

CCNode *goldRing = [CCBReader load:@"goldRing"];
goldRing.position = ccp(randomX * rangeX, (randomY * rangeY)+100);
[_physicsNode addChild:goldRing];
[_goldRings addObject:goldRing];
}

- (void)update:(CCTime)delta {

_hero.position = ccp(_hero.position.x, _hero.position.y);

if(_hero.position.x <= _hero.contentSize.width/2 * -1.5) {
    _hero.position = ccp(_ground.contentSize.width + _hero.contentSize.width/2, _hero.position.y);

    NSMutableArray *collectedGoldRings = nil;
    for (CCNode *goldRing in _goldRings) {
        CGPoint goldRingWorldPosition = [_physicsNode convertToWorldSpace:goldRing.position];
        CGPoint goldRingScreenPosition = [self convertToNodeSpace:goldRingWorldPosition];
        if (goldRingScreenPosition.x < -goldRing.contentSize.width) {
            if (!collectedGoldRings) {
                collectedGoldRings = [NSMutableArray array];
            }
            [collectedGoldRings addObject:goldRing];
        }
    }
    for (CCNode *goldRingToRemove in collectedGoldRings) {
        [goldRingToRemove removeFromParent];
        [_goldRings removeObject:goldRingToRemove];
        // for each removed goldRing, add a new one
        [self spawnNewGoldRing];
    }

}

}

Would it be too much to ask how to keep count of these too to display a score?

Thanks so much for any help.

EDIT*

NSLog on the array of gold rings

"<goldRing = 0x9b756b0 | Rect = (0.00,0.00,24.00,23.50) | tag =  | atlasIndex = -1>"
)
2014-05-08 10:24:32.201 dragonCollector2[10165:60b] Gold Ring Loaded
2014-05-08 10:24:32.201 dragonCollector2[10165:60b] Array:(
"<goldRing = 0x9b77e50 | Rect = (0.00,0.00,24.00,23.50) | tag =  | atlasIndex = -1>"


As I see, you are using spritebuilder, so you are using the new version of Cocos2D. In this version you can track a collision between two types of nodes. In your case, the coins and the hero.

This is as simple as:

1) Set your coin.physicsBody.collisionType = @"coin";

2) Set your hero.physicsBody.collisionType = @"hero";

3) Implement this method:

-(void)ccPhysicsCollisionPostSolve:(CCPhysicsCollisionPair *)pair hero:(CCNode *)nodeA coin:(CCNode *)nodeB{

//this method will be automatically called when the hero hits a coin.
//now call your method to remove the coin and call the method to add another coin.
}

To keep a count of the score, just make an int variable, int score;. In your didLoadFromCCB method, initialize it to 0, score=0; and inside this collision method, just do score++; and maybe do a NSLog(@"Your score:%i",score);

Hope this helps.