Pong-重新启动后屏幕上没有球

时间:2015-02-23 07:24:43

标签: swift

我是Swift和SpriteKit的新手。我制作了一个简单的Pong播放器版本。比赛结束后,比赛重新开始。切换到Game Over场景并返回。之后我无法将球重新拿回屏幕。我不明白spawnBall()没有回复,而spawnPaddle和spawnBottom()都没有。请帮忙。

import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate {
    required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) }
    override init(size: CGSize) { super.init(size: CGSize()) }

    var gameScene: GameScene!
    let BallCategoryName = "ball" let BottomCategoryName = "bottom" let PaddleCategoryName = "paddle"
    var isFingerOnPaddle = false

    let BallCategory: UInt32 = 0x1 << 0 
    let BottomCategory : UInt32 = 0x1 << 1 
    let PaddleCategory : UInt32 = 0x1 << 3

    var score = 0
    var start: UIButton!
    var isRunning = false
    var scoreLabel = SKLabelNode()

    var ball = SKSpriteNode() 
    var paddle = SKSpriteNode() 
    var bottom = SKSpriteNode()
    var isBall = false

    func spawnBall() {

        ball = SKSpriteNode(imageNamed: "ball") 
        ball.position = CGPoint(x: CGRectGetMidX(frame), y: CGRectGetMidY(frame)) 
        ball.xScale = 0.1 
        ball.yScale = 0.1
        ball.hidden = false

        ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.size.height/2)
        ball.physicsBody?.categoryBitMask = BallCategory
        ball.physicsBody?.contactTestBitMask = BottomCategory
        ball.physicsBody?.restitution = 1.0
        ball.physicsBody?.linearDamping = 0.0
        ball.physicsBody?.angularDamping = 0.0

        addChild(ball)
        isBall = true
        println("***")
    }

    func spawnPaddle() {
        paddle = SKSpriteNode(imageNamed: "wit")
        paddle.position = CGPointMake(200, 50)
        paddle.xScale = 0.09
        paddle.yScale = 0.09

        paddle.physicsBody = SKPhysicsBody(rectangleOfSize: paddle.size)
        paddle.physicsBody?.categoryBitMask = PaddleCategory
        paddle.physicsBody?.contactTestBitMask = BottomCategory
        paddle.physicsBody?.restitution = 1.0
        paddle.physicsBody?.linearDamping = 0.0
        paddle.physicsBody?.angularDamping = 0.0
        paddle.physicsBody?.dynamic = false
        paddle.name = "paddle"
        addChild(paddle)
    }

    func spawnBottom() {
        bottom = SKSpriteNode(imageNamed: "wit")
        bottom.position = CGPointMake(0, -111)
        bottom.physicsBody = SKPhysicsBody(rectangleOfSize: bottom.size)
        bottom.physicsBody?.dynamic = false
        bottom.physicsBody?.restitution = 1
        bottom.physicsBody?.angularDamping = 0
        bottom.physicsBody?.linearDamping = 0.0
        bottom.physicsBody?.categoryBitMask = BottomCategory

        addChild(bottom)
        println("Bottom")
    }

    override func didMoveToView(view: SKView) {
        super.didMoveToView(view)

        physicsWorld.gravity = CGVectorMake(0, 0)
        physicsWorld.contactDelegate = self

        backgroundColor = UIColor.blackColor()

        scoreLabel.fontColor = UIColor.whiteColor()
        scoreLabel.fontName = "Avenir"
        scoreLabel.fontSize = 25
        scoreLabel.text = "0"
        scoreLabel.position = CGPoint(x:CGRectGetMidX(frame) + 140, y: CGRectGetMidY(frame))
        addChild(scoreLabel)

        //////THE LOOP/////////////////////////////////////////////
        let borderBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
        borderBody.friction = 0
        self.physicsBody = borderBody

        spawnPaddle()
        spawnBottom()
    }


    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        /* Called when a touch begins */

        var touch = touches.anyObject() as UITouch
        var touchLocation = touch.locationInNode(self)

        if let body = physicsWorld.bodyAtPoint(touchLocation) {
            if body.node?.name == PaddleCategoryName {
               println("On the paddle")
               isFingerOnPaddle = true
            }
        }
    }

    override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
        if isFingerOnPaddle {
            var touch = touches.anyObject() as UITouch
            var touchLocation = touch.locationInNode(self)
            var previousLocation = touch.previousLocationInNode(self)
            var paddleX = paddle.position.x + (touchLocation.x - previousLocation.x)

            paddleX = max(paddleX, paddle.size.width/2)
            paddleX = min(paddleX, size.width - paddle.size.width/2)

            paddle.position = CGPointMake(paddleX, paddle.position.y)
        }
    }

    func random(x: Int) -> Int {
        var y = arc4random_uniform(UInt32(x))
        return Int(y)
    }


    func start(sender: AnyObject) {
        if isBall == false {
            spawnBall()
            ball.physicsBody?.applyImpulse(CGVectorMake(12, 40))
            println("ball")
        }
    }

    override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
        isFingerOnPaddle = false
     }

    func didBeginContact(contact: SKPhysicsContact) {

        var firstBody: SKPhysicsBody
        var secondBody: SKPhysicsBody

        if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
            firstBody = contact.bodyA
            secondBody = contact.bodyB

         } else {
            firstBody = contact.bodyB
            secondBody = contact.bodyA
        }

        func changeScene() {
            let gameOverScene = GameOverScene(size: size)
            gameOverScene.scaleMode = scaleMode
            let reveal = SKTransition.flipHorizontalWithDuration(0.5)
            view?.presentScene(gameOverScene, transition: reveal)  
        }

        if firstBody.categoryBitMask == BallCategory && secondBody.categoryBitMask == BottomCategory {
            println("Hit bottom.")

            ball.physicsBody?.velocity = CGVectorMake(0, 0)

            isBall = false
            ball.removeFromParent()

            score++
            println(score)
            scoreLabel.text = String(score)
            isRunning = false
            if score >= 3 {
               ball.physicsBody?.velocity = CGVectorMake(0, 0)

               isBall = false
               ball.removeFromParent()
               println("Game Over")

               changeScene()
            }
        }
    }
}

0 个答案:

没有答案