2017-09-09 3 views
1

2つのノード間の距離、特にrocksを検索しようとしています。これは簡単な数学です。私は問題に遭遇しています。私は、topRockが距離内にあるかどうか、その距離が決定されることを確認するために、btmRockをチェックしたいと思います。私が持っているコードがある - >SKNodes間の計算された距離は、移動しても変更されません。

私が持っている私の更新機能で今すぐ
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はランダムに生成されるだけでなく、移動します。ノードが動いている場合でも、結果は同じままです。これは私が私のノードを移動する方法です - >

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を得ています。それが助け場合、これは私のノードが作成されている方法です - >

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

UPDATE:私はそれがアンカーポイントとは何かを持っていると考えているが、私はわかりませんよ。

+0

なぜあなたの距離は変わりますか?あなたのトップロックとボトムロックは同じ速度で左に移動します – Knight0fDragon

+0

あなたのすべての岩は同じ位置に作成され、ランダムxの位置はなく、arc4random_uniform(2)は33/33/33ではなく50/50チャンスです0または1を取得してください) – Knight0fDragon

+0

@ Knight0fDragon私が設定したタイマーには、乱数が入っています。だから彼らは同時に作成されていません。それらは、それらの間に異なる間隔で異なる時間に作成されます。彼らはまた、彼らが画面を離れて再び作成されると、親から削除され、再作成されたタイマーにも従います。 –

答えて

1

topRockノードとbottomRockノードは実際にあなたのシーンに表示されているノードではなく、子として追加されません。シーンに追加しているのは、これら2つの「テンプレート」オブジェクトのコピーです。 topRockとbottomRockの位置はシーンの子要素の一部ではないため変更されません(そして、これらの2つのインスタンスの位置を変更する場所にコードが表示されません)

関連する問題