在展开可选值时发现nil(firebase)

时间:2018-05-06 16:51:03

标签: ios firebase-realtime-database sprite-kit swift4.1

我正在使用firebase框架将sprite的X坐标实时上传到服务器,而另一个sprite将使用从服务器获取的坐标镜像其移动但是会出错

  

在解包可选值时发现nil

这是我的代码请帮助:

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) {

 }
}

0 个答案:

没有答案