2017-08-08 107 views
2

將代碼添加到我的update(_ currentTime: TimeInterval)函數後,我的應用程序的代碼繼續崩潰。有沒有更好的方式來表達我的代碼,使其不會崩潰?Swift code crashing and not working

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代碼,它被設置爲0 gamescene.sks

+0

我認爲問題在pointCount函數 –

+1

崩潰在哪裏,並與什麼錯誤?你添加了哪些代碼導致了崩潰?它每次都會崩潰嗎? –

+0

@JaydeepVyas - 我認爲你的東西是...... :-) –

回答

0

我相信問題是playerScore.textcomScore.text可能是nil和應用程序崩潰,當您嘗試強行解開它。在代碼中,您將func gameBegin()中的值分配給comScore.textplayerScore.text。但它並沒有被稱爲任何地方。嘗試調用該函數可能是didMoveto view函數。

0

我認爲問題是出在鑄造這一行

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

相反,你必須使用這樣

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

關於你的崩潰,你已經忘了給一個值playerScorecomScore(你可以調用函數中的函數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」的執行..

+0

這段代碼並不慢,也不會崩潰,但它並沒有爲我註冊一個點,只是來回跳動 –