lundi 29 juin 2015

Swift/SpriteKit - How to add points when an object collides with a player

I want to know how to give the player plus 1 point every time the player collides with that a certain object. Every time the object is collided with, it will at on to the previous amount. Example: if I have 5 coins and I collected 5 in this current game the overall amount of coins I will have is 10. It would be grate if you could link me to a place that has a tutorial of this in swift not in objective-c. Thanks!

Note: I have everything set-up with the coins spawning and the collision works but when the player collides the game ends.

Code, I have removed code that is not necessary:

import Foundation

import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate {

    var movingGround: PPMovingGround!
    var square1: PPSquare1!
    var wallGen: PPWallGen!
    var diamondGen: PPDiamondGen!

    var isStarted = false
    var screenTapped = AVAudioPlayer()
    var isGameOver = false
    var isDiamondContact = false

    override func didMoveToView(view: SKView) {



        addMovingGround()
        addSquare1()
        addDiamondGen()
        addWallGen()
        start()
        addDiamondsLabels()



    }

    func addMovingGround() {
        movingGround = PPMovingGround(size: CGSizeMake(view!.frame.width, kMLGroundHeight))
        movingGround.position = CGPointMake(0, view!.frame.size.height/2)
        addChild(movingGround)
    }

    func addSquare1() {
        square1 = PPSquare1()
        square1.position = CGPointMake(70, movingGround.position.y + movingGround.frame.size.height/2 + square1.frame.size.height/2)
        square1.zPosition = 1
        playerNode.addChild(square1)
    }

    func addDiamondGen() {
        diamondGen = PPDiamondGen(color: UIColor.clearColor(), size: view!.frame.size)
        diamondGen.position = view!.center
        addChild(diamondGen)
    }

    func addWallGen() {
        wallGen = PPWallGen(color: UIColor.clearColor(), size: view!.frame.size)
        wallGen.position = view!.center
        addChild(wallGen)
    }

    func addDiamondsLabels() {
        let diamondsLabel = PPDiamondsLabel(num: 0)
        diamondsLabel.name = "diamondPointsLabel"
        diamondsLabel.alpha = 0.50
        diamondsLabel.position.x = view!.center.x - 120
        diamondsLabel.position.y = view!.center.y + 1000
        diamondsLabel.fontColor = UIColor.whiteColor()
        diamondsLabel.fontName = "Helvetica"
        diamondsLabel.fontSize = 40
        addChild(diamondsLabel)

        let diamondTotalLabel = PPDiamondsLabel(num: 0)
        diamondTotalLabel.name = "diamondHighscoreLabel"
        diamondTotalLabel.alpha = 0.50
        diamondTotalLabel.position = CGPointMake(view!.frame.size.width - 40, view!.frame.size.height - 60)
        diamondTotalLabel.fontColor = UIColor.whiteColor()
        diamondTotalLabel.fontName = "Helvetica"
        diamondTotalLabel.fontSize = 24
        addChild(diamondTotalLabel)

        let diamondTotalTextLabel = SKLabelNode(text: "Diamonds: ")
        diamondTotalTextLabel.alpha = 0.95
        diamondTotalTextLabel.fontColor = UIColor.whiteColor()
        diamondTotalTextLabel.fontSize = 22.0
        diamondTotalTextLabel.fontName = "Helvetica"
        diamondTotalTextLabel.position = CGPointMake(-90.0,2.0)
        diamondTotalLabel.addChild(diamondTotalTextLabel)
    }


    func start() {
        isStarted = true

        square1.stop()
        movingGround.start()
        wallGen.startGenWallsEvery(1)
        diamondGen.startGenDiamondsEvery(1)
    }

    func collisionWithDiamond() {
        isDiamondContact = true

    }


    // MARK - Game Lifecycle


    func gameOver() {
        isGameOver = true

        // everything stops

        square1.fall()
        wallGen.stopWalls()
        diamondGen.stopDiamonds()
        movingGround.stop()
        square1.stop()


        // create game over label
        let gameOverLabel = SKLabelNode(text: "Game Over!")
        gameOverLabel.fontColor = UIColor.whiteColor()
        gameOverLabel.fontName = "Helvetica"
        gameOverLabel.position.x = view!.center.x
        gameOverLabel.position.y = view!.center.y + 80
        gameOverLabel.fontSize = 22.0
        addChild(gameOverLabel)



    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

    }

    override func update(currentTime: CFTimeInterval) {

    }



    // MARK: - SKPhysicsContactDelegate
    func didBeginContact(contact: SKPhysicsContact) {

        if !isGameOver {
            gameOver()
        } else {
            !isDiamondContact
            collisionWithDiamond()
        }

    }

Aucun commentaire:

Enregistrer un commentaire