2017-04-02 105 views
0

我在一個Swift Playground上有一個乒乓球遊戲,我通過在線教程做了一個教程,但是我的助理編輯不會顯示任何東西!我的乒乓球比賽在哪裏!?我的代碼在圖片下面,顯示沒有任何東西顯示出來。爲什麼我的Swift Playground中不會出現任何東西?

enter image description here

import SpriteKit 
import PlaygroundSupport 

// Declare some global constants 
let width = 800 as CGFloat 
let height = 1200 as CGFloat 
let racketHeight = 150 as CGFloat 
let ballRadius = 20 as CGFloat 

// Three types of collision objects possible 
enum CollisionTypes: UInt32 { 
    case Ball = 1 
    case Wall = 2 
    case Racket = 4 
} 

// Racket direction 
enum Direction: Int { 
    case None = 0 
    case Up = 1 
    case Down = 2 
} 

// Make a SpriteKit scene 
class gameScene: SKScene, SKPhysicsContactDelegate { 
    let racketSpeed = 500.0 
    var direction = Direction.None 
    var score = 0 
    var gameRunning = false 

    // Screen elements 
    var racket: SKShapeNode? 
    var ball: SKShapeNode? 
    let scoreLabel = SKLabelNode() 

    // Initialize objects during first start 
    override func sceneDidLoad() { 
     super.sceneDidLoad() 
     scoreLabel.fontSize = 40 
     scoreLabel.position = CGPoint(x: width/2, y: height - 100) 
     self.addChild(scoreLabel) 

     createWalls() 
     createBall(position: CGPoint(x: width/2, y: height/2)) 
     createRacket() 
     startNewGame() 
     self.physicsWorld.contactDelegate = self 
    } 

    // Create the ball sprite 
    func createBall(position: CGPoint) { 
     let physicsBody = SKPhysicsBody(circleOfRadius: ballRadius) 
     ball = SKShapeNode(circleOfRadius: ballRadius) 
     physicsBody.categoryBitMask = CollisionTypes.Ball.rawValue 
     physicsBody.collisionBitMask = CollisionTypes.Wall.rawValue | CollisionTypes.Ball.rawValue | CollisionTypes.Racket.rawValue 
     physicsBody.affectedByGravity = false 
     physicsBody.restitution = 1 
     physicsBody.linearDamping = 0 
     physicsBody.velocity = CGVector(dx: -500, dy: 500) 
     ball!.physicsBody = physicsBody 
     ball!.position = position 
     ball!.fillColor = SKColor.white 
    } 

    // Create the walls 
    func createWalls() { 
     createWall(rect: CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: ballRadius, height: height))) 
     createWall(rect: CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: width, height: ballRadius))) 
     createWall(rect: CGRect(origin: CGPoint(x: 0, y: height - ballRadius), size: CGSize(width: width, height: ballRadius))) 
    } 

    func createWall(rect: CGRect) { 
     let node = SKShapeNode(rect: rect) 
     node.fillColor = SKColor.white 
     node.physicsBody = getWallPhysicsbody(rect: rect) 
     self.addChild(node) 
    } 

    // Create the physics objetcs to handle wall collisions 
    func getWallPhysicsbody(rect: CGRect) -> SKPhysicsBody { 
     let physicsBody = SKPhysicsBody(rectangleOf: rect.size, center: CGPoint(x: rect.midX, y: rect.midY)) 
     physicsBody.affectedByGravity = false 
     physicsBody.isDynamic = false 
     physicsBody.collisionBitMask = CollisionTypes.Ball.rawValue 
     physicsBody.categoryBitMask = CollisionTypes.Wall.rawValue 
     return physicsBody 
    } 

    // Create the racket sprite 
    func createRacket() { 
     racket = SKShapeNode(rect: CGRect(origin: CGPoint.zero, size: CGSize(width: ballRadius, height: racketHeight))) 
     self.addChild(racket!) 
     racket!.fillColor = SKColor.white 
     let physicsBody = SKPhysicsBody(rectangleOf: racket!.frame.size, center: CGPoint(x: racket!.frame.midX, y: racket!.frame.midY)) 
     physicsBody.affectedByGravity = false 
     physicsBody.isDynamic = false 
     physicsBody.collisionBitMask = CollisionTypes.Ball.rawValue 
     physicsBody.categoryBitMask = CollisionTypes.Racket.rawValue 
     physicsBody.contactTestBitMask = CollisionTypes.Ball.rawValue 
     racket!.physicsBody = physicsBody 
    } 

    // Start a new game 
    func startNewGame() { 
     score = 0 
     scoreLabel.text = "0" 
     racket!.position = CGPoint(x: width - ballRadius * 2, y: height/2) 
     let startLabel = SKLabelNode(text: "Game Over") 
     startLabel.position = CGPoint(x: width/2, y: height/2) 
     startLabel.fontSize = 160 
     self.addChild(startLabel) 

     // Animated countdown 
     let fadeIn = SKAction.fadeIn(withDuration: 0.5) 
     let fadeOut = SKAction.fadeOut(withDuration: 0.5) 
     startLabel.text = "3" 
     startLabel.run(SKAction.sequence([fadeIn, fadeOut]), completion: { 
      startLabel.text = "2" 
      startLabel.run(SKAction.sequence([fadeIn, fadeOut]), completion: { 
       startLabel.text = "1" 
       startLabel.run(SKAction.sequence([fadeIn, fadeOut]), completion: { 
        startLabel.text = "0" 
        startLabel.run(SKAction.sequence([fadeIn, fadeOut]), completion: { 
         startLabel.removeFromParent() 
         self.gameRunning = true 
         self.ball!.position = CGPoint(x: 30, y: height/2) 
         self.addChild(self.ball!) 
        }) 
       }) 
      }) 
     }) 
    } 

    // Handle touch events to move the racket 
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
     for touch in touches { 
      let location = touch.location(in: self) 
      if location.y > height/2 { 
       direction = Direction.Up 
      } else if location.y < height/2{ 
       direction = Direction.Down 
      } 
     } 
    } 

    // Stop racket movement 
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
     direction = Direction.None 
    } 

    // Game loop: 
    var dt = TimeInterval(0) 
    override func update(_ currentTime: TimeInterval) { 
     if gameRunning { 
      super.update(currentTime) 
      checkGameOver() 
      if dt > 0 { 
       moveRacket(dt: currentTime - dt) 
      } 
      dt = currentTime 
     } 
    } 

    // Move the racket up or down 
    func moveRacket(dt: TimeInterval) { 
     if direction == Direction.Up && racket!.position.y < height - racketHeight { 
      racket!.position.y = racket!.position.y + CGFloat(racketSpeed * dt) 
     } else if direction == Direction.Down && racket!.position.y > 0 { 
      racket!.position.y = racket!.position.y - CGFloat(racketSpeed * dt) 
     } 
    } 

    // Check if the ball is still on screen 
    // Game Over animation 
    func checkGameOver() { 
     if ball!.position.x > CGFloat(width) { 
      gameRunning = false 
      ball!.removeFromParent() 
      let gameOverLabel = SKLabelNode(text: "Game Over") 
      gameOverLabel.position = CGPoint(x: width/2, y: height/2) 
      gameOverLabel.fontSize = 80 
      self.addChild(gameOverLabel) 

      // Game Over animation 
      let rotateAction = SKAction.rotate(byAngle: CGFloat(M_PI), duration: 1) 
      let fadeInAction = SKAction.fadeIn(withDuration: 2) 
      gameOverLabel.run(SKAction.repeat(rotateAction, count: 2)) 
      gameOverLabel.run(SKAction.scale(to: 0, duration: 2.5), completion: { 
       gameOverLabel.removeFromParent() 
       self.startNewGame() 
      }) 
     } 
    } 

    // Detect collisions between ball and racket to increase the score 
    func didBegin(_ contact: SKPhysicsContact) { 
     if contact.bodyA.categoryBitMask == CollisionTypes.Racket.rawValue || contact.bodyB.categoryBitMask == CollisionTypes.Racket.rawValue { 
      score += 1 
      scoreLabel.text = String(score) 
     } 
    } 
} 

//Initialize the playground and start the scene: 
let skView = SKView(frame: CGRect(origin: CGPoint.zero, size: CGSize(width: width, height: height))) 
let scene = gameScene(size: skView.frame.size) 
skView.presentScene(scene) 

PlaygroundPage.current.liveView = skView 

回答

2

這發生在我身上的時候。使用SpriteKit的運動場不會在時間線視圖中顯示任何內容 - 您確實已打開時間線視圖,對嗎?如果不這樣做,請點擊工具欄上的「顯示助理編輯器」按鈕以打開時間線視圖。

如果您打開時間線視圖並沒有顯示任何內容,請嘗試關閉Xcode並重新啓動它。這通常爲我解決這個問題。

相關問題