【问题标题】:Swift code crashing and not workingSwift 代码崩溃且无法正常工作
【发布时间】:2017-08-08 10:24:53
【问题描述】:

在我的update(_ currentTime: TimeInterval) 函数中添加代码后,我的应用程序代码继续崩溃。有没有更好的方法来表达我的代码,使其不会崩溃?

import SpriteKit
import GameplayKit

class GameScene: SKScene {

var startScreen = SKSpriteNode()
var Ball = SKSpriteNode()
var playerPaddle = SKSpriteNode()
var computerPaddle = SKSpriteNode()
var playerScore = SKLabelNode()
var comScore = SKLabelNode()

override func didMove(to view: SKView) {

    startScreen = childNode(withName: "startScreen") as! SKSpriteNode
    Ball = childNode(withName: "Ball") as! SKSpriteNode
    playerPaddle = childNode(withName: "playerPaddle") as! SKSpriteNode
    computerPaddle = childNode(withName: "computerPaddle") as! SKSpriteNode
    playerScore = childNode(withName: "playerScore") as! SKLabelNode
    comScore = childNode(withName: "comScore") as! SKLabelNode

    let bodyBorder = SKPhysicsBody(edgeLoopFrom: self.frame)
    bodyBorder.friction = 0
    bodyBorder.restitution = 1
    self.physicsBody = bodyBorder

    Ball.physicsBody?.applyImpulse(CGVector(dx:20, dy:10))
}

func gameBegin() {
    playerScore.text = String(0)
    comScore.text = String(0)
}

func playerPoint() {
    var comScoreInt: Int = Int(comScore.text!)!
    comScoreInt += 1
    comScore.text = String(comScoreInt)
    Ball.position = (CGPoint(x:0, y:0))
    Ball.physicsBody?.applyImpulse(CGVector(dx:-20,dy:-10))
}

func comPoint() {
    var playerScoreInt: Int = Int(playerScore.text!)!
    playerScoreInt += 1
    playerScore.text = String(playerScoreInt)
    Ball.physicsBody?.applyImpulse(CGVector(dx:20,dy:10))
    Ball.position = (CGPoint(x:0, y:0))

}

func pointCount() {
    computerPaddle.run(SKAction.moveTo(y: Ball.position.y, duration: 0.5))
    if Ball.position.x <= playerPaddle.position.x - 42 {
        playerPoint()
    } else if Ball.position.x >= computerPaddle.position.x + 42 {
        comPoint()
    }

}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches {
        let location = touch.location(in: self)

        playerPaddle.run(SKAction.moveTo(y: location.y, duration: 0.1))
    }
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches {
        let location = touch.location(in: self)

        playerPaddle.run(SKAction.moveTo(y: location.y, duration: 0))
    }
}


override func update(_ currentTime: TimeInterval) {
   pointCount()
}
}

我是 SpriteKit 的新手,所以我不知道如何让这段代码更有效率。帮助将不胜感激!

编辑:

速度很慢,需要数分钟才能识别得分和球位。 playerScore/comScore.text代码没有问题,在gamescene.sks中设置为0

【问题讨论】:

  • 我认为问题出在 pointCount 函数中
  • 崩溃的地方和错误是什么?您添加了哪些导致崩溃的代码?每次都崩溃吗?
  • @JaydeepVyas - 我认为你在做某事...... :-)
  • Int(playerScore.text!)!是问题

标签: swift function sprite-kit


【解决方案1】:

我认为问题是playerScore.textcomScore.text 可能是nil,当您尝试强制解包时应用程序崩溃。在代码中,您将值分配给func gameBegin() 中的comScore.textplayerScore.text。但它没有在任何地方调用。尝试调用该函数可能是didMoveto view 函数。

【讨论】:

    【解决方案2】:

    我认为问题在于铸造这条线

    Int(playerScore.text!)! and Int(comScore.text!)!
    

    相反,您必须像这样使用

    Int(comScore.text?) ?? 0
    Int(playerScore.text?) ?? 0
    

    【讨论】:

      【解决方案3】:

      关于你的崩溃,你忘记给playerScorecomScore 赋值(你可以在didMove 函数中调用你的函数gameBegin)并且编译器可能会崩溃到该行:

      var comScoreInt: Int = Int(comScore.text!)!
      

      因为comScore 文本为空。

      我认为函数pointCount() 可以通过以下方式更正:

      func pointCount() {
              if computerPaddle.action(forKey: "moveTo") == nil {
                  computerPaddle.run(SKAction.moveTo(y: Ball.position.y, duration: 0.5), withKey:"moveTo")
                  if Ball.position.x <= playerPaddle.position.x - 42 {
                      playerPoint()
                  } else if Ball.position.x >= computerPaddle.position.x + 42 {
                      comPoint()
                  }
              }
          }
      

      此更正仅在 computerPaddle 没有任何先前的“moveTo”执行时限制此操作的执行..

      【讨论】:

      • 这段代码并不慢,也不会崩溃,但是,它并没有为我注册一个点,只是来回弹跳
      猜你喜欢
      • 2021-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-21
      • 1970-01-01
      • 1970-01-01
      • 2017-02-28
      相关资源
      最近更新 更多