I currently have the following functions declared which moves a node to a target location which is called in the update function of the scene (In this situation, I can't use SKAction.moveTo)
func moveTowardsPosition(targetPosition: CGPoint, withTimeInterval timeInterval: NSTimeInterval) {
let currentPosition = self.position
var deltaX = targetPosition.x - currentPosition.x
var deltaY = targetPosition.y - currentPosition.y
var maximumDistance = 3 * CGFloat(timeInterval)
moveFromCurrentPosition(currentPosition, byDeltaX: deltaX, deltaY: deltaY, maximumDistance: maximumDistance)
}
func moveFromCurrentPosition(currentPosition: CGPoint, byDeltaX dx: CGFloat, deltaY dy: CGFloat, maximumDistance: CGFloat) {
let targetPosition = CGPointMake(currentPosition.x + dx, currentPosition.y + dy)
var distRemaining = hypot(dx, dy)
if distRemaining < maximumDistance {
position = targetPosition
} else {
let x = currentPosition.x * maximumDistance
let y = currentPosition.y * maximumDistance
position = CGPointMake(x, y)
}
}
(These are slightly modified from Apples "Adventure" game)
My problem is these lines in the moveCurrentPosition function.
let x = currentPosition.x * maximumDistance
let y = currentPosition.y * maximumDistance
position = CGPointMake(x, y)
Basically, I have no idea what values to set x and y to so that position represents the correct position.
In Apple's example, they are multiplying it by maximumDistance - angle. The angle var is a value that just affects the assets rotation, however my asset is 'static' and doesn't have multiple positions -- just 1.
With that in mind, what would I set x and y to so they are representative of the correct position?
(You can see Apple's example here: https://developer.apple.com/library/prerelease/ios/samplecode/Adventure-Swift/Listings/Swift_Adventure_Adventure_Shared_Sprites_Character_swift.html)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…