2

i am developing a game using Swift and SpriteKit. I want that the background changes at a certain score. Here is the code:

 class GameScene: SKScene {
 var bg = SKSpriteNode()

override func didMoveToView(view: SKView) {

   makeBg()
}

func makeBg() {

    let bgTexture = SKTexture(imageNamed: "img/bg.png")
    let moveBg = SKAction.moveByX(-bgTexture.size().width, y: 0, duration: 9)
    let replaceBg = SKAction.moveByX(bgTexture.size().width, y: 0, duration:0)
    let animateBg = SKAction.repeatActionForever(SKAction.sequence([moveBg,   replaceBg]))


    for var i: CGFloat = 0; i<3; i++ {
        let bg = SKSpriteNode(texture: bgTexture)
        bg.name = "background"
        bg.position = CGPoint(x: bgTexture.size().width/2 + bgTexture.size().width * i, y: CGRectGetMidY(self.frame))
        bg.size.height = self.frame.height
        bg.runAction(animateBg)
        addChild(bg)
    }
}     
   override func touchesBegan(touches: Set<UITouch>, withEvent event:    UIEvent?) {
     if score == 0 {
               bg.texture = SKTexture(imageNamed: "img/bg.png")
            } else if score == 3 {
                bg.texture = SKTexture(imageNamed: "img/bgOri.png")
                }
 }

But image doesn't change...where is the mistake?

Swift1
  • 349
  • 1
  • 4
  • 22
  • 1
    Have you checked if the code inside that if / else block is actually executed? Also, you use `bgTexture` and `bg.texture` inside your touchesBegan... That looks suspicious... – Whirlwind Mar 05 '16 at 01:41
  • Yes the code is executed...the problem is that i can't change background image while scene is running...please help me. I am able to change background image only if i change completely my code...declaring var bg = SKSpriteNode(imageNamed: "img/bg.png") in gamescene...but leaving background static without scrolling. – Swift1 Mar 05 '16 at 15:20
  • 1
    Okay, that's the start. Let me try to recreate the issue ... Do you see by any chance MissingResource image at some point (white box with red X in it) ? – Whirlwind Mar 05 '16 at 15:22
  • No i don't see red X...i see red X only if i don't declare bg = SKSpriteNode(texture: bgTexture) in the for var i cycle...but with the code of this post, image is loaded correctly...the problem is i can't change it at a certain score – Swift1 Mar 05 '16 at 15:28
  • 1
    Note that you don't need extension (you can remove .png from filename). Anyways, to change a texture on a bg node works for me, and I guess it works for you. But you have added three nodes and you are changing a texture on just one node. Other two nodes are not referenced, nor accessed by its name, so there is no way currently to change their texture. Still, it is a bit unclear to me, on which node you are trying to change the texture. – Whirlwind Mar 05 '16 at 15:39
  • i am trying to change bg texture value inside "for var i" cycle...but perhaps it's impossible to do that... – Swift1 Mar 05 '16 at 15:49
  • i should change my code...but i want my background scrolling...and change it while it's scrolling...it's so sad a static background. The code inside the "for cycle" is the only that give me a good fluidity... – Swift1 Mar 05 '16 at 15:52
  • 1
    I made an example for you...Check out my answer. – Whirlwind Mar 05 '16 at 16:24
  • 1
    You should not edit questions / answers like you did. You've removed a lot of your original code (and tried to edit my answer as well). That way, answers / comments may become confusing to future readers (because answer and comments are based on your original question, not to this edit). – Whirlwind Mar 08 '16 at 11:10
  • I did it only to focus the part of the code that solved the problem...not for other reasons. However, thank you for the observation – Swift1 Mar 08 '16 at 11:20
  • 1
    I understand, but still, you shouldn't do it like this, and I suggest you to revert the edit. My answer / comments are based on the code you've removed. That code illustrates what was happening (how nodes were created, that nodes haven't had their names). If you have doubts , you can ask on [Meta](http://meta.stackexchange.com/) about this particular case, but still, I would say, that this edit is inappropriate. I haven't searched a lot, but [this](http://meta.stackoverflow.com/a/281463/3402095) might be helpful. – Whirlwind Mar 08 '16 at 11:42
  • 1
    Also, useful stuff to read http://stackoverflow.com/help/privileges/edit – Whirlwind Mar 08 '16 at 11:42
  • Ok, i understood and i reverted the edit. Thank you for the explanation and sorry for the mistake – Swift1 Mar 08 '16 at 12:47

1 Answers1

3

So this is how you can change a texture on all the nodes created inside your for loop:

class GameScene: SKScene {
    override func didMoveToView(view: SKView) {
       makeBg()
    }

    func makeBg() {
        let bgTexture = SKTexture(imageNamed: "img/bg.png")
        let moveBg = SKAction.moveByX(-bgTexture.size().width, y: 0, duration: 9)
        let replaceBg = SKAction.moveByX(bgTexture.size().width, y: 0, duration:0)
        let animateBg = SKAction.repeatActionForever(SKAction.sequence([moveBg,   replaceBg]))

        for var i: CGFloat = 0; i<3; i++ {
            let bg = SKSpriteNode(texture: bgTexture)
            bg.name = "background"
            bg.position = CGPoint(x: bgTexture.size().width/2 + bgTexture.size().width * i, y: CGRectGetMidY(self.frame))
            bg.size.height = self.frame.height
            bg.runAction(animateBg)

            addChild(bg)
        }
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        enumerateChildNodesWithName("background", usingBlock: { node, stop in
            //Make a check based on score and
            (node as? SKSpriteNode)?.texture = //set the right texture
        })
    }
}

Note that you don't need bg property defined in the GameScene to accomplish this.

DR Haus
  • 652
  • 1
  • 7
  • 12
Whirlwind
  • 14,286
  • 11
  • 68
  • 157