jeudi 31 mai 2018

My GameOver sprite is not appearing (swift)

This is my code. Is there a problem with my sprite, function or my PhisicsDelegate? I went to a few websites and according to them, this code should work. I am trying to create a breakout game and trying to create a game over sprite.

import SpriteKit import GameplayKit import AVFoundation

class GameScene: SKScene, SKPhysicsContactDelegate {

var fingerIsOnPaddle = false

let ballCategoryName = "ball"
let paddleCategoryName = "paddle"
let brickCategoryName = "brick"
let bottomCategoryName = "bottom"

let ballCategory:UInt32 = 0x1 << 0
let bottomCategory:UInt32 = 0x1 << 3
let brickCategory:UInt32 = 0x1 << 1
let paddleCategory:UInt32 = 0x1 << 2

override init(size: CGSize) {
    super.init(size: size)

    self.physicsWorld.contactDelegate = self

    let backgroundImage = SKSpriteNode(imageNamed: "bg")
    backgroundImage.position = CGPoint(x: self.frame.size.width / 2, y: self.frame.size.height / 2)
    self.addChild(backgroundImage)

    self.physicsWorld.gravity = CGVector(dx: 0, dy: -1)

    let worldBorder = SKPhysicsBody(edgeLoopFrom: self.frame)
    self.physicsBody = worldBorder
    self.physicsBody?.friction = 0
    worldBorder.restitution = 1



    let ball = SKSpriteNode(imageNamed: "ball")
    ball.name = ballCategoryName
    ball.position = CGPoint(x: self.frame.size.width / 4, y: self.frame.size.height / 4)
    self.addChild(ball)

    ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.frame.size.width / 2)
    ball.physicsBody?.friction = 0
    ball.physicsBody?.restitution = 1
    ball.physicsBody?.linearDamping = 0
    ball.physicsBody?.allowsRotation = true

    ball.physicsBody?.applyImpulse(CGVector(dx: 15, dy: -15))

    let paddle = SKSpriteNode(imageNamed: "paddle")
    paddle.name = paddleCategoryName
    paddle.position = CGPoint(x: frame.midX, y: paddle.frame.size.height * 2)

    self.addChild(paddle)

    paddle.physicsBody = SKPhysicsBody(rectangleOf: paddle.frame.size)
    paddle.physicsBody?.friction = 0
    paddle.physicsBody?.restitution = 0.1
    paddle.physicsBody?.isDynamic = false

    let bottomRect = CGRect(x: self.frame.origin.x, y: self.frame.origin.y, width: self.frame.size.width, height: 0.5)
    let bottom = SKNode()
    bottom.physicsBody = SKPhysicsBody(edgeLoopFrom: bottomRect)

    self.addChild(bottom)

    bottom.physicsBody?.categoryBitMask = bottomCategory
    ball.physicsBody?.categoryBitMask = ballCategory
    paddle.physicsBody?.categoryBitMask = paddleCategory

    ball.physicsBody?.contactTestBitMask = bottomCategory | brickCategory

    let numberOfRows = 3
    let numberOfBricks = 5
    let brickWidth = SKSpriteNode(imageNamed: "brick").size.width
    let padding:Float = 20

    let xOffset:Float = (Float(self.frame.size.width) - (Float (brickWidth) * Float(numberOfBricks) + padding * (Float(numberOfBricks) - 1))) / 2

    for index in 1 ... numberOfRows{

        var yOffset:CGFloat{
            switch index {
            case 1:
                return self.frame.size.height * 0.9
            case 2:
                return self.frame.size.height * 0.7
            case 3:
                return self.frame.size.height * 0.5
            default:
                return 0.2
            }
        }

        for index in 1 ... numberOfBricks {
            let brick = SKSpriteNode(imageNamed: "brick")

            let calc1:Float = Float(index) - 0.5
            let calc2:Float = Float(index) - 1

            brick.position = CGPoint(x: CGFloat(calc1 * Float(brick.frame.size.width) + calc2*padding + xOffset), y: yOffset)

            brick.physicsBody = SKPhysicsBody(rectangleOf: brick.frame.size)
            brick.physicsBody?.allowsRotation = false
            brick.physicsBody?.friction = 0
            brick.name = brickCategoryName
            brick.physicsBody?.categoryBitMask = brickCategory
            brick.physicsBody?.isDynamic = false

            self.addChild(brick)
        }
    }
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch = touches.first
    let touchLocation = touch?.location(in: self)

    let body: SKPhysicsBody? = self.physicsWorld.body(at: touchLocation!)

    if body?.node?.name == paddleCategoryName {
        fingerIsOnPaddle = true
    }
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    if fingerIsOnPaddle {
        let touch = touches.first
        let touchLoc = touch?.location(in: self)
        let prevTouchLoc = touch?.previousLocation(in: self)

        let paddle = self.childNode(withName: paddleCategoryName) as! SKSpriteNode

        var newXPos = paddle.position.x + ((touchLoc?.x)! - (prevTouchLoc?.x)!)

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

        paddle.position = CGPoint(x: newXPos, y: paddle.position.y)
    }
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    fingerIsOnPaddle = false
}


func didBegin(_ contact: SKPhysicsContact) {
    let bodyAName = contact.bodyA.node?.name
    let bodyBName = contact.bodyB.node?.name

    if bodyAName == "ball" && bodyBName == "brick" || bodyAName == "brick" && bodyBName == "ball"{
        if bodyAName == "brick" {
            contact.bodyA.node?.removeFromParent()
        } else if bodyBName == "brick" {
            contact.bodyB.node?.removeFromParent()

            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
            }

                if firstBody.categoryBitMask == ballCategory && secondBody.categoryBitMask == bottomCategory {
                    let gameOver = SKSpriteNode (imageNamed: "gameOver")
                    gameOver.position = CGPoint(x: self.frame.size.width / 2, y: self.frame.size.height / 2)
                    self.addChild(gameOver)

            }

    }
}

}

}




Aucun commentaire:

Enregistrer un commentaire