簡體   English   中英

在SKSpriteNodes上檢測觸摸事件

[英]Detecting touch events on SKSpriteNodes

所以這里有一些類似的問題,但是它們並不能真正回答我所遇到的問題。

以下對我的項目的解釋可能有幫助,也可能沒有幫助,為防萬一...

我正在嘗試構建我的第一個iOS游戲,並且已經使用場景編輯器構建了一個場景。 該場景包含一個名為“ World”的SKNode。 然后有一個world節點的子節點backgroundNode和前景節點Node。 現在,場景中的所有內容(所有SKSpriteNodes)都是backgroundNode的子代。

在我的GameScene.swift文件中,我將變量附加到背景節點和前景節點,以便在游戲進行時可以將子代添加到這些節點。

希望到目前為止這是清楚的...

現在,我向項目添加了其他5個* .sks文件。 這些文件包含我制作的場景,這些場景將通過GameScene.swift文件中的代碼添加為前景節點的子級。 這些場景文件中的SKSpriteNodes放置在前景中,但是它們的z位置小於背景子節點之一的z位置。 這是因為我要在光束后面顯示一個框(光束位於背景之外,並且框已添加到前景)。 這是一張圖片,以防萬一我感到困惑 添加到前景節點的盒子圖片出現在作為背景節點子元素的燈光后面

我的問題是...我想使用手勢識別器在屏幕上點擊,以便在點擊框時可以執行一些操作。 麻煩的是,由於光束具有較大的z位置(由於我想要的效果),所以每次我使用atPoint(_ p:CGPoint)-> SKNode方法來確定我點擊的哪個節點時,我都會返回光梁節點而不是箱形節點。

我如何只點擊框? 我已經嘗試將燈的isUserInteractionEnabled屬性更改為false,並且嘗試使用touchesBegan,如對類似問題的許多其他響應所示。 我也嘗試閱讀Apple提供的快速開發人員文檔,但是我無法弄清楚。

我的手勢識別器的代碼如下:

 //variables for gesture recognition
let tapGesture = UITapGestureRecognizer()
let swipeUpGesture = UISwipeGestureRecognizer()

//set up the tap gesture recognizer
tapGesture.addTarget(self, action: #selector(GameScene.tappedBox(_:)))
self.view!.addGestureRecognizer(tapGesture)
tapGesture.numberOfTapsRequired = 1
tapGesture.numberOfTouchesRequired = 1

//handles the tap event on the screen
@objc func tappedBox(_ recognizer: UITapGestureRecognizer) {

    //gets the location of the touch
    let touchLocation = recognizer.location(in: recognizer.view)
    if TESTING_TAP {
        print("The touched location is: \(touchLocation)")
    }

    //enumerates through the scenes children to see if the box I am trying to tap on can be detected (It can be detected using this just don't know how to actually detect it by tapping on the box)
    enumerateChildNodes(withName: "//Box1", using: { node, _ in
        print("We have found a single box node")
    })

    //tells me what node is returned at the tapped location
    let touchedNode = atPoint(touchLocation)
    print("The node touched was: \(String(describing: touchedNode.name))")

    //chooses which animation to run based on the game and player states
    if gameState == .waitingForTap && playerState == .idle{
        startRunning()              //starts the running animation
        unPauseAnimation()
    }else if gameState == .waitingForTap && playerState == .running {
        standStill()                //makes the player stand still
        unPauseAnimation()
    }
}

希望這對你們足夠了...如果您需要我提供更多代碼,或者需要我澄清任何內容,請告訴我

請閱讀代碼中的注釋。 您可以在spriteKit中輕松獲得所需的內容。 希望你得到答案。

     @objc func tappedBox(_ recognizer: UITapGestureRecognizer) {

     let touchLocation = recognizer.location(in: recognizer.view)

  // Before you check the position, you need to convert the location from view to Scene. That's the right location.
     let point =  (recognizer.view as! SKView).convert(touchLocation, to: self)

      print ( getNodesatPoint(point, withName: "whatever Node name"  ) )
   }

 // 2 : This function  gives you all nodes with the name you assign. If you node has a unique name, you got it.
 ///     You can change name to other properties and find out.

     private func   getNodesatPoint(_ point : CGPoint , withName name: String) -> [SKNode] {
           return self.nodes(at: point).filter{ $0.name == name}
   }

您想使用nodesAtPoint進行點擊測試,並獲取與該點關聯的所有節點。 然后過濾列表以查找所需的框。

如果要進行的圖層很多,則可能還需要在圖形上方添加一個不可見的圖層來處理被觸摸的節點

您的另一個選擇是關閉除框外的所有內容的isUserInteractionEnabled ,然后覆蓋框的觸摸事件,但這意味着您不能像現在那樣使用手勢。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM