Cocos2D Sprite Collision but in reverse?

Discussion in 'Public Game Developers Forum' started by Vidkid72, Mar 5, 2012.

  1. Vidkid72

    Vidkid72 Well-Known Member

    Apr 11, 2010
    57
    0
    0
    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!
     
  2. bcarbone

    bcarbone Well-Known Member

    Mar 19, 2010
    221
    0
    0
    iPhone Developer
    Estero, FL
    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;
    }
    
     
  3. Vidkid72

    Vidkid72 Well-Known Member

    Apr 11, 2010
    57
    0
    0
    #3 Vidkid72, Mar 6, 2012
    Last edited: Mar 6, 2012
    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
    }
    
     
  4. MrLeQuack

    MrLeQuack Well-Known Member

    you should use box2d collision detection
     

Share This Page