★ TouchArcade needs your help. Click here to support us on Patreon.

Cocos2D Sprite Collision but in reverse?

03-05-2012, 12:04 PM
#1
Joined: Apr 2010
Posts: 57
Cocos2D Sprite Collision but in reverse?

Hey guys, I'm having a difficult time in trying to code up where two sprites are touching but have an event if they no longer touch. I've googled quite a bit without much luck and tried what I know and got close but not quite.

Just throwing a bone out here to see if anyone had any advice?

Thanks!

My currently published games in the app store: http://appstore.com/tomlavier
03-05-2012, 12:39 PM
#2
Joined: Mar 2010
Location: Estero, FL
Posts: 221
It would be something like this:

Code:
BOOL currentCollisionState;
BOOL previousCollisionState;
-(void)spriteCollided {
   currentCollisionState = TRUE;
   
}

-(void)update {
   // state used to be 'collided' but now it's not
   if (!currentCollisionState && previousCollisionState) {
        NSLog(@"stopped colliding");
        
   }
   previousCollisionState = currentCollisionState;
}
03-05-2012, 10:10 PM
#3
Joined: Apr 2010
Posts: 57
Thanks for the reply BCarbone, I appreciate it.

I ended up getting it working with this code:

Code:
    float playerImageSize = [swimmer texture].contentSize.width;
    float boardImageSize = [board texture].contentSize.width;
    float boardCollisionRadius = boardImageSize * 0.37f;float playerCollisionRadius = playerImageSize * 0.37f; 

        // This collision distance will roughly equal the image shapes.
        float maxCollisionDistance = playerCollisionRadius + boardCollisionRadius;
        
        // Get the distance between player and board.
        float actualDistance = ccpDistance(swimmer.position, board.position);
        
        //  
        if (actualDistance < maxCollisionDistance) {
Do This
}

My currently published games in the app store: http://appstore.com/tomlavier

Last edited by Vidkid72; 03-06-2012 at 05:50 PM.
03-06-2012, 08:55 AM
#4
Joined: Mar 2011
Location: In a car
Posts: 324
you should use box2d collision detection

My Twitter:@Tudor