2015-05-22 204 views
1

我現在有下面的代碼。即使代碼構建成功,我似乎無法使其工作。我試圖做到這一點,當你輕彈物體時,它會以開始和結束觸摸的速度移動。用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 
     } 
    } 
}}} 
+0

不是100%肯定這將幫助,但我想你想你做輕彈後只適用的速度。因爲你正在應用每一個更新,你正在運用一個非常小的速度。您可能想要嘗試更改touchesMoved中精靈的位置,然後根據距離開始點和結束點的距離結束觸摸速度。您也可以將其基於輕彈的開始時間和輕彈的結束時間。也可以幫助,如果你解釋你得到的結果,而不是它建立:) –

+0

看到我的答案在這裏! http://stackoverflow.com/a/28259980/2158465 –

+0

可能重複[如何拋出SKSpriteNode?](http://stackoverflow.com/questions/28245653/how-to-throw-skspritenode) –

回答

2

你不小心放置touchesMovedtouchesEndedupdatetouchesBegan。除此之外,你的代碼工作。暗示有問題的是您不需要以touchesMoved,touchesEndedupdateoverride的前綴作爲前綴。在未來,我會建議使用斷點和打印語句來檢查你期望執行的方法,事實上它們正在運行。這樣做,你會看到你的版本touchesMoved,touchesEndedupdate沒有被調用。

總之,這裏的修正它它,現在它完美的作品:

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) 
    } 

    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 
      } 
     } 
    } 

    override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) { 
     for touch: AnyObject in touches { 
      let location = touch.locationInNode(self) 
      touchPoint = location 
     } 
    } 

    override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) { 
     touching = false 
    } 

    override func touchesCancelled(touches: Set<NSObject>!, withEvent event: UIEvent!) { 
     touching = false 
    } 

    override func update(currentTime: NSTimeInterval) { 
     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 
     } 
    } 
} 
+0

感謝堆!它現在的作品:)快速的問題,我將如何使它,所以精靈不會移動,直到你輕彈它?所以你不能拖動它,所以它有一個原始位置? – James

+0

我會建議看看[這](http://stackoverflow.com/questions/28552408/how-do-i-implement-velocityinview-for-a-custom-gesture-recognizer/29479092#29479092)的信息獲取手勢識別器的速度(您在'SKScene'中已經有了相同的方法)。然後你可以在'touchesEnded'中給出精靈速度。此外,如果答案解決了您的問題,您是否會認爲將其標記爲正確? :) – ABakerSmith

+0

感謝您的幫助:)我標記正確 – James

相關問題