Keep relative positions of SKSpriteNode from SKShapeNode from CGPath

混江龙づ霸主 提交于 2019-12-24 00:46:44

问题


I have an array of array of coordinates (from a shapefile), which I'm trying to make into a series of SKSpriteNodes.

My problem is that I need to keep the relative positions of each of the shapes in the array. If I use SKShapeNodes, it works, as they are just created directly from the path I trace, but their resources consumption is quite high, and, in particular I cannot use lighting effects on them.

If I use SKSpriteNodes with a texture created from the shape node, then I lose their relative positions.

I tried calculating the center of each shapes, but their positions are still not accurate.

Here's how I draw them so far:

override func didMoveToView(view: SKView)
{
    self.backgroundColor = SKColor.blackColor()
    let shapeCoordinates:[[(CGFloat, CGFloat)]] = [[(900.66563867095, 401.330302084953), (880.569690215615, 400.455067051099), (879.599839322167, 408.266821560754), (878.358968429675, 418.182833936104), (899.37522863267, 418.54861454054), (900.66563867095, 401.330302084953)],
        [(879.599839322167, 408.266821560754), (869.991637153925, 408.122907880045), (870.320569111933, 400.161243286459), (868.569953361733, 400.11339198742), (864.517810669155, 399.54973007215), (858.682258706015, 397.619367903278), (855.665753299048, 395.808813873244), (853.479452218432, 392.811211835046), (847.923492419877, 394.273974470316), (834.320860167515, 397.859104108813), (826.495867917475, 399.921507079808), (829.86572598778, 404.531781837208), (835.898936154083, 409.178035013947), (840.887737516875, 411.839958392806), (847.191868005112, 414.441797809335), (854.251943938193, 416.198384209245), (860.095769038325, 417.277496957155), (866.21091316512, 417.954970608037), (873.27118845149, 418.182833936104), (878.358968429675, 418.182833936104), (879.599839322167, 408.266821560754)],
        [(931.018881691707, 402.151689416542), (910.610746904717, 401.600140235583), (910.380693886848, 411.576056681467), (930.79710181083, 411.750012342223), (931.018881691707, 402.151689416542)],
        [(880.569690215615, 400.455067051099), (870.320569111933, 400.161243286459), (869.991637153925, 408.122907880045), (879.599839322167, 408.266821560754), (880.569690215615, 400.455067051099)]]

    for shapeCoord in shapeCoordinates
    {
        let path = CGPathCreateMutable()
        var center:(CGFloat, CGFloat) = (0, 0)

        for i in 0...(shapeCoord.count - 1)
        {
            let x = shapeCoord[i].0
            let y = shapeCoord[i].1
            center.0 += x
            center.1 += y

            if i == 0
            {
                CGPathMoveToPoint(path, nil, x, y)
            }
            else
            {
                CGPathAddLineToPoint(path, nil, x, y)
            }
        }

        center.0 /= CGFloat(shapeCoord.count)
        center.1 /= CGFloat(shapeCoord.count)

        let shape = SKShapeNode(path: path)
        let texture = self.view?.textureFromNode(shape)
        let sprite = SKSpriteNode(texture: texture)
        sprite.position = CGPointMake(center.0, center.1)

        //self.addChild(shape)
        self.addChild(sprite)
    }

}

Is it feasible or should I switch to another technology / method?

来源:https://stackoverflow.com/questions/33382469/keep-relative-positions-of-skspritenode-from-skshapenode-from-cgpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!