Cocos2d iphone 碰撞检测cocos2d

Cocos2d iphone 碰撞检测cocos2d,cocos2d-iphone,collision-detection,Cocos2d Iphone,Collision Detection,我想做一个游戏,球在屏幕上随机反弹,如果击中目标,你就赢了。因此,我试图在球精灵和目标精灵之间创建碰撞检测。balls实现在gameplay类之外的单独类中完成,目标添加到gameplay类中,如下所示: -(void) targetCollision{ CCSprite *target = [CCSprite spriteWithFile:@"target.png"]; target.position = ccp(400,50); [self addChild:tar

我想做一个游戏,球在屏幕上随机反弹,如果击中目标,你就赢了。因此,我试图在球精灵和目标精灵之间创建碰撞检测。balls实现在gameplay类之外的单独类中完成,目标添加到gameplay类中,如下所示:

-(void) targetCollision{

    CCSprite *target = [CCSprite spriteWithFile:@"target.png"];
    target.position = ccp(400,50);
    [self addChild:target];


    CCSprite *ball = [[Ball alloc] init];
    [self addChild:ball];

    //CCSprite *ball = [CCSprite spriteWithFile:@"ball2.png"];
    //ball.position = ccp(400,75);
    //id a1 = [CCRotateBy actionWithDuration:1 angle:360];
    //id repAct = [CCRepeatForever actionWithAction:a1];
    //[ball runAction: repAct];
    //[self addChild:ball];

    CGRect ballRect = [ball boundingBox];
    CGRect targetRect =  [target boundingBox];

    if (CGRectIntersectsRect(ballRect, targetRect)) {
        NSLog(@"Target hit! Collision detected"); 
    }  
}

如果我用注释掉的代码添加球,我可以让这个方法工作,但这不是我想要做的。任何帮助都将不胜感激

如何使用CCScheduler每微秒检查一次冲突

您应该在schedule update中调用冲突方法,或者直接在schedule update method中调用冲突方法。 类似这样的事情-

在您的init方法中

[self scheduleUpdate];
在scheduleUpdate方法中,您可以调用

[self targetCollision];
一旦球击中另一个球,它将确认碰撞


我希望它能解决您的问题

我可以看到球和目标被添加,球继续旋转。。所以你的问题是球的运动??球的运动是在另一个类中定义的,然后用CCSprite*ball=[[ball alloc]init]行添加到场景中;[self addChild:ball];本质上是一个在屏幕和固定目标周围随机反弹的球。当球悬停在目标上方或与目标发生碰撞时,我希望控制台显示已检测到碰撞