我目前声明了以下函数,这些函数将节点移动到场景更新函数中调用的目标位置(在这种情况下,我不能使用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)
}
}
(这些是根据苹果的“冒险”游戏稍微修改的)
我的问题是 moveCurrentPosition 函数中的这些行。
let x = currentPosition.x * maximumDistance
let y = currentPosition.y * maximumDistance
position = CGPointMake(x, y)
基本上,我不知道将 x 和 y 设置为什么值,以便该位置代表正确的位置。 在苹果的示例中,他们将其乘以最大距离 - 角度。角度 var 是一个仅影响 Assets 旋转的值,但我的 Assets 是“静态”的并且没有多个位置 - 只有 1。
考虑到这一点,我将x和y设置为多少,以便它们代表正确的位置?
(您可以在此处查看 Apple 的示例: https://developer.apple.com/library/prerelease/ios/samplecode/Adventure-Swift/Listings/Swift_Adventure_Adventure_Shared_Sprites_Character_swift.html )
最佳答案
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)
在这种情况下,您不使用 SKActions
是正确的。我看到您正在尝试在不使用 SKActions
的情况下将节点
移动到某个位置。我不太明白您发布的代码所遇到的问题。不过,我编写了一个小型示例项目,演示了在没有 SKActions
的情况下将节点移动到更新函数中的位置。在此示例项目中,我有一个移动到当前触摸位置的 Sprite 。您可以设置节点行进的速度以及应用速度的速率。您可以尝试调整速率以获得正确的游戏感觉。您还需要处理节点到达目标位置时的情况(同样,这取决于您的游戏)。
如果这有帮助和/或您有任何疑问,请告诉我。
import SpriteKit
class GameScene: SKScene {
var sprite: SKSpriteNode!
var travelPoint: CGPoint = CGPoint() //The point to travel to.
let travelSpeed = CGVector(dx: 200, dy: 200) //The speed at which to travel.
let rate:CGFloat = 0.1 //1.0 Completely responsive. 0.0 Completely unresponsive. I set this value to < 1 to smooth the application of motion.
override func didMoveToView(view: SKView) {
self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
sprite = SKSpriteNode(color: UIColor.redColor(), size: CGSize(width: 50, height: 50))
sprite.physicsBody = SKPhysicsBody(rectangleOfSize: sprite.size)
sprite.position = CGPoint(x: self.size.width/2.0, y: self.size.height/2.0)
sprite.physicsBody?.affectedByGravity = false
self.addChild(sprite)
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
let touch = touches.first as! UITouch
let location = touch.locationInNode(self)
travelPoint = location
}
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
let touch = touches.first as! UITouch
let location = touch.locationInNode(self)
travelPoint = location
}
override func update(currentTime: CFTimeInterval) {
let disp = CGVector(dx: travelPoint.x-sprite.position.x, dy: travelPoint.y-sprite.position.y)
let angle = atan2(disp.dy, disp.dx)
let vel = CGVector(dx: cos(angle)*travelSpeed.dx, dy: sin(angle)*travelSpeed.dy)
let relVel = CGVector(dx: vel.dx-sprite.physicsBody!.velocity.dx, dy: vel.dy-sprite.physicsBody!.velocity.dy)
sprite.physicsBody!.velocity = CGVector(dx: sprite.physicsBody!.velocity.dx+relVel.dx*rate, dy: sprite.physicsBody!.velocity.dy+relVel.dy*rate)
}
}
关于ios - 在一段时间内手动移动节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30362586/