Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 自定义SKSpriteNode addChild不工作_Ios_Swift_Sprite Kit_Skspritenode - Fatal编程技术网

Ios 自定义SKSpriteNode addChild不工作

Ios 自定义SKSpriteNode addChild不工作,ios,swift,sprite-kit,skspritenode,Ios,Swift,Sprite Kit,Skspritenode,我正在尝试开发一款游戏,它需要使用SpriteKit和Swift在所有场景中有一个共同的背景。由于背景是公共的,其操作需要连续,因此我创建了一个自定义的单例子类SKSpriteNode,如下所示: class BackgroundNode: SKSpriteNode { static let sharedBackground = BackgroundNode() private init() { let texture = SKTexture(image

我正在尝试开发一款游戏,它需要使用SpriteKit和Swift在所有场景中有一个共同的背景。由于背景是公共的,其操作需要连续,因此我创建了一个自定义的单例子类
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))
    }
}
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)
    }
}
游戏场景
类中,我将此节点添加到当前视图中,如下所示:

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))
    }
}
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)
    }
}
背景图像显示正确,但未添加云。在上面的代码中,云应该出现在屏幕外,并通过另一个边缘在屏幕内设置动画,然后再从屏幕外设置动画,但是为了验证是否添加了云,我甚至尝试在没有任何动画的情况下将云添加到屏幕中心。云还是没有出现。这里有什么问题?如何修复它

编辑


我发现这个孩子实际上是在被添加,但是正在被添加,并且在屏幕上方的一些点上移动。我还发现它可能与云的锚定点有关,但无论我设置为锚定点的值是什么,云始终保持在屏幕的右上角。我可以对锚定点做些什么,以使云看起来像它应该做的那样(将左下角视为(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()
}
背景节点

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()
}

需要一点时间才能将我自己的答案标记为已解决。:-)