2014-09-20 64 views
0

用力我有一些像這樣:如何在碰撞中的iOS迅速使用精靈套件

func didBeginContact(contact: SKPhysicsContact) { 
    var paddle = getPaddleFromContact(contact) //of type SKPhysicsBody 
    var ball = getBallFromContact(contact) //of type SKPhysicsBody 
    if(paddle != nil && ball != nil){ 
    //apply force to ball depending on the speed/velocity of the paddle 
    //NEED HELP WITH THIS LOGIC 
    } 
} 

目前的用戶能夠使用觸摸拖動槳。它只能移動水平。我不確定哪個屬性或如何根據槳的速度確定施加多大的力。有關如何正確執行此操作的任何建議?

我嘗試使用paddle.velocity但這些都只是0的

目前正在對類似的東西我自己

回答

0

上午。我有我自己的問題,但我有它的工作。我這樣做的方法是使用觸摸功能:

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { 

    let touch = touches.anyObject() as UITouch!; 
    self.firstLocation = touch.locationInNode(self); 

} 

override func touchesMoved(touches: NSSet, withEvent event: UIEvent) { 

    if (nil == self.firstLocation) { 
     // this is reset to nil in your collision function 
     return; 
    } 

    let touch = touches.anyObject() as UITouch!; 
    let location = touch.locationInNode(self); 

    // use this vector in your collision function 
    self.vector = CGVectorMake(self.firstLocation.x - location.x, self.firstLocation.y - location.y); 

} 

如果你的碰撞FUNC使用類似:

ball.applyImpulse(self.vector); 

免責聲明:沒試過編譯上面那麼可能需要的調整爲了展開可選項等,顯然,YMMV。