首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swift代码崩溃,无法正常工作

Swift代码崩溃,无法正常工作
EN

Stack Overflow用户
提问于 2017-08-08 18:24:54
回答 3查看 59关注 0票数 2

在将代码添加到我的update(_ currentTime: TimeInterval)函数后,我的应用程序的代码继续崩溃。有没有更好的方式来表达我的代码,这样它就不会崩溃?

代码语言:javascript
复制
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

EN

回答 3

Stack Overflow用户

发布于 2017-08-08 18:33:36

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

票数 0
EN

Stack Overflow用户

发布于 2017-08-08 18:42:07

我想问题出在这条线上

代码语言:javascript
复制
Int(playerScore.text!)! and Int(comScore.text!)!

相反,您必须像这样使用

代码语言:javascript
复制
Int(comScore.text?) ?? 0
Int(playerScore.text?) ?? 0
票数 0
EN

Stack Overflow用户

发布于 2017-08-08 20:39:22

关于您的崩溃,您忘记了为playerScorecomScore赋值(您可以在didMove函数中调用您的函数gameBegin ),编译器可能会崩溃到下面这一行:

代码语言:javascript
复制
var comScoreInt: Int = Int(comScore.text!)!

因为comScore文本为空。

我认为可以使用以下命令来更正函数pointCount()

代码语言:javascript
复制
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“执行时,此更正才会限制此操作的执行。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45565788

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档