While learning and playing around with Swift and Spritekit i run into an issue:
I can´t seem to create many SKShapeNodes at Runtime.
I am trying to fill the Screen with "random" Triangles
(The green Triangle on top is just a "dummy" Triangle moving from the left to the right so i get to see the FPS)
These brown-ish Triangles are SKShapeNodes created like this:
func createTwoFloorTrianglesAtLocation(xPos:CGFloat, yPos:CGFloat, width:CGFloat, height:CGFloat, orientation:Bool) -> [SKShapeNode] {
let path1 = UIBezierPath()
let path2 = UIBezierPath()
if orientation {
path1.move(to: CGPoint(x: xPos, y: yPos))
path1.addLine(to: CGPoint(x: xPos, y: yPos + height))
path1.addLine(to: CGPoint(x: xPos + width, y: yPos))
path2.move(to: CGPoint(x: xPos + width, y: yPos))
path2.addLine(to: CGPoint(x: xPos + width, y: yPos + height))
path2.addLine(to: CGPoint(x: xPos, y: yPos + height))
} else {
path1.move(to: CGPoint(x: xPos, y: yPos))
path1.addLine(to: CGPoint(x: xPos + width, y: yPos + height))
path1.addLine(to: CGPoint(x: xPos, y: yPos + height))
path2.move(to: CGPoint(x: xPos + width, y: yPos))
path2.addLine(to: CGPoint(x: xPos, y: yPos))
path2.addLine(to: CGPoint(x: xPos + width, y: yPos + height))
}
let triangle1 = SKShapeNode(path: path1.cgPath)
triangle1.fillColor = groundColors[GKRandomSource.sharedRandom().nextInt(upperBound: groundColors.count)]!
triangle1.name = "colorSprite"
triangle1.lineWidth = 0.0
let triangle2 = SKShapeNode(path: path2.cgPath)
triangle2.fillColor = groundColors[GKRandomSource.sharedRandom().nextInt(upperBound: groundColors.count)]!
triangle2.name = "colorSprite"
triangle2.lineWidth = 0.0
return [triangle1, triangle2]
}
I add these Triangles calling #addChild(SKNode) to the GameScene.
These Triangles don´t move or anything, they are supposed to disappear later in the Game by "drawing lines" above them.
The Problem is that i get very bad FPS and a high CPU-Usage as well as a high Power-Usage in both the XCode-Simulator and on a real Device (iPhone 13 Pro Max, too).
If i reduce the Amount of Triangles it gets better, but i´d like to not do that.
Is there any way to fix this issue?
I also read online (https://stackoverflow.com/questions/23461966/create-an-sktexture-from-an-skshapenode) that some use SKSpriteNode instead of SKShapeNode.
But if use the following code to "convert" my ShapeNodes to SpriteNodes before adding them as a Child to the Scene, i see the Node-Counter in the Bottom left is also around 1000ish but there are no Rectangles rendered.
func addNodesToParent(_ stuff:[SKShapeNode]) {
for n in stuff {
addChild(SKSpriteNode(texture: self.view?.texture(from: n)))
//addChild(n)
}
}
I hope someone has an idea that i can try.
Thanks in advance,
Greetings,
Nico