【问题标题】:SpriteKit enumerateChildNodesWithName with advanced searching?SpriteKit enumerateChildNodesWithName 与高级搜索?
【发布时间】:2013-12-20 20:26:02
【问题描述】:

以下代码无法按预期工作。根据 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]+" 也不起作用。

我做错了什么?

编辑: 这可行,我可以实现正则表达式匹配而不是子字符串,但希望让 Sprite Kit 实现工作(理想情况下)。

[[self children] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    SKNode *node = (SKNode *)obj;
    if ([[node.name substringWithRange:NSMakeRange(0, 7)] isEqual: @"CIRCLE_"]) {
        [node removeFromParent];
    }
}];

【问题讨论】:

  • 对我来说看起来很合适。试试@"/CIRCLE_" 或@"//CIRCLE_" 看看会怎样。确保圆形节点实际上是您要向其发送 enumerate/childNodeWithName 消息的节点(自身)的直接子节点。
  • 这也不起作用,之前也尝试过(省略星号)并使用斜杠(根据我的 perl 正则表达式经验)。
  • 感谢LearnCocos2D;是的,它们是 self 的子节点。事实上,在我上面的第一个编辑中,我展示了一个在引用 self [self children] 时确实有效的块 :)
  • 并且您还没有运行 removeFromParent 代码片段,无论出于何种原因,当您仍在尝试在方法中的某个位置找到这些节点时,该代码片段现在仍然有效? ;)
  • 对不起,我不明白你的问题。

标签: objective-c sprite-kit


【解决方案1】:

我在 iOS 和 OS-X 上的 Xcode 当前版本和 beta 版本中都尝试了您的代码的以下变体。

    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];

您的所有示例表达式都可以在两个版本的 SDK 中的 Mac 上运行。然而,在 iOS 上,它们只在开发者预览版中工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-11
    • 2018-07-13
    • 2015-04-17
    • 2012-04-09
    • 2015-04-06
    相关资源
    最近更新 更多