假设我添加上面的代码,并在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(),height: 80))redSquare.position = CGPoint(x: size.wIDth/2,y: size.height/2)baseNode.addChild(redSquare)let cameraNode = SKCameraNode()camera = cameraNodeaddChild(camera!)camera!.position = redSquare.positioncameraNode.runAction(SKAction.sequence([ SKAction.waitForDuration(1),SKAction.moveBy(CGVector(dx: 100,dy: 100),duration: 3),SKAction.moveBy(CGVector(dx: -100,duration: 1),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 speedfunc addParallaxBackgroundNode(background:SKNode,vector:CGVector) { backgroundNodes.append(background) backgroundNodesspeedFactor.append(vector)}
填充我们的新变量的init()也可能很有用
init(position:CGPoint) { super.init() self.position = position}
现在我们可以填充dIDSet()监听器:
// Move our backgroundsvar i = 0for 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继承的替代实现
总结以上是内存溢出为你收集整理的ios – SpriteKit – SKCameraNode视差效果?全部内容,希望文章能够帮你解决ios – SpriteKit – SKCameraNode视差效果?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)