Cocos2d iphone Cocos2d精灵碰撞

Cocos2d iphone Cocos2d精灵碰撞,cocos2d-iphone,sprite,collision-detection,Cocos2d Iphone,Sprite,Collision Detection,我在背景精灵上放置了多个精灵,如下所示: //my background CCSprite *bg = [CCSprite spriteWithFile:@"imageName.png"]; [self addchild:bg]; 然后我把我的物品添加到背景上 //this is how i add my items CCSprite *items = [CCSprite spriteWithFile:@"itemName.png"]; [bg addchild:items]; 哦,别忘

我在背景精灵上放置了多个精灵,如下所示:

//my background
CCSprite *bg = [CCSprite spriteWithFile:@"imageName.png"];

[self addchild:bg];
然后我把我的物品添加到背景上

//this is how i add my items
CCSprite *items = [CCSprite spriteWithFile:@"itemName.png"];

[bg addchild:items];
哦,别忘了我的汽车精灵

//my car
CCSprite *car = [CCSprite spriteWithFile:@"car.png"];
[self addchild:car];
我使用循环将多个精灵添加到背景上

现在的问题是,我如何检测汽车是否与我放在背景上的多个精灵相撞

我试过使用CGRectIntersectsRect,但它不起作用

我尝试过使用毕达哥拉斯定理的方法,但再次证明它不起作用

有一种方法涉及将项目精灵添加到NSMutableArray中,但它也不起作用

有谁能建议一个我可以尝试的方法吗

附加代码:

-(void) initializeCarAndItems
{
    car = [CCSprite spriteWithFile:@"android.png"];
    car.position = ccp(screenSize.width/2, screenSize.height * 0.30);
    [self addChild:car z:1];
    carRect = [car boundingBox];
}

-(void) initializeMap
{
    bg1 = [CCSprite spriteWithFile:@"racingBG.png"];
    bg1.anchorPoint = ccp(0, 0);
    bg1.position = ccp(0, 0);

    [self addChild:bg1 z:-1];

    bg2 = [CCSprite spriteWithFile:@"racingBG2.png"];
    bg2.anchorPoint = ccp(0,0);
    bg2.position = ccp(0, bg1.boundingBox.size.height - 1);

    [self addChild:bg2 z:-1];

    convertedWidth = (int)bg1.boundingBox.size.width;
    convertedHeight = (int)bg1.boundingBox.size.height;

    for (y = 0; y < 15; y++)
    {   
        positionX = arc4random()%convertedWidth;
        positionY = arc4random()%convertedHeight;

        items = [CCSprite spriteWithFile:@"item.png"];
        items.position = ccp(positionX, positionY + 300);
        [bg1 addChild:items z:100];
        [itemsArray addObject:items];
    }

    for (y = 0; y < 15; y++)
    {   
        positionX = arc4random()%convertedWidth;
        positionY = arc4random()%convertedHeight;

        items = [CCSprite spriteWithFile:@"item.png"];
        items.position = ccp(positionX, positionY);
        [bg2 addChild:items z:100];
        [itemsArray addObject:items];
    }
}

-(void) accelerate
{
    bg1.position = ccp(0, bg1.position.y - accelerateNumber);
    bg2.position = ccp(0, bg2.position.y - accelerateNumber);

    if (bg1.position.y < -bg1.boundingBox.size.height)
    {
        questionCount++;
        bg1.position = ccp(0, bg2.position.y + bg2.boundingBox.size.height - 1);
        [self question];

        [bg1 removeAllChildrenWithCleanup:YES];
        for (y = 0; y < 15; y++)
        {   
            positionY = arc4random()%convertedHeight;
            positionX = arc4random()%convertedWidth;

            items.position = ccp(positionX, positionY);
            items = [CCSprite spriteWithFile:@"item.png"];
            [bg1 addChild:items z:100];
            [itemsArray addObject:items];
        }
    }
    else if (bg2.position.y < -bg2.boundingBox.size.height)
    {
        questionCount++;
        bg2.position = ccp(0, bg1.position.y + bg1.boundingBox.size.height - 1);
        [self question];

        [bg2 removeAllChildrenWithCleanup:YES];
        for (y = 0; y < 15; y++)
        {   
            positionY = arc4random()%convertedHeight;
            positionX = arc4random()%convertedWidth;

            items.position = ccp(positionX, positionY);
            items = [CCSprite spriteWithFile:@"item.png"];
            [bg2 addChild:items z:100];
            [itemsArray addObject:items];
        }
    }
}

-(void) update:(ccTime)deltaTime
{
    [self ifEdgeOfScreen];
    [self accelerate];

    for (CCSprite *itemFromArray in itemsArray)
    {
        CGRect itemRect = [itemFromArray boundingBox];
        if (CGRectIntersectsRect(carRect, itemRect))
        {
            NSLog(@"Collision!");
        }
    }

    if (leftButton.active == TRUE)
    {
        [self moveLeftRight:1];
    }
    else if (rightButton.active == TRUE)
    {
        [self moveLeftRight:2];
    }
}

我发现代码有很多问题

  • 当您调用
    removeAllChildren
    时。。确保还从阵列中删除对象。。从父对象中删除精灵并不会将其从阵列中删除

  • 在更新方法中更新car rect。所以在你的更新方法中

    -(作废)更新:(ccTime)deltaTime {


  • 希望这有帮助。:)

    你也能为交叉口编写代码吗?你是如何访问对象的?因为它将显示你是如何处理碰撞的…:)嘿Nikhil Aneja!我在问题中添加了更多代码:)提前谢谢!Heya Nikhil Aneja。我已经解决了这个问题:D我刚刚更改了这个:为了(itemsArray中的CCSprite*itemFromArray){CGRect itemRect=[itemFromArray boundingBox];if(CGRectIntersectsRect(carRect,itemRect)){NSLog(@“Collision!”);}}对于(itemsArray中的CCSprite*itemFromArray){if(CGRectIntersectsRect(carRect,[itemFromArray boundingBox])){NSLog(@“碰撞!”;}}}当然我添加了carRect=[carboundingbox]:)谢谢你的帮助!
    -(void) update:(ccTime)deltaTime
    {
        car = [car boundingbox];
    
        [self ifEdgeOfScreen];
        [self accelerate];
    
    
    
        for (CCSprite *itemFromArray in itemsArray)
        {
            if (CGRectIntersectsRect(carRect, [itemFromArray boundingbox]))
            {
                NSLog(@"Collision!");
            }
        }
    
        if (leftButton.active == TRUE)
        {
            [self moveLeftRight:1];
        }
        else if (rightButton.active == TRUE)
        {
            [self moveLeftRight:2];
        }
    }
    
        [self ifEdgeOfScreen];
    
        [self accelerate];
    
        carRect = [car boundingBox];
    ...........
    }