首页 > 代码库 > 碰撞的两种实现,

碰撞的两种实现,

-(void)checkAttack

{

    static BOOL beforeState;

    static int count = 0;

    

    BOOL attack = CGRectIntersectsRect(self.lifeView.frame, self.blockView.frame);

    if (beforeState == 0 && attack == 1) {

        count ++;

        [self.countLabel setText:[NSString stringWithFormat:@"碰撞次数:%d",count]];

    }

    beforeState = attack;

    

    CGFloat blockOriginY = self.lifeView.frame.origin.y - self.blockView.frame.size.height;

    CGFloat maxBlockOriginY = self.lifeView.frame.origin.y + self.blockView.frame.size.height;

    CGFloat lifeOriginX = self.blockView.frame.origin.x - self.lifeView.frame.size.width;

    CGFloat maxLifeOriginX = self.blockView.frame.origin.x + self.lifeView.frame.size.width;

    if ((maxLifeOriginX > self.lifeView.frame.origin.x &&  self.lifeView.frame.origin.x> lifeOriginX) && (self.blockView.frame.origin.y > blockOriginY && maxBlockOriginY > self.blockView.frame.origin.y)) {

        count ++;

        [self.countLabel setText:[NSString stringWithFormat:@"碰撞次数:%d",count]];

        

    }

}

碰撞的两种实现,