Swift代码崩溃且不工作

Swift代码崩溃且不工作,swift,function,sprite-kit,Swift,Function,Sprite Kit,将代码添加到我的更新(\uCurrentTime:TimeInterval)功能后,我的应用程序代码继续崩溃。有没有更好的方法来表达我的代码,这样它就不会崩溃 import SpriteKit import GameplayKit class GameScene: SKScene { var startScreen = SKSpriteNode() var Ball = SKSpriteNode() var playerPaddle = SKSpriteNode() var computer

将代码添加到我的
更新(\uCurrentTime: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
导入游戏工具包
类游戏场景:SKScene{
var startScreen=SKSpriteNode()
var Ball=SKSpriteNode()
var playerPaddle=SKSpriteNode()
var computerpaile=SKSpriteNode()
var playerScore=SKLabelNode()
var comScore=SKLabelNode()
覆盖func didMove(到视图:SKView){
startScreen=childNode(名称为:“startScreen”)为!SKSpriteNode
Ball=childNode(名称为“Ball”)as!SKSpriteNode
playerPaddle=childNode(名称为:“playerPaddle”)as!SKSpriteNode
ComputerPaile=childNode(名称为:“ComputerPaile”)作为!SKSpriteNode
playerScore=childNode(名称为:“playerScore”)作为!SKLabelNode
comScore=childNode(名称为:“comScore”)作为!SKLabelNode
让bodyBorder=SKPhysicsBody(edgeLoopFrom:self.frame)
bodyBorder.摩擦力=0
bodyBorder.Restoration=1
self.physicsBody=bodyBorder
Ball.physicsBody?applyImpulse(CGVector(dx:20,dy:10))
}
func gameBegin(){
playerScore.text=字符串(0)
comScore.text=字符串(0)
}
func playerPoint(){
var comScoreInt:Int=Int(comScore.text!)!
comScoreInt+=1
comScore.text=字符串(comScoreInt)
Ball.position=(CGPoint(x:0,y:0))
球.物理体?.applyImpulse(CGVector(dx:-20,dy:-10))
}
func comPoint(){
var playerScoreInt:Int=Int(playerScore.text!)!
playerScoreInt+=1
playerScore.text=字符串(playerScoreInt)
Ball.physicsBody?applyImpulse(CGVector(dx:20,dy:10))
Ball.position=(CGPoint(x:0,y:0))
}
func pointCount(){
电脑划桨。跑步(SKAction。moveTo(y:球。位置。y,持续时间:0.5))
如果Ball.position.x=计算机拨杆位置.x+42{
comPoint()
}
}
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
接触{
让位置=触摸。位置(in:self)
playerPaddle.run(SKAction.moveTo(y:location.y,duration:0.1))
}
}
覆盖功能触摸移动(touchs:Set,带有事件:UIEvent?){
接触{
让位置=触摸。位置(in:self)
playerPaddle.run(SKAction.moveTo(y:location.y,duration:0))
}
}
覆盖函数更新(uCurrentTime:TimeInterval){
点计数()
}
}
我是SpriteKit的新手,所以我不知道如何使这段代码更有效率。非常感谢您的帮助

编辑:


速度非常慢,需要几分钟才能识别分数和球的位置。playerScore/comScore.text代码没有问题,它在游戏场景中被设置为0。sks

我认为问题在于
playerScore.text
comScore.text
可能是
nil
,当你试图强制打开它时,应用程序崩溃。在代码中,您为
func gameBegin()
中的
comScore.text
playerScore.text
赋值。但它并没有在任何地方被称为。试着在maybe
didmove中调用那个函数来查看
函数。

我认为问题在于这一行的转换

Int(playerScore.text!)! and Int(comScore.text!)!
相反,你必须像这样使用

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

关于崩溃,您忘记给
playerScore
comScore
(您可以在
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()
            }
        }
    }
func pointCount(){
如果计算机浆动作(forKey:“moveTo”)==nil{
电脑划桨。跑步(SKAction.moveTo(y:Ball.position.y,持续时间:0.5),按键:“moveTo”)
如果Ball.position.x=计算机拨杆位置.x+42{
comPoint()
}
}
}

只有当
ComputerPable
没有任何以前的“moveTo”执行时,此更正才会限制此操作的执行。

我认为问题在于pointCount函数在何处发生崩溃以及出现了什么错误?你添加了什么导致崩溃的代码?它每次都会崩溃吗?@JaydeepVyas-我想你发现了什么…:-)Int(playerScore.text!)!问题是,这段代码速度不慢,也不会崩溃,但是,它没有为我记录一个点,只是来回跳转