我有两个场景,云彩(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)
}发布于 2015-05-16 07:49:12
当您按下第一个场景中的按钮时,可以将云的位置存储到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)
}还有一个建议给你。不要从更新方法中更新云位置,而是可以创建一个类似于updatePosOfCloud的函数,并且可以这样将该函数运行到didMoveToView方法中:
runAction(SKAction.repeatActionForever(SKAction.sequence([SKAction.runBlock(updatePosOfCloud), SKAction.waitForDuration(0.05)])))我已经在我的样本项目中这样做了。
这里是一个完整的工作项目。
编辑:
正如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!您可以使用任何方法来实现您的需求。
https://stackoverflow.com/questions/30272703
复制相似问题