Swift 代码崩溃且无法正常工作

标签 swift function sprite-kit

将代码添加到我的 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

最佳答案

我认为问题出在 playerScore.textcomScore.text 可能是 nil 并且当您尝试强制解包时应用程序崩溃.在代码中,您在 func gameBegin() 中为 comScore.textplayerScore.text 赋值。但它不会在任何地方被调用。尝试在 didMoveto view 函数中调用该函数。

关于Swift 代码崩溃且无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45565788/

相关文章:

ios - 如何限制触摸输入?

ios - 如何在 Swift Xcode 中为所有设备在整个屏幕周围制作边框?

ios - 比较日期组件 Swift

swift - 在 Swift 5.5 中编写同步和异步函数

ios - 更新我的 pod 会导致 GoogleDataTransport Umbrella header 中出现错误

function - LLVM 插入内在函数 Cos

ios - 是否可以降低状态栏的高度?

javascript - jQuery run 函数 onload 和 key up

c - Visual Studio C 函数语法错误

ios - SpriteKit : how to turn touches into vectors of uniform speed?