0

I am using firebase frameworks to upload the X coordinates of a sprite to the server in real time and the other sprite will mirror its moves using the coordinates that get from the server but gives the error

found nil while unwrapping an optional value

and here is my code pls help :

import SpriteKit
import GameplayKit
import FirebaseDatabase

class GameScene: SKScene {

var loc: DatabaseReference?
var getLoc: DatabaseHandle?
var x : CGFloat?
var y : CGFloat?
var handle = SKSpriteNode()
var opponentHandle = SKSpriteNode()
override func sceneDidLoad() {
    handle = childNode(withName: "handle") as! SKSpriteNode
    opponentHandle = childNode(withName: "opponentHandle") as! SKSpriteNode
    loc = Database.database().reference()
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    for t in touches{
        var fingerloc = t.location(in: self)
        handle.run(SKAction.move(to: fingerloc, duration: 0.000001))
        loc?.child("ali").child("position").child("locX").child("locx").setValue(CGFloat(handle.position.x))
        loc?.child("ali").child("position").child("locY").childByAutoId().setValue(String(describing: handle.position.y))
        getLoc = loc?.child("ali").child("position").child("locX").observe(.childAdded, with: { (snapshot) in

            self.x = snapshot.value as? CGFloat
            print(self.x!)
        })
        /*getLoc = loc?.child("ali").child("position").child("locY").observe(.childAdded, with: { (snapshot) in

            self.y = snapshot.value as? CGFloat
        })*/
        //opponentHandle.run(SKAction.moveTo(y: CGFloat((y!)), duration: 0.000001))*/
    }
    opponentHandle.run(SKAction.moveTo(x: x!, duration: 0.000001))
}
 override func update(_ currentTime: TimeInterval) {

 }
}
Ron Myschuk
  • 5,817
  • 2
  • 17
  • 31

0 Answers0