簡體   English   中英

SpriteKit如何檢測節點是否在移動

[英]SpriteKit How to detect if node is moving or not

對於我的游戲,我需要檢測節點是否仍在移動。 現在,我嘗試在一個時間間隔內比較舊位置和新位置。 如果位置具有相同的值,則節點不會移動。 反之亦然。 像這樣:

 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
        }

由於使用物理引擎移動對象,因此您要做的就是檢查速度。

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

為此,您可以從GameplayKit使用GKAgent2D 它具有特性velocity 有關更多信息,請閱讀Apple的文檔: 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/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM