【问题标题】:SKSpriteNode touch not detected未检测到 SKSpriteNode 触摸
【发布时间】:2018-06-24 02:32:16
【问题描述】:

我正在尝试在我的游戏中实现暂停按钮。但是,每次我点击 iOS 设备上的暂停按钮 (SKSpriteNode) 时,什么都没有发生。我尝试让按钮执行其他操作,并尝试让其他精灵执行操作。尽管我可以触摸屏幕上的任何位置并执行操作,但都没有工作。我正在将 Swift 4 与最新版本的 Xcode (9.4.1) 一起使用。该应用是一款 iOS 游戏,我使用的是随应用一起创建的 GameScene.swift。

这里是按钮的部分代码(代码的无关部分被省略了):

import SpriteKit
import CoreMotion
import GameplayKit

class GameScene: SKScene, SKPhysicsContactDelegate {

var pauseButton:SKSpriteNode!

override func didMove(to view: SKView) {

    pauseButton = SKSpriteNode(imageNamed: "pauseButton")
    pauseButton.size = CGSize(width: 50, height: 50)
    pauseButton.position = CGPoint(x: self.size.width - 
    pauseButton.size.width, y: self.size.height - 
        pauseButton.size.height)
    pauseButton.zPosition = 5
    self.addChild(pauseButton)

}

override func touchesEnded(_ touches: Set<UITouch>, with: UIEvent?) {

    fireBullet() //This function does not relate to the pause button.

}

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

    let touch = touches.first

        if let location = touch?.location(in: self) {
            let nodesArray = self.nodes(at: location)

        if nodesArray.first?.name == "pauseButton" {
            self.view?.isPaused = true
        }
    }
}
}

在此先感谢您抽出宝贵时间回复,这对我很有帮助! 托马斯

【问题讨论】:

  • "pauseButton" 是图像名称,而不是设置为节点名称属性的名称。

标签: ios swift sprite-kit touch skspritenode


【解决方案1】:

简单修复,在您的 touchesBegan 方法中,您正在搜索名为“pauseButton”的节点上的触摸,但没有名为“pauseButton”的节点,因此您的搜索返回一个错误值。

只需将pauseButton.name = "pauseButton" 添加到您设置暂停按钮的代码中即可。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多