如何更改 SpriteKit 中精灵的触摸偏移量?

How can I change the touch offset of a sprite in SpriteKit?

当我触摸播放器槌 (Air Hockey) 时,我想让槌稍微移动到触摸点上方。这样,木槌将在游戏中更加明显。我找到了一些解决方案,但很难在我的功能中正确实施。

这是我的 touchesMoved() 函数的示例:

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?)
{
    bottomTouchIsActive = true
    var releventTouch:UITouch!
    //convert set to known type
    let touchSet = touches

    //get array of touches so we can loop through them
    let orderedTouches = Array(touchSet)


    for touch in orderedTouches
    {
        //if we've not yet found a relevent touch
        if releventTouch == nil
        {
            //look for a touch that is in the activeArea (Avoid touches by opponent)
            if activeArea.contains(touch.location(in: parent!))
            {
                isUserInteractionEnabled = true
                releventTouch = touch
            }
            else
            {
                releventTouch = nil
            }
        }
    }

    if (releventTouch != nil)
    {
        //get touch position and relocate player
        let location = releventTouch!.location(in: parent!)
        position = location

        //find old location and use pythagoras to determine length between both points
        let oldLocation = releventTouch!.previousLocation(in: parent!)
        let xOffset = location.x - oldLocation.x
        let yOffset = location.y - oldLocation.y
        let vectorLength = sqrt(xOffset * xOffset + yOffset * yOffset)

        //get eleapsed and use to calculate speed6A
        if  lastTouchTimeStamp != nil
        {
            let seconds = releventTouch.timestamp - lastTouchTimeStamp!
            let velocity = 0.01 * Double(vectorLength) / seconds

            //to calculate the vector, the velcity needs to be converted to a CGFloat
            let velocityCGFloat = CGFloat(velocity)

            //calculate the impulse
            let directionVector = CGVector(dx: velocityCGFloat * xOffset / vectorLength, dy: velocityCGFloat * yOffset / vectorLength)

            //pass the vector to the scene (so it can apply an impulse to the puck)
            delegate?.bottomForce(directionVector, fromBottomPlayer: self)
            delegate?.bottomTouchIsActive(bottomTouchIsActive, fromBottomPlayer: self)
        }
        //update latest touch time for next calculation
        lastTouchTimeStamp = releventTouch.timestamp
    }
}

你从触摸位置获取的position变量是用来设置锤子位置的吗?如果是,那么如果你想要击球上方的木槌,为什么不在 position = location 之后立即执行类似 position.y += 50 的操作以将其向上移动 50 点?

或者,您可能会发现将木槌的 anchorPoint 属性 (https://developer.apple.com/documentation/spritekit/skspritenode/1519877-anchorpoint and https://developer.apple.com/documentation/spritekit/skspritenode/using_the_anchor_point_to_move_a_sprite) 设置为默认位置(精灵的中心)以外的其他位置更合乎逻辑,例如与通常握住木槌的木槌手柄部分相对应的点。