SKLabel不会出现

时间:2015-03-19 23:10:47

标签: swift sprite-kit

我正在使用精灵套件快速制作一个飞扬的鸟类游戏。游戏运行正常,但我试图让游戏得分,所以我设置了一个变量和SKLabel,只要管道在屏幕的一半左右,它就会自动增加。但是我无法获得标签以显示任何帮助,这是我的代码:

//  flappy rainbow sheep
//
//  Created by Heather Arnold on 3/3/15.
//  Copyright (c) 2015 ian arnold. All rights reserved.
//

import SpriteKit

class GameScene: SKScene {

var bird = SKSpriteNode()
var pipeUpTexture = SKTexture()
var pipeDownTexture = SKTexture()
var PipeMoveAndRemove = SKAction()
let pipeGap = 225.0
var score: Int = 0
var scoreLabel: SKLabelNode = SKLabelNode(fontNamed:"System-Bold")




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

    //Physics
    self.physicsWorld.gravity = CGVectorMake(0.0, -10.0);

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

    bird = SKSpriteNode(texture: BirdTexture)
    bird.setScale(0.5)
    bird.position = CGPoint(x: self.frame.size.width * 0.35, y: self.frame.size.height * 0.6)

    scoreLabel.position.x = 50
    scoreLabel.position.y = view.bounds.height - 50

    scoreLabel.text = "0"
    scoreLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.Left

    scoreLabel.hidden = false
    self.addChild(scoreLabel)

    bird.physicsBody = SKPhysicsBody(circleOfRadius:bird.size.height/4.0);
    bird.physicsBody?.dynamic = true
    bird.physicsBody?.allowsRotation = false

    self.addChild(bird)

    //off screen


    //Ground
    var groundTexture = SKTexture(imageNamed:"rainbowobstacle")
    var sprite = SKSpriteNode(texture: groundTexture)
    sprite.setScale(2.0)
    sprite.position = CGPointMake(self.size.width/2.0, sprite.size.height/2.0)

    self.addChild(sprite)

    var ground = SKNode()

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

    //Pipes

    //create the pipes
    pipeUpTexture = SKTexture(imageNamed:"rainbowpipe")
    pipeDownTexture = SKTexture(imageNamed:"rainbowpipe")

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

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

    //spwan pipes
    let spawn = SKAction.runBlock({() in self.spawnPipes()})
    let delay = SKAction.waitForDuration(NSTimeInterval(0.2))
    let spawnThenDelay = SKAction.sequence([spawn, delay])
    let spawnThenDelayForever = SKAction.repeatActionForever(spawnThenDelay)
    self.runAction(spawnThenDelayForever)

    let pipeUp = SKSpriteNode(texture:pipeUpTexture)

    if (pipeUp.position.x + (pipeUp.size.width / 2) < self.view!.bounds.size.width / 2)
    {
        score++

    }
    scoreLabel.hidden = false


}



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

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

    pipeDown.physicsBody = SKPhysicsBody(rectangleOfSize:pipeDown.size)
    pipeDown.physicsBody?.dynamic = false
    pipePair.addChild(pipeDown)

    let 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
    pipePair.addChild(pipeUp)

    pipePair.runAction(PipeMoveAndRemove)
    self.addChild(pipePair)





}





class GameScene: SKScene {

    let bird = SKSpriteNode()
    var gameOver = false

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

        self.bird.position = CGPoint(x: 0, y: 0)
        self.addChild(self.bird)

    }

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

    func endGame(){
        // restart the game
        let gameScene = GameScene(size: self.size)
        self.view!.presentScene(gameScene)
    }

    override func update(currentTime: NSTimeInterval) {


        // our bird doesnt intersect the frame,
        // we use gameOver bool so we dont call endGame more than once
        if !self.frame.intersects(self.bird.frame) && !self.gameOver{
            self.gameOver = true
            self.endGame()
        }

    }

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

    for touch: AnyObject in touches {

        let location = touch.locationInNode(self)

        bird.physicsBody?.velocity = CGVectorMake(0, 0)
        bird.physicsBody?.applyImpulse(CGVectorMake(0, 8))


    }
}

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

2 个答案:

答案 0 :(得分:0)

您的标签可能会被另一张图片覆盖。尝试将SKLabelNode zPosition属性设置为更高的值。也许像900这样的东西。

答案 1 :(得分:0)

尝试将标签的zPosition设置为1。

scoreLabel.zPosition = 1

即使另一个节点经过它,这也应该使节点出现。 以下是另外两个可能对您有帮助的功能:

scoreLabel.color = UIColor.blackColor()//set color to black - you can use any almost color here 
scoreLabel.fontSize = 32//changes font size to make label bigger/smaller