SpriteKit 创建按钮问题

SpriteKit creating a button issues

我正在尝试使用此页面上找到的解决方案中的一些代码来创建按钮

class GameScene: SKScene {
   let button = SKSpriteNode(imageNamed: "yourImgName")

   override func didMoveToView(view: SKView) {
      button.name = "btn"
      button.size.height = 100
      button.size.width = 100
      button.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame) + 50)
      self.addChild(button)

      //Adjust button properties (above) as needed
      }

   override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
      let touch = touches.first
      let positionInScene = touch!.locationInNode(self)
      let touchedNode = self.nodeAtPoint(positionInScene)

    if let name = touchedNode.name {
        if name == "btn" {

            let yourNextScene = YourNextScene(fileNamed: "YourNextScene")
            self.view?.presentScene(yourNextScene!)

          }
      }
   }
}

我的当前代码应该让玩家在按下按钮时跳跃,但按下按钮时目前没有任何反应

import SwiftUI
import SpriteKit
import UIKit

class GameScene: SKScene {
    override func didMove(to view: SKView) {
        let button = SKSpriteNode(imageNamed: "playerOBJ")
        let player = SKSpriteNode(imageNamed: "playerOBJ")
        let playerRadius = player.frame.width / 2.0
    
    
        player.position = CGPoint(x: 200, y: 500)
        player.name = "Jimmy"
    
        addChild(player)
    
        player.physicsBody = SKPhysicsBody(circleOfRadius: playerRadius)
        player.physicsBody?.allowsRotation = false
        player.physicsBody?.friction = 0
        player.physicsBody?.restitution = 0
        player.zPosition = 100
    
        // Button
        button.name = "btn"
        button.size.height = 100
        button.size.width = 100
        button.position = CGPoint(x: 100, y: 100)
        self.addChild(button)
    
    
        // Physics
        physicsBody = SKPhysicsBody(edgeLoopFrom: frame.inset(by: UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)))
    
        Timer.scheduledTimer(withTimeInterval: 2, repeats: true) { _ in
            player.physicsBody?.applyForce(CGVector(dx: 100, dy: 1000))
        }
    
         func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
            let touch = touches.first
            let positionInScene = touch!.location(in: self)
            let touchedNode = self.atPoint(positionInScene)

            if let name = touchedNode.name {
                if name == "btn" {
                    player.physicsBody?.applyForce(CGVector(dx: 0, dy: 10000))
                }
            }
        }
    }
    override func update(_ currentTime: TimeInterval) { }
}

我想这可能是媒体根本没有重新定位的问题,但我不确定

您的主要问题是您将所有代码都放在 didMove 函数中。您将 touchesBegan 函数放在 didMove 函数中。当 didMove 功能完成时,touchesBegan 消失,因此 none 的触摸将在游戏中处理。

您还在 didMove 函数中将按钮和播放器声明为局部变量。

override func didMove(to view: SKView) {
    let button = SKSpriteNode(imageNamed: "playerOBJ")
    let player = SKSpriteNode(imageNamed: "playerOBJ")
    // Rest of function omitted
}

didMove函数结束时,按钮和播放器也会消失。播放器和按钮的图像名称也相同。

解决方法是使 GameScene class 的 buttonplayer 变量属性。也将 touchesBegan 移出 didMove 函数。

class GameScene: SKScene {
    // Declare button and player here.
    var button = SKSpriteNode()
    var player = SKSpriteNode()

    override func didMove(to view: SKView) {
        // Initialize button, player, and everything else here.
    }

    func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        // Code in touchesBegan func goes here.
    }
}