cocos2d-iphone

Solid obstacle to cause game over


I'm new to the cocos2d environment. I've recently purchased the line game starter kit and my first game is nearly complete which I'm very excited about.

I have one hurdle to get over. My game has a wall in the background which I'd like to make the players go avoid.

At the moment if my characters bump into each other, it's game over, but I'd like to use the same rules if the character bumps into the wall.


Solution

  • Is your wall a Sprite? if yes then you can do a simple collision detection like this-

    --CCSprite *wall= [CCSprite spriteWithSpriteFrameName:@"wall.png"];
    --CCSprite *player = [CCSprite spriteWithSpriteFrameName:@"player.png"];
    
    if (CGRectIntersectsRect(wall.boundingBox, player.boundingBox)) {                
        //Player hit the wall
    }
    

    (I'm hoping you already have a wall and player entities -just use them in place of wall and player in the above code).