拖动和flicking/throwing skspritenode
Dragging and flicking/throwing skspritenode
我有一个小球 sprite,我希望它能够弹向其他 sprite。现在,我有下面的 TouchesMoved 函数,但是当我希望我的用户首先触摸精灵并将他们的手指拖向目标时,它会将我的精灵移动到屏幕上的任何触摸动作,从而导致精灵移动。这是功能失调的功能。任何帮助将不胜感激!
编辑:我希望无论弹射长度如何,球速度都保持不变。其他答案未解决。
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
if firstTimerStarted == false {
let touch = touches.first as! UITouch
let touchLocation = touch.locationInNode(self)
sceneTouched(touchLocation)
}
这应该可以,我只是从一个旧项目中挖出来的。
CGFloat dt 用于改变运动的speed/power。
var touching = false
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
let touch = touches.first as! UITouch
let location = touch.locationInNode(self)
if sprite.frame.contains(location) {
touchPoint = location
touching = true
}
}
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
let touch = touches.first as! UITouch
let location = touch.locationInNode(self)
touchPoint = location
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
touching = false
}
override func update(currentTime: CFTimeInterval) {
if touching {
if touchPoint != sprite.position
{
let dt:CGFloat = 0.15
let distance = CGVector(dx: touchPoint.x-sprite.position.x, dy: touchPoint.y-sprite.position.y)
let vel = CGVector(dx: distance.dx/dt, dy: distance.dy/dt)
sprite.physicsBody!.velocity = vel
}
}
}
编辑:
距离越远它变得越强的原因是因为矢量是精灵和触摸点之间的距离。
尝试将其作为更新功能弹出。它应该工作...
override func update(currentTime: CFTimeInterval) {
if touching {
if touchPoint != sprite.position
{
let pointA = touchPoint
let pointB = sprite.position
let pointC = CGPointMake(sprite.position.x + 2, sprite.position.y)
let angle_ab = atan2(pointA.y - pointB.y, pointA.x - pointB.x)
let angle_cb = atan2(pointC.y - pointB.y, pointC.x - pointB.x)
let angle_abc = angle_ab - angle_cb
let vectorx = cos(angle_abc)
let vectory = sin(angle_abc)
let dt:CGFloat = 15
let vel = CGVector(dx: vectorx * dt, dy: vectory * dt)
sprite.physicsBody!.velocity = vel
}
}
}
通过触摸点 (pointA) 和精灵的位置 (pointB),我们可以创建一个角度。
atan2,是一个非常有名的 C 函数,它创建两点之间的角度。
但是,它的 0 度位置与平时不同。
所以,我们需要自己的 0 度标记,我使用点的右中作为我的标记。
这是常见的 0 度放置:
因为它在精灵位置的右侧,我们在精灵右侧创建一个点 (pointC)。
我们现在使用 atan2 来求角度。
要从某个角度创建矢量,我们只需使用 cos 和 sin 作为 x 和 y 值。
我有一个小球 sprite,我希望它能够弹向其他 sprite。现在,我有下面的 TouchesMoved 函数,但是当我希望我的用户首先触摸精灵并将他们的手指拖向目标时,它会将我的精灵移动到屏幕上的任何触摸动作,从而导致精灵移动。这是功能失调的功能。任何帮助将不胜感激!
编辑:我希望无论弹射长度如何,球速度都保持不变。其他答案未解决。
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
if firstTimerStarted == false {
let touch = touches.first as! UITouch
let touchLocation = touch.locationInNode(self)
sceneTouched(touchLocation)
}
这应该可以,我只是从一个旧项目中挖出来的。
CGFloat dt 用于改变运动的speed/power。
var touching = false
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
let touch = touches.first as! UITouch
let location = touch.locationInNode(self)
if sprite.frame.contains(location) {
touchPoint = location
touching = true
}
}
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
let touch = touches.first as! UITouch
let location = touch.locationInNode(self)
touchPoint = location
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
touching = false
}
override func update(currentTime: CFTimeInterval) {
if touching {
if touchPoint != sprite.position
{
let dt:CGFloat = 0.15
let distance = CGVector(dx: touchPoint.x-sprite.position.x, dy: touchPoint.y-sprite.position.y)
let vel = CGVector(dx: distance.dx/dt, dy: distance.dy/dt)
sprite.physicsBody!.velocity = vel
}
}
}
编辑: 距离越远它变得越强的原因是因为矢量是精灵和触摸点之间的距离。 尝试将其作为更新功能弹出。它应该工作...
override func update(currentTime: CFTimeInterval) {
if touching {
if touchPoint != sprite.position
{
let pointA = touchPoint
let pointB = sprite.position
let pointC = CGPointMake(sprite.position.x + 2, sprite.position.y)
let angle_ab = atan2(pointA.y - pointB.y, pointA.x - pointB.x)
let angle_cb = atan2(pointC.y - pointB.y, pointC.x - pointB.x)
let angle_abc = angle_ab - angle_cb
let vectorx = cos(angle_abc)
let vectory = sin(angle_abc)
let dt:CGFloat = 15
let vel = CGVector(dx: vectorx * dt, dy: vectory * dt)
sprite.physicsBody!.velocity = vel
}
}
}
通过触摸点 (pointA) 和精灵的位置 (pointB),我们可以创建一个角度。
atan2,是一个非常有名的 C 函数,它创建两点之间的角度。 但是,它的 0 度位置与平时不同。
所以,我们需要自己的 0 度标记,我使用点的右中作为我的标记。 这是常见的 0 度放置:
因为它在精灵位置的右侧,我们在精灵右侧创建一个点 (pointC)。
我们现在使用 atan2 来求角度。
要从某个角度创建矢量,我们只需使用 cos 和 sin 作为 x 和 y 值。