vendredi 10 juin 2016

Endless background spritekit


I'm trying to make endless background with the method from book 2D iOS & tvOS Games by Ray Wenderlich and my game crash with reason "Attemped to add a SKNode which already has a parent" that happened because the child is already on scene and the loop try to add it again, but I don't know how to fix it.

Thanks!

    func backgroundNode() -> SKSpriteNode {
    // 1
    let backgroundNode = SKSpriteNode()
    backgroundNode.anchorPoint = CGPoint.zero
    backgroundNode.name = "background"
    // 2
    background1.anchorPoint = CGPoint.zero
    background1.size = self.frame.size
    background1.position = CGPoint(x: 0, y: 0)
    backgroundNode.addChild(background1)
    // 3
    background2.anchorPoint = CGPoint.zero
    background1.size = self.frame.size
    background2.position =
        CGPoint(x: background1.size.width, y: 0)
    backgroundNode.addChild(background2)
    // 4
    backgroundNode.size = CGSize(
        width: background1.size.width + background2.size.width,
        height: background1.size.height)

    return backgroundNode
}

for i in 0...1 {
let background = backgroundNode()
background.anchorPoint = CGPointZero
background.position =
CGPoint(x: CGFloat(i)*background.size.width, y: 0)
background.name = "background"
addChild(background)
}

Aucun commentaire:

Enregistrer un commentaire