我現在有下面的代碼。即使代碼構建成功,我似乎無法使其工作。我試圖做到這一點,當你輕彈物體時,它會以開始和結束觸摸的速度移動。用SpriteKit拋出一個對象
import SpriteKit
class GameScene: SKScene {
var sprite: SKSpriteNode!
var touchPoint: CGPoint = CGPoint()
var touching: Bool = false
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)
self.addChild(sprite)
}
//for touch: AnyObject in touches {
//let location = touch.locationInNode(self)
//let touchedNode = self.nodeAtPoint(location)
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if sprite.frame.contains(location) {
touchPoint = location
touching = true
}
}
func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
touchPoint = location
}
func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
touching = false
}
func update(currentTime: CFTimeInterval) {
if touching {
let dt:CGFloat = 1.0/60.0
let distance = CGVector(dx: touchPoint.x-sprite.position.x, dy: touchPoint.y-sprite.position.y)
let velocity = CGVector(dx: distance.dx/dt, dy: distance.dy/dt)
sprite.physicsBody!.velocity = velocity
}
}
}}}
不是100%肯定這將幫助,但我想你想你做輕彈後只適用的速度。因爲你正在應用每一個更新,你正在運用一個非常小的速度。您可能想要嘗試更改touchesMoved中精靈的位置,然後根據距離開始點和結束點的距離結束觸摸速度。您也可以將其基於輕彈的開始時間和輕彈的結束時間。也可以幫助,如果你解釋你得到的結果,而不是它建立:) –
看到我的答案在這裏! http://stackoverflow.com/a/28259980/2158465 –
可能重複[如何拋出SKSpriteNode?](http://stackoverflow.com/questions/28245653/how-to-throw-skspritenode) –