SpriteKit enumerateChildNodesWithName 与高级搜索? [英] SpriteKit enumerateChildNodesWithName with advanced searching?

查看:73
本文介绍了SpriteKit enumerateChildNodesWithName 与高级搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码未按预期工作.根据 SPRITE KIT 编程指南,第 61 页和第 62 页可以使用正则表达式之类的语法执行高级搜索",因此,除非我误解了实现,否则这应该可行吗?

The following code doesn't work as expected. According to the SPRITE KIT PROGRAMMING GUIDE, pages 61 and 62 one may perform "advanced searches" by using regular expression like syntax, so, unless I'm misunderstanding the implementation, this should work?

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]+".

But alas, the nodes do not go away. If I specify an exact name (like @"CIRCLE_10_10") it works, but the wildcard expression * doesn't seem to, nor does something like this @"CIRCLE_[0-9]+_[0-9]+" -- not even if I use slashes @"/CIRCLE_[0-9]+_[0-9]+".

我做错了什么?

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

EDIT: THIS WORKS and I could implement regular expression matching instead of substring'ing, but hoping to get the Sprite Kit implementation working (ideally).

[[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 中尝试了以下代码变体.

I tried the following variation of your code in both the current and beta versions of Xcode on both iOS and OS-X.

    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 上,它们只适用于开发者预览版.

All of your sample expressions work on the Mac in both versions of the SDK. On iOS, however, they only worked in the Developer Preview.

这篇关于SpriteKit enumerateChildNodesWithName 与高级搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆