Collision in Cocos2d

A continuation of coloring sprites in Cocos2d, we will make the movable sprite change colors when it collides with another sprite.Start by getting the code for Coloring sprites in Cocos2d.

Now add this line of code to the end of your init method.

[self schedule:@selector(update:)];

Now your program will call the method update method over and over. Before it can call the method we must create it. Add the method to your program.

- (void)update:(ccTime)dt
{
CGRect RedRect = CGRectMake(_RedSquare.position.x(_RedSquare.contentSize.width/2),
_RedSquare.position.y(_RedSquare.contentSize.height/2),
_RedSquare.contentSize.width,
_RedSquare.contentSize.height);

CGRect moveRect = CGRectMake(_Square.position.x(_Square.contentSize.width/2),
_Square.position.y(_Square.contentSize.height/2),
_Square.contentSize.width,
_Square.contentSize.height);

CGRect GreenRect = CGRectMake(_GreenSquare.position.x(_GreenSquare.contentSize.width/2),
_GreenSquare.position.y(_GreenSquare.contentSize.height/2),
_GreenSquare.contentSize.width,
_GreenSquare.contentSize.height);

CGRect BlueRect = CGRectMake(_BlueSquare.position.x(_BlueSquare.contentSize.width/2),
_BlueSquare.position.y(_BlueSquare.contentSize.height/2),
_BlueSquare.contentSize.width,
_BlueSquare.contentSize.height);

if (CGRectIntersectsRect(moveRect, RedRect)){
[_Square setColor:ccc3(255,0,0)];
}

else if (CGRectIntersectsRect(moveRect, BlueRect)){
[_Square setColor:ccc3(0,0,255)];
}

else if (CGRectIntersectsRect(moveRect, GreenRect)){
[_Square setColor:ccc3(0,255,0)];
}
}

First thing we do is create a rectangle that tells the program where the sprite is. Next we test if the movable sprite has collided with any other of the sprites. If the sprite did collided with the other sprite then we change the movable sprite’s color.

Download the code for Collisions in Cocos2d.

Copy right Britney Lee Johnson August 2010.

This entry was posted in cocos2d. Bookmark the permalink.

6 Responses to Collision in Cocos2d

  1. Joethemonkey101 says:

    Hey! It’s me from the forums. I was wondering if you could do tutorial on a pause button and a pause screen. I’m having a little trouble with structs.

  2. Garretot says:

    ЎGracias por el artнculo. Cada vez que quieres leer.
    Have a nice day

    Garretot

  3. SuperSonic says:

    Greatings, Todo dinбmica y muy positiva! :)
    Have a nice day

    SuperSonic

  4. This is very interesting, You’re a very skilled blogger.

    I’ve joined your feed and look forward to seeking more of your great post.
    Also, I have shared your site in my social networks!

  5. I think that is one of the such a lot vital info for me.

    And i am glad reading your article. But should observation on few normal things, The web site taste is perfect, the articles is in point of fact
    great : D. Excellent activity, cheers

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>