以下代码无法按预期工作。 根据 SPRITE KIT PROGRAMMING GUIDE,第 61 页和第 62 页,人们可以使用语法等正则表达式来执行"高级搜索",所以,除非我误解了实现,否则这应该有效吗?
SKShapeNode *myCircle = [self getCircle];
myCircle.name = [NSString stringWithFormat:@"CIRCLE_%d_%d", x, y];
myCircle.position = CGPointMake(10,10);
[self addChild:myCircle];
// Lets remove ALL SKNodes that begin with the name "CIRCLE_"
[self enumerateChildNodesWithName:@"CIRCLE_*" usingBlock:^(SKNode *node, BOOL *stop) {
[node removeFromParent];
}];
但可惜的是,节点不会消失。 如果我指定一个确切的名称(如 @"CIRCLE_10_10"
),它可以工作,但通配符表达式似乎*
没有,类似的东西也没有@"CIRCLE_[0-9]+_[0-9]+"
——即使我使用斜杠@"/CIRCLE_[0-9]+_[0-9]+"
。
我做错了什么?
编辑:这有效,我可以实现正则表达式匹配而不是子字符串,但希望让精灵工具包实现正常工作(理想情况下)。
[[self children] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
SKNode *node = (SKNode *)obj;
if ([[node.name substringWithRange:NSMakeRange(0, 7)] isEqual: @"CIRCLE_"]) {
[node removeFromParent];
}
}];
我在 iOS 和 OS-X 上的当前和测试版 Xcode 中尝试了以下代码变体。
SKScene *scene = [[SKScene alloc] initWithSize:self.gameView.frame.size];
for (int x = 0; x < 20; x++)
{
for (int y = 0; y < 20; y++)
{
CGPathRef myPath = CGPathCreateWithEllipseInRect(CGRectMake(0, 0, 20, 20), NULL);
SKShapeNode *myCircle = [[SKShapeNode alloc] init];
myCircle.path = myPath;
#if TARGET_OS_IPHONE
myCircle.fillColor = [UIColor redColor];
#else
myCircle.fillColor = [NSColor redColor];
#endif
myCircle.name = [NSString stringWithFormat:@"CIRCLE_%d_%d", x, y];
myCircle.position = CGPointMake(20*x,20*y);
[scene addChild:myCircle];
CGPathRelease(myPath);
}
}
[self.gameView presentScene:scene];
所有示例表达式都可以在 Mac 上的两个版本的 SDK 中工作。但是,在iOS上,它们只能在开发人员预览版中使用。