如何在精灵套件中更改精灵的触摸偏移量?



当我触摸球员木槌(空气曲棍球(时,我想让它使木槌稍微移动到触摸上方。这样木槌将在游戏中更加明显。我已经找到了一些解决方案,但很难在我的函数中正确实现。

这是我的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
}
}

您从触摸位置获取的positionvar 是否用于设置木槌的位置?如果是,那么如果你想让木槌在触摸上方,为什么不在position = location后立即做一些类似position.y += 50的事情,将其向上移动 50 点?

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

最新更新