我有两个场景,其中云(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)
}
答案 0 :(得分:2)
当您从第一个场景按下按钮时,您可以将云的位置以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建议您也可以这样做:
将全局变量声明在第一个场景的类声明之上:
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!
您可以使用任何方式来达到您的要求。