-1

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.

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()
        }

    }
HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
Swift101
  • 81
  • 10
  • possible duplicate of [How would I save "coins" in my game in Swift SpriteKit?](http://stackoverflow.com/questions/28881480/how-would-i-save-coins-in-my-game-in-swift-spritekit) – Whirlwind Jun 29 '15 at 18:12
  • You can use NSUserDefaults to implement data persistence... Another way would be to use NSCoding...There are some examples about the topic so I guess you will easily find what you are interested in if you search this site. – Whirlwind Jun 29 '15 at 18:14

1 Answers1

0

You can create your SKSpriteNodes (that will collide) with a .physicsBody.categoryBitMask = WhateverBitMask and then in didBeginContact you can check to see if either contact.bodyA.categoryBitMask == WhateverBitMask or contact.bodyB.categoryBitMask == WhateverBitMask. If that is true increment your coin total. Of course you need to implement WhateverBitMask (see below)

Have a look at the section on Collisions at http://www.raywenderlich.com/87231/make-game-like-mega-jump-sprite-kit-swift-part-1 for further information

ozidom
  • 159
  • 1
  • 5