Why creating so many SKNodes takes so long

I am creating a random spinner element for my iOS app in Swift using SpriteKit, but currently my method is not very efficient, and I am pretty sure that it may not be the best way to achieve what I am aiming for. The process that I am doing now is essentially.
- When loading a scene, swipe through a list of 13 different elements (stored in JSON)
- create a group of nodes displaying the corresponding data
- Add each group of nodes to the second group of nodes, slightly moving them along the x axis, creating a long line SKNode (elements for the counter)
- Moving this group along the x axis to give the impression that it rotates through objects

But this method takes about 6 seconds to even load the scene (and the delay is adding the nodes to a much larger node: group.addChild(spinnerItem)and since I'm only new to Swift. I am sure there is a better way that I do this, so any help / advice is appreciated to improve this code, because it seriously needs to improve performance!

Here is the didMove function ...

override func didMove(to view: SKView) {
    var xPos = CGFloat(self.frame.width + 150)
    var eventWeapon = weapon()

    for n in 0...13{
        eventWeapon = getWeapon(caseName: "chromaCase", weaponID: n)
        print(eventWeapon)
        let texture = createSpinnerItem(wName: "\(eventWeapon.name)", wSkin: "\(eventWeapon.skin)", wColor: "\(eventWeapon.color)")
        let spinnerItem = SKSpriteNode(texture: texture)
        spinnerItem.position = CGPoint(x: xPos, y: self.frame.height / 2)
        group.addChild(spinnerItem)

        xPos = xPos + 300 * 1.2
    }

    self.addChild(group)
    group.run(SKAction.moveBy(x: -5000, y: 0, duration: 2))
}

and the function createSpinnerItemused in this ...

func createSpinnerItem(wName: String, wSkin: String, wColor: String) -> SKTexture {
    let object = SKNode()

    let bGround = SKShapeNode(rectOf: CGSize(width: 300, height: 300))
    bGround.fillColor = color.offWhite
    bGround.strokeColor = color.gray
    bGround.lineWidth = 5
    object.addChild(bGround)

    let titleBG = SKShapeNode(rectOf: CGSize(width: 300, height: 100))
    if wColor == "blue"{ titleBG.fillColor = color.CSGOblue }
    if wColor == "red"{ titleBG.fillColor = color.CSGOred }
    if wColor == "pink"{ titleBG.fillColor = color.CSGOpink }
    if wColor == "purple"{ titleBG.fillColor = color.CSGOpurple }
    titleBG.strokeColor = color.gray
    titleBG.lineWidth = 5
    titleBG.position = CGPoint(x: 0, y: -150 + 50)
    object.addChild(titleBG)

    let weaponName = SKLabelNode()
    weaponName.text = wName
    weaponName.fontColor = color.white
    weaponName.fontSize = 40
    weaponName.fontName = "Avenir-Regular"
    weaponName.verticalAlignmentMode = .center
    weaponName.position = CGPoint(x: 0, y: -150 + 70)
    object.addChild(weaponName)

    let weaponSkin = SKLabelNode()
    weaponSkin.text = wSkin
    weaponSkin.fontColor = color.white
    weaponSkin.fontSize = 20
    weaponSkin.fontName = "Avenir-Regular"
    weaponSkin.verticalAlignmentMode = .center
    weaponSkin.position = CGPoint(x: 0, y: -150 + 30)
    object.addChild(weaponSkin)

    return object
}
+4
source share
1 answer

, , - , "" , . 6 - , , . , , . , KnightOfDragon .

, , , SK SO. , . , , , , , . ? , , . JSON . , , , , , . , , , didMove viewDidLoad. , , , . , , , , . , - - .

, , , . , , . - .

, SKNode , (, addChild ). SKAction, . .

+2

Source: https://habr.com/ru/post/1659988/


All Articles