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
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 (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
//Edit1: Tried to use Arrays of CGPoints instead of CGPath, sadly it didn´t change anything in the Performance. Code:
func createTwoFloorTrianglesAtLocation(xPos:CGFloat, yPos:CGFloat, width:CGFloat, height:CGFloat, orientation:Bool) -> [SKShapeNode] {
var p1:[CGPoint]
var p2:[CGPoint]
if orientation {
p1 = [CGPoint(x: xPos, y: yPos), CGPoint(x: xPos, y: yPos + height), CGPoint(x: xPos + width, y: yPos)]
p2 = [CGPoint(x: xPos + width, y: yPos), CGPoint(x: xPos + width, y: yPos + height), CGPoint(x: xPos, y: yPos + height)]
} else {
p1 = [CGPoint(x: xPos, y: yPos), CGPoint(x: xPos + width, y: yPos + height), CGPoint(x: xPos, y: yPos + height)]
p2 = [CGPoint(x: xPos + width, y: yPos), CGPoint(x: xPos, y: yPos), CGPoint(x: xPos + width, y: yPos + height)]
}
let triangle1 = SKShapeNode(points: &p1, count: p1.count)
triangle1.fillColor = groundColors[GKRandomSource.sharedRandom().nextInt(upperBound: groundColors.count)]!
triangle1.name = "colorSprite"
triangle1.lineWidth = 0.0
let triangle2 = SKShapeNode(points: &p2, count: p2.count)
triangle2.fillColor = groundColors[GKRandomSource.sharedRandom().nextInt(upperBound: groundColors.count)]!
triangle2.name = "colorSprite"
triangle2.lineWidth = 0.0
return [triangle1, triangle2]
}