SKNodes之间的计算距离虽然在移动

时间:2017-09-09 00:00:01

标签: ios swift sprite-kit geometry

我试图找到两个节点之间的距离,特别是rocks。现在这是简单的数学;但我遇到了一个问题。我想查看topRock是否在btmRock之内的距离,距离是否确定。我的代码是 - >

extension CGPoint {
    func distanceFromCGPoint(point:CGPoint)->CGFloat{
        return sqrt(pow(self.x - point.x,2) + pow(self.y - point.y,2))
    }
}

现在在我的更新功能中,我有 - >

var updateTopTime : Double = 0
var updateBottomTime : Double = 0
var genInterval : Double = 2
var genOffset : Double = 3.5

override func update(_ currentTime: TimeInterval) {
    moveBackgroundImg()

    //optional prevents generation if game is not playing
    //guard gameState == .playing else { return }

    if updateTopTime == 0 {
        updateTopTime = currentTime
    }

    if updateBottomTime == 0 {
        updateBottomTime = currentTime
    }

    if currentTime - updateBottomTime > genOffset {
        createBtmRock()
        genOffset = genInterval
        updateBottomTime = currentTime
    }
    else if currentTime - updateTopTime > genInterval {
        createTopRock()
        updateTopTime = currentTime
    }

    var distance = btmRock.position.distanceFromCGPoint(point: topRock.position)
    print(distance)

    if distance <= 10 {
        btmRock.position.x += 10
    }

    if holdingTouch{
        progressBar.progress -= 0.001
        voloc += 15
        plane.physicsBody?.velocity = CGVector(dx: 0, dy:  voloc)
        //plane.physicsBody?.applyImpulse(CGVector(dx: 0, dy: ))
    }
}

问题是distance会返回244.201193259997。我的topRockbtmRock随机生成并移动。即使节点移动,结果也保持不变。这就是我移动节点的方式 - &gt;

func moveBackgroundImg(){
    self.enumerateChildNodes(withName: "BackgroundImg", using: ({
        (node, error) in

        node.position.x -= self.backgroundMovingSpeed

        if node.position.x < -((self.scene?.size.width)!){
            node.position.x += (self.scene?.size.width)! * 3
        }

    }))

    self.enumerateChildNodes(withName: "GroundImg", using: ({
        (node, error) in

        node.position.x -= self.backgroundMovingSpeed

        if node.position.x < -((self.scene?.size.width)!){
            node.position.x += (self.scene?.size.width)! * 3
        }

    }))

    self.enumerateChildNodes(withName: "TopRock", using: ({
        (node, error) in

        node.position.x -= self.backgroundMovingSpeed

        if node.position.x < -((self.scene?.size.width)!){
            node.removeFromParent()
        }

    }))

    self.enumerateChildNodes(withName: "BtmRock", using: ({
        (node, error) in

        node.position.x -= self.backgroundMovingSpeed

        if node.position.x < -((self.scene?.size.width)!){
            node.removeFromParent()
        }

    }))
}

我对如何随机创建节点感到困惑,因此它们有不同的xy点;但我仍然得到244.201193259997。如果它有助于我的节点创建方式 - &gt;

func setupRocks() {

    //create the base bottom rock
    var btmRockChoice = [SKTexture(image: #imageLiteral(resourceName: "rock")), SKTexture(image: #imageLiteral(resourceName: "rockGrass")), SKTexture(image: #imageLiteral(resourceName: "rockSnow")), SKTexture(image: #imageLiteral(resourceName: "rockIce"))]

    btmRock = SKSpriteNode(texture: btmRockChoice[mapChoice], size: CGSize(width: (self.scene?.size.width)! / 10,  height: (self.scene?.size.height)! / 2.2))
    btmRock.zPosition = -9
    btmRock.position = CGPoint(x: self.frame.width, y: frame.minY + btmRock.frame.height / 2)
    btmRock.name = "BtmRock"

    btmRock.physicsBody = SKPhysicsBody(texture: btmRockChoice[mapChoice], size: CGSize(width: (self.scene?.size.width)! / 10,  height: (self.scene?.size.height)! / 2.2))
    btmRock.physicsBody?.categoryBitMask = physicsCatagory.topRock
    btmRock.physicsBody?.collisionBitMask = physicsCatagory.plane
    btmRock.physicsBody?.contactTestBitMask = physicsCatagory.plane
    btmRock.physicsBody?.affectedByGravity = false
    btmRock.physicsBody?.isDynamic = false

    //create the base top rock
    var topRockChoice = [SKTexture(image: #imageLiteral(resourceName: "rockDown")), SKTexture(image: #imageLiteral(resourceName: "rockGrassDown")), SKTexture(image: #imageLiteral(resourceName: "rockSnowDown")), SKTexture(image: #imageLiteral(resourceName: "rockIceDown"))]

    topRock = SKSpriteNode(texture: topRockChoice[mapChoice], size: CGSize(width: (self.scene?.size.width)! / 10,  height: (self.scene?.size.height)! / 2.2))
    topRock.zPosition = -9
    topRock.name = "TopRock"
    topRock.position = CGPoint(x: self.frame.width + topRock.size.width * 2, y: frame.maxY - topRock.frame.height / 2)

    topRock.physicsBody = SKPhysicsBody(texture: topRockChoice[mapChoice], size: CGSize(width: (self.scene?.size.width)! / 10,  height: (self.scene?.size.height)! / 2.2))
    topRock.physicsBody?.categoryBitMask = physicsCatagory.topRock
    topRock.physicsBody?.collisionBitMask = physicsCatagory.plane
    topRock.physicsBody?.contactTestBitMask = physicsCatagory.plane
    topRock.physicsBody?.affectedByGravity = false
    topRock.physicsBody?.isDynamic = false
}

func createTopRock() {

    //You can make this number a class variable to increase the rate as the game progresses
    let randomNum = arc4random_uniform(2)

    //there is a 1 in 3 chance that this rock will get created
    if randomNum == 0 {

        let rock = topRock.copy() as! SKSpriteNode
        self.addChild(rock)
    }
}

func createBtmRock() {

    //You can make this number a class variable to increase the rate as the game progresses
    let randomNum = arc4random_uniform(2)

    //there is a 1 in 2 chance that this rock will get created
    if randomNum == 0 {

        let rock = btmRock.copy() as! SKSpriteNode
        self.addChild(rock)
    }
}

override func didMove(to view: SKView) {
    spawnDelayForeverTop = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.createTopRock), userInfo: nil, repeats: false)

    spawnDelayForeverBtm = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(self.createBtmRock), userInfo: nil, repeats: false)
}

更新:我认为这与锚点有关,但我不确定。

1 个答案:

答案 0 :(得分:1)

topRock和bottomRock节点实际上不是您在场景中看到的节点,也不会作为子节点添加。您要添加到场景中的是这两个“模板”对象的副本。 topRock和bottomRock的位置永远不会改变,因为它们不是场景子节点的一部分(我在代码中看不到你改变这两个实例位置的任何地方)