N
N
Nick932015-06-24 22:13:30
iOS
Nick93, 2015-06-24 22:13:30

How to set camera tracking of an object in spritekite?

Hello. I'm trying to set the camera to track an object, but the object moves up when loading.

class GameScene: SKScene, AnalogStickProtocol {
    
    var oval: SKSpriteNode?
    var World: SKNode!
    var Camera: SKNode!
    
    let moveAnalogStick: AnalogStick = AnalogStick()
    let rotateAnalogStick: AnalogStick = AnalogStick()
    override func didMoveToView(view: SKView) {
        //camera
        
        self.World = SKNode()
        self.World.name = "World"
        
        addChild(self.World)
        self.Camera = SKNode()
        self.Camera.name = "Camera"
        self.World.addChild(self.Camera)
        
        /* Setup your scene here */
        let bgDiametr: CGFloat = 120
        let thumbDiametr: CGFloat = 60
        let joysticksRadius = bgDiametr / 2
        self.backgroundColor = SKColor.yellowColor()
        moveAnalogStick.bgNodeDiametr = bgDiametr
        moveAnalogStick.thumbNodeDiametr = thumbDiametr
        moveAnalogStick.position = CGPointMake(joysticksRadius + 15, joysticksRadius + 15)
        moveAnalogStick.delegate = self
        self.addChild(moveAnalogStick)
        rotateAnalogStick.bgNodeDiametr = bgDiametr
        rotateAnalogStick.thumbNodeDiametr = thumbDiametr
        rotateAnalogStick.position = CGPointMake(CGRectGetMaxX(self.frame) - joysticksRadius - 15, joysticksRadius + 15)
        rotateAnalogStick.delegate = self
        self.addChild(rotateAnalogStick)
        //camera 
        
       
        
        
        oval = SKSpriteNode(imageNamed: "oval2")
        
        if let aN = oval {
            aN.physicsBody = SKPhysicsBody(texture: aN.texture, size: aN.size)
            
            aN.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
            aN.physicsBody?.affectedByGravity = false;
            
            //World.insertChild(aN, atIndex: 0)
            World.addChild(aN)
            
        }
        self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
        
   
       
        if let aN = oval {
            Camera.position = CGPoint(x: aN.position.x, y: aN.position.y)
            self.centerOnNode(Camera)
        }
        
        
    }
    
    
    
    override func didFinishUpdate() {
        
    }
    
    
    
    func centerOnNode(node: SKNode) {
        
        let cameraPositionInScene: CGPoint = node.scene!.convertPoint(node.position, fromNode: World)
        
        node.parent!.runAction(SKAction.moveTo(CGPoint(x:node.parent!.position.x + cameraPositionInScene.x, y:node.parent!.position.y + cameraPositionInScene.y), duration: 1))
        
    }
    
    // MARK: AnalogStickProtocol
    func moveAnalogStick(analogStick: AnalogStick, velocity: CGPoint, angularVelocity: Float) {
        if let aN = oval {
            if analogStick.isEqual(moveAnalogStick) {
                aN.position = CGPointMake(aN.position.x + (velocity.x * 0.12), aN.position.y + (velocity.y * 0.12))
            } else if analogStick.isEqual(rotateAnalogStick) {
                aN.zRotation = CGFloat(angularVelocity)
            }
        }
    }
}

b0b487c28ec24fbb96d1af4e358f46fb.png2399f27cfdae42d884311400418e4828.png

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question