0

我有一个简单的游戏,玩家需要进行三轮比赛才能获得最高分。gameScene 存在于 SwiftUI 视图中,创建方式如下:

var gameScene: SKScene {
       let scene = NyonindoGameScene(
           size: CGSize(
               width: UIScreen.main.bounds.width,
               height: UIScreen.main.bounds.height
           )
       )
       scene.viewModel = self.viewModel
       scene.scaleMode = .aspectFill
       return scene
       }

使用 SpriteView() 从视图主体(在 ZStack 内的 GeometryReader 内)调用它。代码运行良好,直到我在新的 iPhone 13 上进行测试,这给了我各种古怪和意想不到的行为。我现在不会详细说明它们,因为我已经修复了大部分,但我仍然留下了一个“幻影”开始按钮。它旨在根据正在播放的回合显示不同的文本(即:“开始”、“再试一次”、“最后机会”),使用精确计数回合的变量。但是,我在第一轮结束时得到了这个:

重叠测试按钮的屏幕截图

当这个科学怪人按钮被点击时,新一轮开始。但是, SKPhysicsContactDelegate didBegin(_:) 不会被调用,并且冲突被忽略。(在这里我普遍感到困惑,我不知道这是一个单独的问题,还是在我解决重叠按钮问题时会消失的问题。)无论如何,这里是 startButton 的相关代码:

func addStartButton(text: String) {
        startButton.removeFromParent() // added as one of many failed remedies
        let startButtonLabel = SKLabelNode(text: text)
        startButtonLabel.fontName = SKFont.bold
        startButtonLabel.fontSize = 40.0
        startButtonLabel.fontColor = UIColor.white
        startButtonLabel.position = CGPoint(x: 0, y: -12)
        startButton.position = CGPoint(x:self.frame.midX, y:self.frame.midY)
        startButton.zPosition = 3
        startButton.addChild(startButtonLabel)
        addChild(startButton)
    }

在 didMove(查看:SKView)中,初始启动按钮的调用方式如下:

if attempts == 0 {
            addStartButton(text: "Start")
        }

第二轮和第三轮的按钮在 gameOver() 函数中调用,如下所示:

if attempts ==  1 {
  startButton.removeFromParent() // again, this is overkill as it gets removed before this...
  let text: String = "Try Again!"
  addStartButton(text: text)
            }
if attempts ==  2 {
  startButton.removeFromParent() 
  let text: String = "Last Chance!"
  addStartButton(text: text)
            }

我最初有一个 switch 语句而不是两个 if 语句,但这产生了同样的问题。打印到控制台的语句表明每一轮只调用一个按钮,但结果表明有些不同。有什么线索吗?(抱歉,如果我没有提供足够的代码进行评估。)

4

1 回答 1

1

你为什么要删除按钮?更改它的标签:

在此处输入图像描述

class TTESTGameScene: SKScene {
    
    var allBoxes: [SKSpriteNode] = []
    
    var startButton: SKShapeNode = SKShapeNode(rect: CGRect(x: 0, y: 0, width: 200, height: 43), cornerRadius: 20)
    
    override func didMove(to view: SKView) {
        physicsBody = SKPhysicsBody(edgeLoopFrom: frame)
        view.allowsTransparency = true
        self.backgroundColor = .clear
        view.alpha = 1.0
        view.isOpaque = true
        view.backgroundColor = SKColor.clear.withAlphaComponent(0.0)
        
        let nextButton = SKShapeNode(rect: CGRect(x: 0, y: view.frame.maxY - 40, width: 66, height: 33), cornerRadius: 20)
        nextButton.fillColor = .yellow
        nextButton.name = "nextButton"
        let nextLabel = SKLabelNode(text: "")
        nextLabel.fontSize = 40.0
        nextLabel.fontColor = UIColor.white
        nextLabel.position = CGPoint(x: 0, y: -12)
        nextButton.addChild(nextLabel)
        addChild(nextButton)
   
        
        startButton.fillColor = .red
        startButton.name = "startButton"

        let startButtonLabel = SKLabelNode(text: "000")
        startButtonLabel.fontSize = 30.0
        startButtonLabel.fontColor = UIColor.white
        startButtonLabel.horizontalAlignmentMode = .center
        
        startButtonLabel.position = CGPoint(x: startButton.frame.size.width/2, y: 10)
        startButtonLabel.name = "startButtonLabel"
        
        startButton.position = CGPoint(x:self.frame.midX - startButton.frame.size.width/2, y:self.frame.midY)
        startButton.zPosition = 3
        startButton.addChild(startButtonLabel)
        addChild(startButton)
                
    }

    var attempts: Int = 0
    
    func nextLevel() {
        //startButton.removeFromParent() // added as one of many failed remedies
        var text = ""
        if attempts == 0 {
            text = "Start"
        }
        else if attempts ==  1 {
            text = "Try Again!"
        }
        else if attempts ==  2 {
          text = "Last Chance!"
        }
        if let label = startButton.childNode(withName: "//startButtonLabel") as? SKLabelNode {
            label.text = text
            attempts += 1
            attempts = attempts > 2 ? 0:attempts
        }
    }
    
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard let touch = touches.first else { return }
        let location = touch.location(in: self.view)
        
        let sceneTouchPoint = self.convertPoint(fromView: location)
        let touchedNode = self.atPoint(sceneTouchPoint)
        print(touchedNode.name)
        if touchedNode.name == "nextButton" {
            nextLevel()
        }
    }
}

// A sample SwiftUI creating a GameScene and sizing it
// at 300x400 points
struct TTESTContentView: View {
    var scene: SKScene {
        let scene = TTESTGameScene()
        scene.size = CGSize(width: 300, height: 400)
        scene.scaleMode = .aspectFill
        return scene
    }

    var body: some View {
        SpriteView(scene: scene)
            .frame(width: 300, height: 400)
            //.ignoresSafeArea()
    }
}
struct ContentViewTest_Previews: PreviewProvider {
    static var previews: some View {
        TTESTContentView()
    }
}
于 2021-10-22T12:10:51.177 回答