一次抽奖中有多个skshapenode?

时间:2013-12-27 20:42:38

标签: ios ios7 sprite-kit skshapenode

您好我已经绘制了连接图形中节点的skshapenodes,用于我的视觉*表示。

for(int x=0; x<64; x++)
        {
            for(int y=0; y<48; y++)
            {
                PathFindingNode *node = [[gridToDraw objectAtIndex:x] objectAtIndex:y];
                for(int i=0; i< node.connections.count; i++)
                {
                    PathFindingNode *neighbor = [node.connections objectAtIndex:i];

                    SKShapeNode *line = [SKShapeNode node];
                    CGMutablePathRef pathToDraw = CGPathCreateMutable();
                    CGPathMoveToPoint(pathToDraw, NULL, node.position.x, node.position.y);
                    CGPathAddLineToPoint(pathToDraw, NULL, neighbor.position.x, neighbor.position.y);
                    line.path = pathToDraw;
                    line.lineWidth = 0.1f;
                    [line setStrokeColor:[UIColor blueColor]];
                    line.alpha = 0.1f;
                    [self addChild:line];
                }
            }
        }

我的图表中有很多节点,这吸引了近22,000个形状。有没有办法可以在一次绘制调用中绘制这些形状,因为它们都是相同的,唯一的区别是它们的起点和终点位置。

如果我使用了一个纹理,它将被加载一次,我怎么能改变它的旋转来加入我的所有节点,如上所述。

此致

更新:

SKShapeNode *line = [SKShapeNode node];
        CGMutablePathRef pathToDraw = CGPathCreateMutable();

        for(int x=0; x<64; x++)
        {
            for(int y=0; y<48; y++)
            {
                PathFindingNode *node = [[gridToDraw objectAtIndex:x] objectAtIndex:y];
                for(int i=0; i< node.connections.count; i++)
                {
                    PathFindingNode *neighbor = [node.connections objectAtIndex:i];
                    CGPathMoveToPoint(pathToDraw, NULL, node.position.x, node.position.y);
                    CGPathAddLineToPoint(pathToDraw, NULL, neighbor.position.x, neighbor.position.y);
                }
            }
        }

        line.path = pathToDraw;
        line.lineWidth = 0.1f;
        [line setStrokeColor:[UIColor blueColor]];
        line.alpha = 0.1f;
        [self addChild:line];

我已经更新了我的代码,如上所示,这绘制了一个sknode,但它绘制了185次,为什么会这样?

1 个答案:

答案 0 :(得分:4)

我昨天将在一个类似的问题上回答这个问题,但它已经消失了。

您可以使用SKShapeNode创建一个路径并将所有内容绘制为一个CGPathAddPath,以便合并路径。

以下是一个例子:

SKEffectNode *effectNode = [[SKEffectNode alloc]init];
SKShapeNode  *shape = [[SKShapeNode alloc] init];
CGMutablePathRef myPath = CGPathCreateMutable();
CGPathAddArc(myPath, NULL, 0,0, 15, 0, M_PI*2, YES);

CGMutablePathRef pathTwo = CGPathCreateMutable();
CGPathAddArc(pathTwo, NULL, 50,0, 15, 0, M_PI*2, YES);     
CGPathAddPath(myPath, NULL, pathTwo);

CGMutablePathRef pathThree = CGPathCreateMutable();
CGPathAddArc(pathThree, NULL, 100,0, 15, 0, M_PI*2, YES);
CGPathAddPath(myPath, NULL, pathThree);

shape.path = myPath;

[effectNode addChild:shape];
[self addChild:effectNode];

effectNode.shouldRasterize = YES;  // if you don't rasterize it's horrifically slow.

这只是一个如何合并不同路径以创建一个SKShapeNode的示例,其中包含您正在寻找的所有视觉元素。您需要将此概念应用于您的代码。

我将生成的SKShapeNode添加到SKEffectNode,以便我可以利用shouldRasterize属性来缓存形状,否则您的帧率将非常糟糕。

相关问题