2015-11-19 58 views
-1

playing game from anywhere需要幫助使其讓球只能從遊戲

喜的上半部分被丟棄,所以我希望用戶只能夠從釘砸球的頂部行上方點擊通過釘。現在,你可以簡單地觸摸屏幕上的任何地方,包括目標,球將會去那裏。我曾嘗試使用if語句和CGPoint來讓它只能從pegs上方刪除,但我太初學者瞭解它。謝謝。

這裏是我的代碼,用斯威夫特2和Xcode的7:我的身影,它會何去何從......

for touch in touches { 

     if self.nunus.count >= numberOfNunus { 
      self.removeChildrenInArray(self.nunus) 
      self.nunus = [] 
      self.score = 0 
      updateLabels() 
     } else { 

      let nunu = SKSpriteNode(imageNamed:"nunu") 

      nunu.xScale = 0.13 
      nunu.yScale = 0.13 
      nunu.position = touch.locationInNode(self) 

      nunu.physicsBody = SKPhysicsBody(circleOfRadius: nunu.size.height/2) 
      nunu.physicsBody?.categoryBitMask = self.nunuCategory 
      nunu.physicsBody?.collisionBitMask = self.pegCategory | self.borderCategory | self.postCategory | self.nunuCategory 
      nunu.physicsBody?.contactTestBitMask = smallGoalCategory | self.mediumGoalCategory | self.bigGoalCategory 



      self.addChild(nunu) 

      self.nunus.append(nunu) 
      updateLabels() 
     } 
    } 
} 
+2

歡迎來到SO。發佈幾百行代碼並希望讀者排除所有問題以找出問題是一種糟糕的形式。請編輯您的問題,向我們指出相關的代碼並解釋您當前的邏輯。 –

+0

「誰能幫忙?」不是一個問題。 – matt

+0

爲了澄清,這可能是一個語法問題,但它不符合編程問題的堆棧溢出定義。 –

回答

0

此代碼創建和如果只有你比現場的midle觸及高路節點。如果whant改變高度改變這一行:

if location.y > worl.frame.size.height/2 

例如:

if location.y > worl.frame.size.height * 0.8 

意味着你的觸摸位置必須高於80%的場景高度

touchesBegan

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 

    for touch in touches { 
     let location = touch.locationInNode(self) 
     //let touchedNode = nodeAtPoint(location) 

     if location.y > worl.frame.size.height/2 { 

      createNode() 
     } 
    } 
} 

func createAndDropNode() { 

    //code here 
} 

world - SKNo與您的SKScene尺寸完全相同

+0

謝謝你的幫助。你讓我走向正確的方向!我加了這個:if touch.locationInNode(self).y> 500 – ciccioboles

+0

你不應該使用那一行,我推薦做這樣的事情:if touch.locationInNode(self).y> world.frame.size.height/18 * 16 – Darvydas

0

找出來了。 iPhone屏幕上的關鍵點在500或以上。所以如果用戶觸及500點以下,丟球的代碼將不會運行。我加入這行代碼:如果touch.locationInNode(個體).Y> 500

FUNC的touchesBegan(觸摸:設置,withEvent事件:?的UIEvent){ /*調用當觸摸開始*/

for touch in touches { 

     if touch.locationInNode(self).y > 500 { 


     if self.nunus.count >= numberOfNunus { 
      self.removeChildrenInArray(self.nunus) 
      self.nunus = [] 
      self.score = 0 
      updateLabels() 
     } else { 

      let nunu = SKSpriteNode(imageNamed:"nunu") 

      nunu.xScale = 0.13 
      nunu.yScale = 0.13 
      nunu.position = touch.locationInNode(self) 

      nunu.physicsBody = SKPhysicsBody(circleOfRadius: nunu.size.height/2) 
      nunu.physicsBody?.categoryBitMask = self.nunuCategory 
      nunu.physicsBody?.collisionBitMask = self.pegCategory | self.borderCategory | self.postCategory | self.nunuCategory 
      nunu.physicsBody?.contactTestBitMask = smallGoalCategory | self.mediumGoalCategory | self.bigGoalCategory 




      self.addChild(nunu) 

      self.nunus.append(nunu) 
      updateLabels() 
     } 
     } 
    } 
} 
+0

iPhone 5,5s有568,但其他手機有更多。 http://www.paintcodeapp.com/news/ultimate-guide-to-iphone-resolutions – Darvydas

+0

那麼你切換到另一個設備它不會是corect – Darvydas