removeFromParent操作不执行预期的功能

时间:2014-08-13 10:21:24

标签: ios objective-c sprite-kit

我有一个方法可以创建一条应该增加分数的线,并且该方法是按间隔调用的,只要有两条或更多条相同的线,每当Hero接触到离他最近的线时,我运行一个removeFromParent Action取而代之的是最远的行。但是,当生成其他行之前联系的第一行时,不会发生此问题。我怎么能纠正这个?这些线路连接到中间的平台。

platformGenerationMethod

-(void)createPlatform {
    switch (arc4random_uniform(2)) {
    case (0):
        objectTexture = [SKTexture textureWithImageNamed:@"shortPlatform3"];
        platformCount ++;
        NSLog(@"%li", (long)platformCount);
        break;
    case (1):
        objectTexture = [SKTexture textureWithImageNamed:@"shortPlatform3"];
        platformCount ++;
        NSLog(@"%li",(long)platformCount);
        break;
    case (2):
        objectTexture = [SKTexture textureWithImageNamed:@"shortPlatform3"];
        platformCount ++;
        NSLog(@"%li", (long)platformCount);
    default:
        break;
    }
    variaPlatform = [SKSpriteNode spriteNodeWithTexture:objectTexture];
    variaPlatform.name = @"variaPlatform";
    variaPlatform.zPosition = 2;
    variaPlatform.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:variaPlatform.size];
    variaPlatform.physicsBody.friction = 1;
    variaPlatform.physicsBody.linearDamping = 1;
    variaPlatform.physicsBody.categoryBitMask = fPlatformCategory;
    variaPlatform.physicsBody.contactTestBitMask = fPlayerCategory | fEnemyCategory;
    variaPlatform.physicsBody.collisionBitMask = fPlayerCategory | fEnemyCategory;
    variaPlatform.physicsBody.dynamic = NO;
    variaPlatform.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
    variaPlatform.position = CGPointMake(700, variaPlatform.position.y - 129);

    scoreLine = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(0.5, 700)];
    scoreLine.hidden = NO;
    scoreLine.physicsBody.dynamic = NO;
    scoreLine.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:scoreLine.size];
    scoreLine.position = CGPointMake(variaPlatform.position.x, variaPlatform.position.y/2);
    scoreLine.physicsBody.categoryBitMask = fScoreCategory;
    scoreLine.physicsBody.contactTestBitMask = fPlayerCategory;
    scoreLine.physicsBody.collisionBitMask = 0;

    [self addChild:scoreLine];
    [self addChild:variaPlatform];
    [self connectNode1:variaPlatform toNode2:scoreLine];
    [variaPlatform runAction:variaActions];
}

didBeginContact

-(void)didBeginContact:(SKPhysicsContact *)contact {

    SKPhysicsBody *firstBody, *secondBody;

    if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask)
    {
        firstBody = contact.bodyA;
        secondBody = contact.bodyB;
    }
    else
    {
        firstBody = contact.bodyB;
        secondBody = contact.bodyA;
    }

    if (((firstBody.categoryBitMask & fPlayerCategory) !=0) && ((secondBody.categoryBitMask & fScoreCategory) != 0))
    {
        NSLog(@"score");
        currentScore++;
        [scoreLine runAction:removeFromParent];
        [currentScoreLabel setText:[NSString stringWithFormat:@"%ld", (long)currentScore]];
    }
}

建议将不胜感激。

1 个答案:

答案 0 :(得分:1)

这应该允许您删除正确的分数节点...

// Returns a node that matches the specified category or nil
static inline SKSpriteNode *nodeFromBody(SKPhysicsBody *body1,
                                       SKPhysicsBody *body2,
                                       uint32_t category) {
    SKSpriteNode *node = nil;
    if (body1.categoryBitMask & category) {
        node = (SKSpriteNode *)body1.node;
    }
    else if (body2.categoryBitMask & category) {
        node = (SKSpriteNode *)body2.node;
    }
    return node;
}

- (void)didBeginContact:(SKPhysicsContact *)contact
{
    SKPhysicsBody *firstBody, *secondBody;
    SKSpriteNode *player = nil;
    SKSpriteNode *score = nil;
    SKSpriteNode *enemy = nil;

    firstBody = contact.bodyA;
    secondBody = contact.bodyB;

    player = nodeFromBody(firstBody, secondBody, fPlayerCategory);
    score = nodeFromBody(firstBody, secondBody, fScoreCategory);
    enemy = nodeFromBody(firstBody, secondBody, fEnemyCategory);

    // Test for contact between nodes
    if (player) {
        if (score) {
            NSLog(@"score");
            currentScore++;
            // remove the node that made contact with player
            [score runAction:removeFromParent];
            [currentScoreLabel setText:[NSString stringWithFormat:@"%ld", (long)currentScore]];
        }
        else if (enemy) {
            // Do something
        }
    }
}