跨多个场景移动 SKSpriteNode 位置

Move SKSpriteNode position across multiple scenes

我有两个场景,其中云 (SKSpriteNodes) 在 x 轴上移动。当我改变场景时,它们会重置到初始位置。

移动场景时如何将x坐标传递给新场景?

这是我的代码。两个场景的代码和结构相同

谢谢大家!

override func didMoveToView(view: SKView) {

    addScene()
    addMenuButtons()
    addSocial()

}

override func update(currentTime: CFTimeInterval) {

    if self.cloud01.position.x < self.cloud01.size.width * -1 {
        self.cloud01.position.x = self.frame.size.width + (self.cloud01.size.width / 2)
    } else {
        self.cloud01.position.x -= 0.5
    }

    if self.cloud02.position.x < self.cloud02.size.width * -1 {
        self.cloud02.position.x = self.frame.size.width + (self.cloud02.size.width / 2)
    } else {
        self.cloud02.position.x -= 0.3
    }

    if self.cloud03.position.x < self.cloud03.size.width * -1 {
        self.cloud03.position.x = self.frame.size.width + (self.cloud03.size.width / 2)
    } else {
        self.cloud03.position.x -= 0.4
    }
}

func addScene() {

    self.cloud01.anchorPoint = CGPointMake(0.5, 0.5)
    self.cloud01.size.width = self.frame.size.width / 3
    self.cloud01.size.height = self.cloud01.size.width / 5
    self.cloud01.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMaxY(self.frame) - 50)

    self.cloud02.anchorPoint = CGPointMake(0.5, 0.5)
    self.cloud02.size.width = self.frame.size.width / 3
    self.cloud02.size.height = self.cloud01.size.width / 5
    self.cloud02.position = CGPointMake(CGRectGetMaxX(self.frame) - 50, CGRectGetMaxY(self.frame) - 200)

    self.cloud03.anchorPoint = CGPointMake(0.5, 0.5)
    self.cloud03.size.width = self.frame.size.width / 3
    self.cloud03.size.height = self.cloud01.size.width / 5
    self.cloud03.position = CGPointMake(CGRectGetMinX(self.frame) + 50, CGRectGetMaxY(self.frame) - 125)

    self.addChild(self.cloud01)
    self.addChild(self.cloud02)
    self.addChild(self.cloud03)
}

当您在第一个场景中按下按钮时,您可以将云的位置存储到 NSUserDefaults 这样进入 touchBegan 方法:

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    /* Called when a touch begins */

    for touch in (touches as! Set<UITouch>) {
        let location = touch.locationInNode(self)

        if self.nodeAtPoint(location) == self.playButton {

            let cloud1Pos = cloud01.position
            let cloud2Pos = cloud02.position
            let cloud3Pos = cloud03.position

            NSUserDefaults().setObject(NSStringFromCGPoint(cloud1Pos), forKey: "cloud1Pos")
            NSUserDefaults().setObject(NSStringFromCGPoint(cloud2Pos), forKey: "cloud2Pos")
            NSUserDefaults().setObject(NSStringFromCGPoint(cloud3Pos), forKey: "cloud3Pos")

            let reveal = SKTransition.flipHorizontalWithDuration(0.5)
            let letsPlay = playScene(size: self.size)
            self.view?.presentScene(letsPlay, transition: reveal)
        }
    }
}

之后,当新场景加载到视图中时,您可以读取存储的值:

let cloud1Pos = CGPointFromString(NSUserDefaults().objectForKey("cloud1Pos") as! String)
let cloud2Pos = CGPointFromString(NSUserDefaults().objectForKey("cloud2Pos") as! String)
let cloud3Pos = CGPointFromString(NSUserDefaults().objectForKey("cloud3Pos") as! String)

现在您拥有所有云的位置,因此您可以将该位置分配给您的云:

func addScene() {

    self.cloud01.anchorPoint = CGPointMake(0.5, 0.5)
    self.cloud01.size.width = self.frame.size.width / 3
    self.cloud01.size.height = self.cloud01.size.width / 5
    self.cloud01.position = cloud1Pos

    self.cloud02.anchorPoint = CGPointMake(0.5, 0.5)
    self.cloud02.size.width = self.frame.size.width / 3
    self.cloud02.size.height = self.cloud01.size.width / 5
    self.cloud02.position = cloud2Pos

    self.cloud03.anchorPoint = CGPointMake(0.5, 0.5)
    self.cloud03.size.width = self.frame.size.width / 3
    self.cloud03.size.height = self.cloud01.size.width / 5
    self.cloud03.position = cloud3Pos

    self.addChild(self.cloud01)
    self.addChild(self.cloud02)
    self.addChild(self.cloud03)
}

还有一个建议给你。不要通过 Update 方法更新云位置,而不是你可以创建一个像 updatePosOfCloud 这样的函数,你可以 运行 将那个函数 didMoveToView 这样的方法:

runAction(SKAction.repeatActionForever(SKAction.sequence([SKAction.runBlock(updatePosOfCloud), SKAction.waitForDuration(0.05)])))

我在示例项目中已经这样做了。

HERE 是完整的工作项目。

编辑:

正如 Paulw11 所建议的,您也可以这样做:

在第一个场景的 class 声明之上声明全局变量:

var cloud1Position : CGPoint?
var cloud2Position : CGPoint?
var cloud3Position : CGPoint?

这可以存储您的云位置,当按下按钮时,您可以通过这种方式将云的位置分配给此变量:

cloud1Position = cloud01.position
cloud2Position = cloud02.position
cloud3Position = cloud03.position

在您的第二个场景中,您可以为云分配位置:

self.cloud01.position = cloud1Position!
self.cloud02.position = cloud2Position!
self.cloud03.position = cloud3Position!

您可以使用任何方式来实现您的要求。