2016-06-10 36 views
1

我在做籃球比賽。我的問題是當我投籃時,我可以再次在半空中觸球,而我不想這樣做。一旦用戶投球,我不希望觸摸產生效果。這裏是我的代碼:如何更改我的程序以允許此對象僅對單點觸摸作出反應?

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent? 
{ 
    touching = false 
    for touch: AnyObject in touches { 
    let location = touch.locationInNode(self) 
    if ball.frame.contains(location){ 
     touchingPoint = location 
     touching = true 
    } 
    } 
} 


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


override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    if touching{ 
     let dt:CGFloat = 8.25/29.5 
     let distance = CGVector(dx: touchingPoint.x-ball.position.x, dy: touchingPoint.y-ball.position.y) 
     let velocity = CGVector(dx: distance.dx/dt, dy: distance.dy/dt) 
     ball.physicsBody!.velocity=velocity 
    } 
} 

我可以添加什麼我的程序使得觸摸不會有任何影響一旦球已經出手?

回答

1

你拍皮球后禁用用戶交互

ball.userInteractionEnabled = false 

然後再次啓用它,當你希望他們能夠觸摸到它

+0

地獄是@LukePatterson –

+0

@DeanSponholz :) –

+0

你以某種方式連接到我的個人資料有問題?或者你是否隨機遇到這個問題。只是好奇,因爲這實際上是我見過的最快的正確答案,因爲我知道你幫助我解決了以前的問題。 @LukePatterson –

相關問題