我想知道应该添加什么代码来阻止岩石堆积在屏幕底部,并在静止(不移动)时将其移除。
这是创建岩石的代码。如果您需要任何其他代码,请告诉我:)
-(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]];
}
在此先感谢您的帮助!
最好的祝福,
路易
最佳答案
由于您希望岩石在静止时被移除,因此可以在其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];
}
}];
}
关于ios - SpriteKit-停止对象堆积,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23384778/