自定义 SKSpriteNode addChild 不工作

Custom SKSpriteNode addChild not working

我正在尝试使用 SpriteKit 和 Swift 开发一款需要在所有场景中具有共同背景的游戏。由于背景很常见并且它的动作需要连续,我创建了一个自定义的单例子 class of SKSpriteNode 像这样:

class BackgroundNode: SKSpriteNode {
    static let sharedBackground = BackgroundNode()

    private init()
    {
        let texture = SKTexture(imageNamed: "Background")
        super.init(texture: texture, color: UIColor.clearColor(), size: texture.size())
        addActors()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    private func addActors() {
        addClouds()
    }

    private func addClouds() {
        addCloud1()
    }

    private func addCloud1() {
        let cloud1 = SKSpriteNode(imageNamed: "Cloud1")
        cloud1.size = CGSizeMake(0.41953125*self.size.width, 0.225*self.size.height)
        cloud1.position = CGPointMake(-self.size.width/2, 0.7*self.size.height)
        self.addChild(cloud1)
        let moveAction = SKAction.moveTo(CGPointMake(self.size.width/2, 0.7*self.size.height), duration: 20)
        cloud1.runAction(SKAction.repeatActionForever(moveAction))
    }
}

然后从 GameScene class 我将此节点添加到当前视图,如下所示:

class GameScene: SKScene {
    override func didMoveToView(view: SKView) {
        BackgroundNode.sharedBackground.size = CGSizeMake(self.size.width, self.size.height)
        BackgroundNode.sharedBackground.position = CGPointMake(self.size.width/2, self.size.height/2)
        addChild(BackgroundNode.sharedBackground)
    }
}

背景图像显示正确,但未添加云。从上面的代码开始云应该出现在屏幕外并通过另一边动画进入屏幕然后再次离开屏幕,但为了验证它是否被添加,我什至尝试将云添加到屏幕中央而不用任何动画。云还是没有出现。这可能是什么问题?以及如何修复它?

编辑

我发现 child 实际上正在添加,但正在添加并移动到屏幕上方的一些点。我还发现它可能与云的锚点有关,但无论我将哪个值设置为锚点,云始终保持在屏幕的右上角。我该怎么做锚点才能使云看起来像它应该出现的那样(考虑左下角为(0, 0)是我想要的)

问题已解决。问题是我必须手动设置场景和节点的锚点。将场景和节点的锚点设置为 (0, 0) 解决了这个问题。新代码如下所示:

游戏场景

override func didMoveToView(view: SKView) {
    anchorPoint = CGPointMake(0, 0) //Added this
    BackgroundNode.sharedBackground.size = CGSizeMake(self.size.width, self.size.height)
    BackgroundNode.sharedBackground.position = CGPointMake(0, 0)
    addChild(BackgroundNode.sharedBackground)
}

背景节点

private init()
{
    let texture = SKTexture(imageNamed: "Background")
    super.init(texture: texture, color: UIColor.clearColor(), size: texture.size())
    anchorPoint = CGPointMake(0, 0) //Added this
    addActors()
}