将代码添加到我的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
我认为问题在pointCount函数 –
崩溃在哪里,并与什么错误?你添加了哪些代码导致了崩溃?它每次都会崩溃吗? –
@JaydeepVyas - 我认为你的东西是...... :-) –