代码之家  ›  专栏  ›  技术社区  ›  Roman

SpriteKit如何检测节点是否移动

  •  0
  • Roman  · 技术社区  · 7 年前

    对于我的游戏,我需要检测节点是否仍在移动。现在我试着在一段时间内比较新旧位置。如果位置具有相同的值,则节点不会移动。反之亦然。像这样的:

     if aStonesPositionsOld[index] == aStonesPositionsNew[index] {
                            print("# stone \(index) is not moving")
                        }
    

    有人知道更好更容易的方法来检查节点是否移动了吗?

    更新。KnighOfDragon问题的答案(节点如何开始移动?) 如果被触摸,节点就会开始移动,或者更准确地说,用手指移动。这里有一个代码:

          if touching {
    
                let dt:CGFloat = 1.0/60.0
                let distance = CGVector(dx: touchPoint.x-aCuesMe[touchingNr].position.x, dy: touchPoint.y-aCuesMe[touchingNr].position.y)
    
                velocity = CGVector(dx: distance.dx/dt, dy: distance.dy/dt)
                aCuesMe[touchingNr].physicsBody!.velocity=velocity
            }
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Tob    7 年前

    既然你用物理引擎移动物体,你所要做的就是检查速度。

    if let v = aCuesMe[someIndex].physicsBody?.velocity {
        if v == CGVector(dx: 0, dy: 0) {
            // Object has 0 velocity and is not moving
        }
    }
    
        2
  •  0
  •   Max Gribov    7 年前

    你可以用 GKAgent2D 为此目的 GameplayKit . 它有财产 velocity . 有关更多信息,请阅读苹果的文档: https://developer.apple.com/library/archive/documentation/General/Conceptual/GameplayKit_Guide/Agent.html#//apple_ref/doc/uid/TP40015172-CH8

    观看视频: https://developer.apple.com/videos/play/wwdc2016/608/