我一直在试验SKCameraNode,但我找不到任何关于用它创建视差效果的东西. 假设我添加上面的代码,并在baseNode下面添加一个后台节点.然后我动画我的相机. let behindNode = SKNode()addChild(behindNo
假设我添加上面的代码,并在baseNode下面添加一个后台节点.然后我动画我的相机.
let behindNode = SKNode() addChild(behindNode) let blueSquare = SKSpriteNode(color: SKColor.blueColor(), size: CGSize(width: 80, height: 80)) blueSquare.position = CGPoint(x: size.width/2+40, y: size.height/2+40) behindNode.addChild(blueSquare) let baseNode = SKNode() addChild(baseNode) let redSquare = SKSpriteNode(color: SKColor.redColor(), size: CGSize(width: 80, height: 80)) redSquare.position = CGPoint(x: size.width/2, y: size.height/2) baseNode.addChild(redSquare) let cameraNode = SKCameraNode() camera = cameraNode addChild(camera!) camera!.position = redSquare.position cameraNode.runAction(SKAction.sequence([ SKAction.waitForDuration(1), SKAction.moveBy(CGVector(dx: 100, dy: 100), duration: 3), SKAction.moveBy(CGVector(dx: -100, dy: 100), duration: 1), SKAction.moveBy(CGVector(dx: 100, dy: -100), duration: 1) ]))
当相机移动时,我需要蓝色方块相对于红色方块以较慢的速度移动..我希望cameranode有相对位置字典或其他东西,但它不会…
我创建了一个不处理秒或帧的解决方案.您可以创建SKCameraNode的子类,并使用Swift的didSet()方法,该方法在更改类变量后调用.在我们的例子中,我们想要监听摄像机节点位置变量的变化.
class CameraNodeParallax:SKCameraNode{ override var position : CGPoint { didSet { // Move our backgrounds } }
我们还希望创建一个包含所有视差背景节点的阵列,以及一个包含相对于摄像机移动速度的相应移动速度的阵列.
var backgroundNodes:[SKNode] = [] var backgroundNodesSpeedFactor:[CGVector] = [] // in relation to camera nodes speed func addParallaxBackgroundNode(background:SKNode, vector:CGVector) { backgroundNodes.append(background) backgroundNodesSpeedFactor.append(vector) }
填充我们的新变量的init()也可能很有用
init(position:CGPoint) { super.init() self.position = position }
现在我们可以填充didSet()监听器:
// Move our backgrounds var i = 0 for node in backgroundNodes { let positionChangeX = position.x-oldValue.x let positionChangeY = position.y-oldValue.y let changeX = positionChangeX*backgroundNodesSpeedFactor[i].dx let changeY = positionChangeY*backgroundNodesSpeedFactor[i].dy node.position = CGPointMake(node.position.x+changeX,node.position.y+changeY) i += 1 }
最终代码如下所示:
class CameraNodeParallax:SKCameraNode{ var backgroundNodes:[SKNode] = [] var backgroundNodesSpeedFactor:[CGVector] = [] // in relation to camera nodes speed init(position:CGPoint) { super.init() self.position = position } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } override var position : CGPoint { didSet { // Move your parallax backgrounds var i = 0 for node in backgroundNodes { let positionChangeX = position.x-oldValue.x let positionChangeY = position.y-oldValue.y let changeX = positionChangeX*backgroundNodesSpeedFactor[i].dx let changeY = positionChangeY*backgroundNodesSpeedFactor[i].dy node.position = CGPointMake(node.position.x+changeX,node.position.y+changeY) i += 1 } } } func addParallaxBackgroundNode(background:SKNode, vector:CGVector) { backgroundNodes.append(background) backgroundNodesSpeedFactor.append(vector) } }
我在https://github.com/gitmalong/lovelySpriteKitHelpers/blob/master/ParallaxBackgroundMover.swift创建了一个不从SKCameraNode继承的替代实现