SpriteKit - 停止对象堆积

时间:2014-04-30 09:45:33

标签: ios macos sprite-kit

我想知道我应该添加什么代码来阻止岩石堆积在屏幕底部并在休息时移除它们(不移动)。

这是创建岩石的代码。如果您需要任何其他代码,请告诉我:))

-(void)addRock {

    SKSpriteNode *rock = [SKSpriteNode spriteNodeWithImageNamed:@"asteroid"];
    rock.position = CGPointMake ([self makeRandomXWBetween:0 and:self.size.width], self.size.height);
    rock.name = @"rock";
    rock.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:rock.size];
    rock.physicsBody.usesPreciseCollisionDetection = YES;
    rock.physicsBody.categoryBitMask = rockCategory;
    rock.physicsBody.contactTestBitMask = dodgerCategory;
    [self addChild:rock]; 
}

-(void)makeRocks {

    SKAction *makeRocks = [SKAction sequence: @[
                                                [SKAction performSelector:@selector(addRock) onTarget:self],
                                                [SKAction waitForDuration:0.3 withRange:0.0]
                                                ]];
    [self runAction: [SKAction repeatActionForever:makeRocks]];
}

提前感谢您的帮助!

最诚挚的问候, 路易斯。

1 个答案:

答案 0 :(得分:1)

由于您希望岩石在休息时被移除,因此当它们的velocity属性为零时可以移除它们。

您可以使用-update方法进行检查。

-(void)update:(CFTimeInterval)currentTime {

    [self enumerateChildNodesWithName:@"rock" usingBlock:^(SKNode *node, BOOL *stop){

        if ((node.physicsBody.velocity.dx == 0) && (node.physicsBody.velocity.dy == 0) && (node.position.y < self.size.height)) {
            [node removeFromParent];
        }
    }];
}