0

我将多个精灵放置在这样的背景精灵上:

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

[self addchild:bg];

然后我将我的项目添加到 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];

我使用循环将多个精灵添加到 bg 上。

现在的问题是如何检测汽车是否与我放置在背景上的多个精灵发生碰撞?

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

我尝试过使用 pythagoras theorem 方法,但它再次不起作用。

有一种方法涉及将项目精灵添加到 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];
    }
}

更新:

它是固定的:)

-(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];
    }
}
4

1 回答 1

0

我发现代码有很多问题......

  1. 当您调用时removeAllChildren..确保您还从数组中删除对象..从父母中删除精灵不会将其从数组中删除。

  2. 在更新方法中更新汽车矩形。所以在你的更新方法中

    -(无效)更新:(ccTime)deltaTime {

        [self ifEdgeOfScreen];
    
        [self accelerate];
    
        carRect = [car boundingBox];
    ...........
    }
    

希望这可以帮助.. :)

于 2012-05-07T08:47:59.327 回答