斯威夫特 - 如何收集"钻石",添加"钻石"钻石?

时间:2015-06-24 20:23:00

标签: ios swift

我正在制作一个游戏,当玩家与钻石碰撞时,它会在钻石分数中加一个,如果他们再次玩当前 钻石分数添加上一个一个。如果用户关闭应用程序,我希望钻石保持不变并且不会重置。

到目前为止,我已经让钻石产生并随着游戏一起移动,但是当它发生碰撞时,游戏结束了。我不想要那个。

问题1 - 我想知道如何让玩家与钻石碰撞并收集它(removeFromIndex)我猜测,同时为钻石得分增加+1

问题2 - 我想知道如何将当前的钻石得分添加到总分上。因此,如果用户有5颗钻石并再次播放并收集4颗钻石,则用户应拥有的钻石总数为9颗钻石

代码:

import Foundation

import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate {

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

var isStarted = false
var isGameOver = false
var lastChangedBackground = 0
var lastMovingSpeed = 0
var background = SKSpriteNode()

var playerNode: SKNode!


override func didMoveToView(view: SKView) {
    //backgroundColor = UIColor.greenColor()
    //backgroundColor = UIColor(red: 223/255.0, green: 86/255.0, blue: 94/255.0, alpha: 1.0)

    background = SKSpriteNode(color: UIColor(red: 220/255.0, green: 220/255.0, blue: 220/255.0, alpha: 1.0), size: view.frame.size)


    background.position = view.center
    self.addChild(background)

    playerNode = SKNode()
   // playerNode.position = CGPointMake(40, 200)
    addChild(playerNode)






    addMovingGround()
    addSquare1()
    addEffect()
    addDiamondGen()
    addWallGen()
    addTapToStartLabel()
    addDiamondsLabels()
    addPointsLabels()
    addPhysicsWorld()
    loadHighscore()




}

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 addEffect() {
    var playerTrailPath = NSBundle.mainBundle().pathForResource("sparks", ofType: "sks")!
    var playerTrail = NSKeyedUnarchiver.unarchiveObjectWithFile(playerTrailPath) as! SKEmitterNode

    playerTrail.name = "playerTrail"
    playerTrail.position = CGPointMake(-30, -20)

    square1.addChild(playerTrail)


}

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 addTapToStartLabel() {
    let tapToStartLabel = SKLabelNode(text: "Tap to start!")
    tapToStartLabel.name = "tapToStartLabel"
    tapToStartLabel.position.x = view!.center.x
    tapToStartLabel.position.y = view!.center.y + 40
    tapToStartLabel.fontColor = UIColor.whiteColor()
    tapToStartLabel.fontName = "Helvetica"
    tapToStartLabel.fontSize = 22.0
    addChild(tapToStartLabel)
}

func addDiamondsLabels() {
    let diamondsLabel = PPDiamondsLabel(num: 0)
    diamondsLabel.name = "diamondPointsLabel"
    diamondsLabel.alpha = 0.50
    diamondsLabel.position.x = view!.center.x
    diamondsLabel.position.y = view!.center.y + 120
    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 - 30)
    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 addPointsLabels() {
    let pointsLabel = PPPointsLabel(num: 0)
    pointsLabel.name = "pointsLabel"
    pointsLabel.alpha = 0.50
    pointsLabel.position.x = view!.center.x
    pointsLabel.position.y = view!.center.y + 120
    pointsLabel.fontColor = UIColor.whiteColor()
    pointsLabel.fontName = "Helvetica"
    pointsLabel.fontSize = 40
    addChild(pointsLabel)

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

    let highscoreTextLabel = SKLabelNode(text: "Highscore: ")
    highscoreTextLabel.alpha = 0.95
    highscoreTextLabel.fontColor = UIColor.whiteColor()
    highscoreTextLabel.fontSize = 22.0
    highscoreTextLabel.fontName = "Helvetica"
    highscoreTextLabel.position = CGPointMake(-90.0,2.0)
    highscoreLabel.addChild(highscoreTextLabel)


}

func addPhysicsWorld() {
    physicsWorld.contactDelegate = self
}

func loadHighscore() {
    let defaults = NSUserDefaults.standardUserDefaults()

    let highscoreLabel = childNodeWithName("highscoreLabel") as! PPPointsLabel
    highscoreLabel.setTo(defaults.integerForKey("highscore"))
}


func start() {
    isStarted = true

    let tapToStartLabel = childNodeWithName("tapToStartLabel")
    tapToStartLabel?.removeFromParent()

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


// 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)

    // save current points label value
    let pointsLabel = childNodeWithName("pointsLabel") as! PPPointsLabel
    let highscoreLabel = childNodeWithName("highscoreLabel") as! PPPointsLabel

    let diamondsPointsLabel = childNodeWithName("diamondPointsLabel") as! PPDiamondsLabel
    let diamondHighscoreLabel = childNodeWithName("diamondHighscoreLabel") as! PPDiamondsLabel

    if highscoreLabel.number < pointsLabel.number {
        highscoreLabel.setTo(pointsLabel.number)

        let defaults = NSUserDefaults.standardUserDefaults()
        defaults.setInteger(highscoreLabel.number, forKey: "highscore")
    }

}


func restart() {

    let newScence = GameScene(size: view!.bounds.size)
    newScence.scaleMode = .AspectFill

    view!.presentScene(newScence)
}

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

    if isGameOver {
        restart()
    } else if !isStarted {
        start()
    } else {
        square1.flip()
    }

}

override func update(currentTime: CFTimeInterval) {



    if wallGen.wallTrackers.count > 0 {

        let wall = wallGen.wallTrackers[0] as PPWall

        let wallLocation = wallGen.convertPoint(wall.position, toNode: self)
        if wallLocation.x < square1.position.x {
            wallGen.wallTrackers.removeAtIndex(0)

            let pointsLabel = childNodeWithName("pointsLabel") as! PPPointsLabel
            pointsLabel.increment()



            if pointsLabel.number % 10 == 0 {
                kDefaultXToMovePerSecond == kDefaultXToMovePerSecond + 50
            }
            else if (pointsLabel.number % 20 == 0) {
                kDefaultXToMovePerSecond == kDefaultXToMovePerSecond + 50
            }
            else if (pointsLabel.number % 30 == 0) {
                kDefaultXToMovePerSecond == kDefaultXToMovePerSecond + 50
            }
            else if (pointsLabel.number % 40 == 0) {
                kDefaultXToMovePerSecond == kDefaultXToMovePerSecond + 50
            }
            else if (pointsLabel.number % 50 == 0) {
                kDefaultXToMovePerSecond == kDefaultXToMovePerSecond + 50
            }
            else if (pointsLabel.number % 60 == 0) {
                kDefaultXToMovePerSecond == kDefaultXToMovePerSecond + 50
            }
            else if (pointsLabel.number % 70 == 0) {
                kDefaultXToMovePerSecond == kDefaultXToMovePerSecond + 50
            }
            else if (pointsLabel.number % 80 == 0) {
                kDefaultXToMovePerSecond == kDefaultXToMovePerSecond + 50
            }




            if pointsLabel.number % 10 == 0 && pointsLabel.number != lastChangedBackground{
                lastChangedBackground = pointsLabel.number
                if (lastChangedBackground == 10) {
                    background.runAction(SKAction.colorizeWithColor(UIColor(red: 164/255, green: 200/255, blue: 237/255, alpha: 1),  colorBlendFactor: 0.1, duration: 1)) //light blue
                }
                else if (lastChangedBackground == 20) {
                    background.runAction(SKAction.colorizeWithColor(UIColor(red: 237/255, green: 164/255, blue: 236/255, alpha: 1), colorBlendFactor: 0.1, duration: 1)) //light purple
                }
                else if (lastChangedBackground == 30) {
                    background.runAction(SKAction.colorizeWithColor(UIColor(red: 164/255, green: 237/255, blue: 165/255, alpha: 1), colorBlendFactor: 0.1, duration: 1)) //light green
                }
                else if (lastChangedBackground == 40) {
                    background.runAction(SKAction.colorizeWithColor(UIColor(red: 237/255, green: 164/255, blue: 164/255, alpha: 1), colorBlendFactor: 0.1, duration: 1)) //light red
                }
                else if (lastChangedBackground == 50) {
                    background.runAction(SKAction.colorizeWithColor(UIColor(red: 000/255, green: 000/255, blue: 000/255, alpha: 1), colorBlendFactor: 0.1, duration: 1)) //black
                }
                else if (lastChangedBackground == 60) {
                    background.runAction(SKAction.colorizeWithColor(UIColor(red: 000/255, green: 128/255, blue: 255/255, alpha: 1), colorBlendFactor: 0.1, duration: 1)) // ultra blue
                }
                else if (lastChangedBackground == 70) {
                    background.runAction(SKAction.colorizeWithColor(UIColor(red: 88/255, green: 201/255, blue: 50/255, alpha: 1), colorBlendFactor: 0.1, duration: 1)) //ultra green
                }
                else if (lastChangedBackground == 80) {
                    background.runAction(SKAction.colorizeWithColor(UIColor(red: 245/255, green: 221/255, blue: 39/255, alpha: 1), colorBlendFactor: 0.1, duration: 1)) // ultra yellow
                }
                else if (lastChangedBackground == 90) {
                    background.runAction(SKAction.colorizeWithColor(UIColor(red: 245/255, green: 221/255, blue: 39/255, alpha: 1), colorBlendFactor: 0.1, duration: 1))
                }
                else if (lastChangedBackground == 100) {
                    background.runAction(SKAction.colorizeWithColor(UIColor(red: 245/255, green: 221/255, blue: 39/255, alpha: 1), colorBlendFactor: 0.1, duration: 1))
                }
            }

        }
    }
}



// MARK: - SKPhysicsContactDelegate
func didBeginContact(contact: SKPhysicsContact) {
    if !isGameOver {
        gameOver()
    }

}
  

提前致谢!

0 个答案:

没有答案