Search code examples
swiftxcodesprite-kitspriteskspritenode

multiple sprite nodes, only one appears


1 image of output located here

Im pretty new to coding in swift and the code 'works' on the sense that one of the sprites will appear in this case the "play button" however I cannot get the ground to appear at all.

I don't know if this is simply because I am taking the wrong approach or using the wrong method but after a bit of research I am still stuck so I turn to you guys.

I have included the code below (I've included the entire document however the key area will be the didMove to view section, I included the rest as it may be relevant). as I say one of the sprites does appear but the other does not(no matter how I alter the values).

Any help would be greatly appreciated.

public class GameScene: SKScene {

    let background = UIImage(named: "Sky")
    let playButton = SKSpriteNode(imageNamed: "Play Button")
    let ground = SKSpriteNode(imageNamed: "Ground")
    let groundPoint = CGPoint(x: 0, y:2)

    override public func didMove(to view: SKView) {

        self.playButton.position = CGPoint(x: 1, y: 2)
        self.ground.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 3)

        self.addChild(playButton)
        self.addChild(ground)

        self.backgroundColor = UIColor.white
    }

    override public func touchesBegan(_ touches: Set<UITouch>, with event: 
    UIEvent?) {
        for touch: AnyObject in touches{
            let location = touch.location(in: self)
            if (self.atPoint(location) == self.playButton) {
                let skView = self.view as! SKView
                skView.ignoresSiblingOrder = true

                let scene = PlayScene(size: self.size)
                scene.scaleMode = .resizeFill
                scene.size = skView.bounds.size

                skView.presentScene(scene)
            }   
        }
    }
}

Solution

  • I am assuming that you are not understanding how the default anchor system works in Spritekit.

    enter image description here

    By default the scene and all objects are created with an anchorPoint 0.5, 0,5. which puts all new objects in the direct center of the screen.

    so you setting a position on your play button of CGPoint(x: 1, y: 2) is setting the play button 3 pixels off from the center (not very much).

    ASSUMING you want the ground on the bottom of the scene, and your play button in the center of the screen you would lay it out like so

    playButton.position = CGPoint(x: 0, y: 0) //or CGPoint.zero or nothing at all since by default it will get placed in the center
    playButton.zPosition = 1
    self.addChild(playButton)
    
    ground.position = CGPoint(x: 0, y: 0 - self.size.height / 2 + ground.size.height / 2)
    ground.zPosition = 1
    self.addChild(ground)
    

    I know you have specified to ignore sibling order, but I think it a better practice to specifiy zPositions.

    worth noting you can change the scene anchor point to lower left corner as well by using

    self.anchorPoint = CGPoint.zero
    

    It is probably a case of your zPositions not being set as well as the positions being off