标签没有显示(Swift 4 SpriteKit)

时间:2018-06-12 18:05:26

标签: swift sprite-kit swift4

我正在使用Swift 4,SpriteKit在Xcode中制作游戏。我的崩溃检测工作正常,但是当我尝试在播放器崩溃时在屏幕上显示文本时,文本不会出现。我的游戏基本上都有使用控制火箭飞船,它必须躲避流星,玩家也有发射子弹的能力。我已经检测到火箭飞船撞击流星但是无法将文字显示在屏幕上。我知道崩溃检测正在运行,因为每当我在didBegin函数中添加print语句时,就会执行print语句。所以它必须是我显示标签的方式,但我不知道我做错了什么标签。 这是我的代码:

import SpriteKit
import GameplayKit
import UIKit



class GameScene: SKScene, SKPhysicsContactDelegate{

    let player = SKSpriteNode(imageNamed: "spaceship")
    let stars = SKSpriteNode(imageNamed: "stars")
    let meteor = SKSpriteNode(imageNamed: "meteor")


    override func didMove(to view: SKView) {
        physicsWorld.contactDelegate = self
        print(frame.size.width)
        print(frame.size.height)

        stars.position = CGPoint(x:0, y:0)
        stars.zPosition = 1

        player.position = CGPoint(x:0, y:-320)
        player.zPosition = 4
        player.physicsBody = SKPhysicsBody(circleOfRadius: player.size.width / 2)
        player.physicsBody?.affectedByGravity = false
        player.physicsBody?.isDynamic = false

        player.physicsBody?.categoryBitMask = 2
        player.physicsBody?.collisionBitMask = 1
        player.physicsBody?.contactTestBitMask = 1



        self.addChild(player)
        self.addChild(stars)

        self.addMeteor()


    }

    func didBegin(_ contact: SKPhysicsContact) {
        gameOver()

    }

    func addMeteor() {
        meteor.physicsBody = SKPhysicsBody(circleOfRadius: meteor.size.width / 2)
        meteor.physicsBody?.affectedByGravity = false
        meteor.setScale(0.50)
        meteor.position = CGPoint(x:Int(arc4random()%300),y:Int(arc4random()%600))
        //meteor.position = CGPoint(x:0 , y:0)
        meteor.zPosition = 4

        meteor.physicsBody?.categoryBitMask = 1
        meteor.physicsBody?.collisionBitMask = 0
        meteor.physicsBody?.contactTestBitMask = 2

        self.addChild(meteor)
    }

    func fireBullet() {
        let bullet = SKSpriteNode(imageNamed: "bullet")
        bullet.position = player.position
        bullet.setScale(0.5)
        bullet.zPosition = 3
        self.addChild(bullet)
        let moveBullet = SKAction.moveTo(y: self.size.height + bullet.size.height, duration: 1)
        let deleteBullet = SKAction.removeFromParent()
        let bulletSequence = SKAction.sequence([moveBullet, deleteBullet])
        bullet.run(bulletSequence)


    }

    func gameOver() {
        print("Game Over!")
        var gameOverLabel: SKLabelNode!
        gameOverLabel = SKLabelNode(fontNamed: "Chalkduster")
        gameOverLabel.text = "Game Over! You lost!"
        gameOverLabel.horizontalAlignmentMode = .right
        gameOverLabel.position = CGPoint(x: 0, y:0)
        self.addChild(gameOverLabel)
        print("Label added")
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        fireBullet()
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        for touch:  AnyObject in touches {
            let pointOfTouch = touch.location(in: self)
            let previousPointOfTouch = touch.previousLocation(in: self)
            let amountDragged = pointOfTouch.x - previousPointOfTouch.x
            player.position.x += amountDragged
        }

    }



    override func update(_ currentTime: TimeInterval) {
        meteor.position.y -= 6
        if meteor.position.y < player.position.y - 300{
            meteor.removeFromParent()
            addMeteor()

        }
    }
}

不确定我做错了什么?我在didBegin中调用了gameOver()函数,该函数应该在每次检测到碰撞时执行。但没有任何反应?流星只是越过火箭飞船。如果有人可以帮助我那真棒!

2 个答案:

答案 0 :(得分:1)

正如我所知,您正试图将标签置于屏幕中心,您可以尝试将其添加到您的代码中,在gameover func上:

1.尝试将位置更改为

gameOverLabel.position = CGPoint(x: (self.scene!.frame.width / 2) - (self.scene!.frame.width * self.scene!.anchorPoint.x) / 2, y:(self.scene!.frame.height / 2) - (self.scene!.frame.height * self.scene!.anchorPoint.y))

2.我不知道你是否有超出标签的东西并影响其观点,所以试着:

gameOverLabel.zPosition = 10 //A number higher than any other zPosition 

3.标签可能太小了......试试:

gameOverLabel.fontSize = 30 //Adjust it to what you want

4.只是一个提示:

移动:

var gameOverLabel: SKLabelNode! 

之后的下一行
let meteor = SKSpriteNode(imageNamed: "meteor") 

这样您将拥有通用标签,而不必每次都重新生成标签。

如果你这样做,只是一个最后的提示......我不知道你是否有类似gameRestart(或类似的东西)的功能......但要确保在重启游戏时删除标签:

gameOverLabel.removeFromParent()

如果您不这样做,当应用尝试再次添加标签时,它会崩溃!

希望它有所帮助!

答案 1 :(得分:0)

就你所关心的物理而言,你没有移动任何物体。您需要施加力而不是手动移动位置。你的玩家动态也被设置为假,这意味着他从不移动,所以联系只发生在流星端。