2014-09-03 61 views
2

我在Xcode 6 Beta 6中使用Swift在SpriteKit中編寫代碼。在代碼中,當用戶移動它時,我需要一張圖片來跟隨手指。 touchesMoved的作品,但與故障。如果我慢慢地移動手指,一切都很好。如果我移動手指快速地從右向左移動,那麼一切都很好。如果我快速地從左向右移動手指,那麼圖片只會在手指上快速移動幾分之一秒。如果我在現在位置點擊並保持圖片大約半秒鐘,那麼當我從右向左或從左向右快速移動圖片時,一切都很順利。總之,我不能將圖片從左向右快速移動,除非我點擊並保持約半秒的圖片。任何人都知道爲什麼會發生這種情況?謝謝你的時間。以下是代碼。我正在移動SKSPriteNode follow2TouchesMoved在迅速滯後

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

    for touch: AnyObject in touches { 

     let angle_area_location = touch.locationInNode(self) 

     if self.nodeAtPoint(angle_area_location) == self.angle_area { 

      if (angle_area_location.x <= 21) { 
       angle = 1.55681122463001 
       distance12 = sqrt((angle_area_location.y - 30) * (angle_area_location.y - 30) + 1) 

      } 

      if (angle_area_location.y <= 31) { 
       angle = 0.0102037274939542 
       distance12 = sqrt((31 - 30) * (31 - 30) + (angle_area_location.x - 20) * (angle_area_location.x - 20)) 

      } 

      if (angle_area_location.x > 21) && (angle_area_location.y > 31) { 

       angle = atan((angle_area_location.y - 30)/(angle_area_location.x - 20)) 
       distance12 = sqrt((angle_area_location.y - 30) * (angle_area_location.y - 30) + (angle_area_location.x - 20) * (angle_area_location.x - 20)) 

      } 

      if (distance12 <= maxFollow2) && (distance12 >= minFollow2) { 

       self.cannon.zRotation = angle 
       self.arc.zRotation = angle 

       if (angle_area_location.x > 21) || (angle_area_location.y > 31) { 
       follow2.position = CGPointMake(angle_area_location.x , angle_area_location.y) 
       } 

       if(angle_area_location.x <= 21) { 

        follow2.position = CGPointMake(21 , angle_area_location.y) 

       } 

       if (angle_area_location.y <= 31) { 

        follow2.position = CGPointMake(angle_area_location.x , 31) 

       } 



      } 
      if(distance12 > maxFollow2) { 

       self.cannon.zRotation = angle 
       self.arc.zRotation = angle 
       delta = 290/3 
       arc.size = CGSizeMake(160 * (1 + delta/20) , 35) 
       arc.position = CGPointMake(20 - 3 * (delta) * cos(angle) , 30 - 3 * (delta) * sin(angle)) 
       followdist = 360 
       follow2.position = CGPointMake(angle_area_location.x , angle_area_location.y) 
       velocity = vmin + (followdist - minFollow2) * (300/(maxFollow2 - minFollow2)) 

      } 

      if (distance12 < minFollow2) { 

       self.cannon.zRotation = angle 
       self.arc.zRotation = angle 
       arc.size = CGSizeMake(160 , 6.8) 
       arc.position = CGPointMake(20 , 30) 
       follow2.position = CGPointMake(minFollow2 * cos(angle) + 20 , minFollow2 * sin(angle) + 30) 
       followdist = sqrt((follow2.position.y - 30) * (follow2.position.y - 30) + (follow2.position.x - 20) * (follow2.position.x - 20)) 
       velocity = vmin + (followdist - minFollow2) * (300/(maxFollow2 - minFollow2)) 


      } 

     } 

    } 


} 

回答

0

使用距離時常見的技巧是避免採用平方根並僅比較平方值。這節省了相當多的處理器資源。

例子:

let maxfollow2sqr = maxFollow2 * maxFollow2 

distance12 = (angle_area_location.y - 30) * (angle_area_location.y - 30) + (angle_area_location.x - 20) * (angle_area_location.x - 20) 
if (distance12 <= maxFollow2sqr) { 
// do something here 
} 

因爲所有你關心的是,如果計算距離分鐘,就可以對付剛剛與正方形max之間。這可能會加快函數的速度,但可能還有其他優化可以完成。

1

好吧,我想出了故障。我有一個UISwipeGestureRecognizer,當我右擊時調用一個方法。我停用了,一切正常。我認爲在觸摸移動時右移並左右移動會干擾對方。