2015-03-19 2 views
1

Im делает игру с птичьим птицей с быстрым использованием набора спрайтов. Игра отлично работает, но я пытаюсь сделать так, чтобы забить игру, поэтому я установил переменную и SKLabel, которая добавляет к себе всякий раз, когда труба примерно на полпути по экрану. Однако я не могу получить ярлык, чтобы показать какую-либо помощь в этом, вот мой код: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 */ 
} 
} 

ответ

0

Ваш ярлык может быть покрыт другим изображением. Попробуйте установить свойство SKLabelNode zPosition на большее значение. Возможно, что-то вроде 900.

+0

я попробовал то, что вы сказали, и что до сих пор ничего нет – eman885113

+0

@ eman885113 - Может быть, что указанный шрифт не найден. Попробуйте использовать другой шрифт. Что-то вроде «Helvetica». – sangony

+0

спасибо за помощь, но он все еще не появился – eman885113

0

Попробуйте установить zPosition ярлыка на один.

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