簡體   English   中英

使用Sprite Kit和Swift在游戲中顯示當前得分

[英]Display Current Score in Game using Sprite Kit and Swift

我已經在Sprite Kit中開發了Flappy Bird類型的游戲已有幾天了。 我是編程新手,所以我一直在嘗試通過各種在線視頻和教程來教自己制作游戲。 現在我需要創建一個評分系統,該評分系統將在屏幕上實時顯示當前分數。 我一直在尋找關於此的好的教程,但是沒有運氣。 我已經在下面的代碼中設置了得分標簽。 我已將整個場景包括在內。 我所需要知道的是,每次鳥飛過管道時如何更新分數標簽文本。 任何意見或建議,將不勝感激,謝謝。

//
//  ArcheryScene.swift
//  FlappyBird (swift)
//
//  Created by Brandon Ballard on 1/6/15.
//  Copyright (c) 2015 Brandon Ballard. All rights reserved.
//

import UIKit
import SpriteKit

class ArcheryScene: SKScene, SKPhysicsContactDelegate {

var bird = SKSpriteNode()
var pipeUpTexture = SKTexture()
var pipeDownTexture = SKTexture()
var pipesMoveAndRemove = SKAction()
var impulse = 1
var count = 0
var scoreLabel = SKLabelNode()
let scoreLabelName = "scoreLabel"

let pipeGap = 150.0

enum ColliderType:UInt32 {
    case BIRD = 1
    case PIPE = 2
}

override func didMoveToView(view: SKView) {
    /* Setup your scene here */

    backgroundColor = SKColor.cyanColor()

    //physics
    self.physicsWorld.gravity = CGVectorMake(0.0, -15.0);
    self.physicsWorld.contactDelegate = self

    //Bird
    var birdTexture = SKTexture(imageNamed:"Bird")
    birdTexture.filteringMode = SKTextureFilteringMode.Nearest

    bird = SKSpriteNode(texture: birdTexture)
    bird.setScale(0.6)
    bird.position = CGPoint(x: self.frame.width * 0.35 + 20, y: self.frame.size.height * 0.95)

    bird.physicsBody = SKPhysicsBody(circleOfRadius: bird.size.height / 2.0)
    bird.physicsBody?.dynamic = true
    bird.physicsBody?.allowsRotation = true
    bird.physicsBody?.affectedByGravity = true
    bird.physicsBody!.collisionBitMask = ColliderType.BIRD.rawValue
    bird.physicsBody!.contactTestBitMask = ColliderType.PIPE.rawValue
    self.addChild(bird)

    //Ground
    var groundTexture = SKTexture(imageNamed: "Ground")

    var sprite = SKSpriteNode(texture: groundTexture)
    sprite.setScale(2.0)
    sprite.position = CGPointMake(self.size.width / 2, sprite.size.height / 2.0)

    self.addChild(sprite)

    var ground = SKNode()

    ground.position = CGPointMake(0, groundTexture.size().height + 0)
    ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width, groundTexture.size().height * 2.0))
    ground.physicsBody?.dynamic = false
    self.addChild(ground)

    //Score Label

    scoreLabel = SKLabelNode(fontNamed: "ScoreLabel")
    scoreLabel.name = scoreLabelName
    scoreLabel.fontSize = 125
    scoreLabel.fontColor = SKColor.whiteColor()
    scoreLabel.text = "\(count)"
    println(size.height)
    scoreLabel.position = CGPointMake(frame.size.width / 2, frame.size.height / 14)
    self.addChild(scoreLabel)

    //Pipes

    //Create the Pipes

    pipeUpTexture = SKTexture(imageNamed: "PipeUp")
    pipeDownTexture = SKTexture(imageNamed: "PipeDown")

    //Movement of Pipes

    let distanceToMove = CGFloat(self.frame.size.width + 2.0 * pipeUpTexture.size().width)
    let movePipes = SKAction.moveByX(-distanceToMove, y: 0.0, duration: NSTimeInterval(0.01 * distanceToMove))
    let removePipes = SKAction.removeFromParent()

    pipesMoveAndRemove = SKAction.sequence([movePipes,removePipes])

    //Spawn Pipes

    let spawn = SKAction.runBlock({() in self.spawnPipes()})
    let delay = SKAction.waitForDuration(NSTimeInterval(2.0))
    let spawnThenDelay = SKAction.sequence([spawn,delay])
    let spawnThenDelayForever = SKAction.repeatActionForever(spawnThenDelay)

    self.runAction(spawnThenDelayForever)

    }

func spawnPipes() {

    let pipePair = SKNode()
    pipePair.position = CGPointMake(self.frame.size.width + pipeUpTexture.size().width * 2, 0)
    pipePair.zPosition = -10

    let height = UInt32(self.frame.size.height / 4)
    let y = arc4random() % height + height

    var pipeDown = SKSpriteNode(texture: pipeDownTexture)
    pipeDown.setScale(2.0)////////
    pipeDown.position = CGPointMake(3.0, CGFloat(y) + pipeDown.size.height + CGFloat(pipeGap) )

    pipeDown.physicsBody = SKPhysicsBody(rectangleOfSize: pipeDown.size)
    pipeDown.physicsBody?.dynamic = false
    pipeDown.physicsBody!.affectedByGravity = false
    pipeDown.physicsBody!.collisionBitMask = ColliderType.PIPE.rawValue
    pipePair.addChild(pipeDown)

    var pipeUp = SKSpriteNode(texture: pipeUpTexture)
    pipeUp.setScale(2.0)
    pipeUp.position = CGPointMake(0.0, CGFloat(y))

    pipeUp.physicsBody = SKPhysicsBody(rectangleOfSize: pipeUp.size )
    pipeUp.physicsBody?.dynamic = false
    pipeUp.physicsBody!.affectedByGravity = false
    pipeUp.physicsBody!.collisionBitMask = ColliderType.PIPE.rawValue
    pipePair.addChild(pipeUp)

    pipePair.runAction(pipesMoveAndRemove)
    self.addChild(pipePair)

}

func didBeginContact(contact: SKPhysicsContactDelegate) {

    impulse = 0

    let fadeOut = SKAction.sequence([SKAction.waitForDuration(3.0)])

    let welcomeReturn = SKAction.runBlock({
        let Transition = SKTransition.revealWithDirection(SKTransitionDirection.Down, duration: 1.0)
        let welcomeScene = GameScene(fileNamed: "GameScene")
        welcomeScene.scaleMode = .AspectFill
        self.scene!.view?.presentScene(welcomeScene, transition: Transition)
    })

    let sequence = SKAction.sequence([fadeOut, welcomeReturn])
    self.runAction(sequence)
}

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

    for touch: AnyObject in touches {

        let location = touch.locationInNode(self)

        if bird.position.y > (self.frame.size.height * 0.999){
            impulse = 0
        }

        if impulse == 1 {
            bird.physicsBody?.velocity = CGVectorMake( 0, 0 )
            bird.physicsBody?.applyImpulse(CGVectorMake(0,25))
        }

    }
}

override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */
}
}

只需在管道之間設置一個看不見的精靈即可。 當您的鳥經過時,您可以檢測到並提高得分。

你可以像這樣制作一個看不見的精靈

let scoreSprite = SKSpriteNode(color: SKColor.clearColor(), size: theSize)

在上面添加一個物理物體,您就可以開始使用了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM