整数未更新

时间:2016-05-09 05:23:32

标签: swift sprite-kit

我制作的游戏包括一旦玩家死亡后出现的高分标签,以及重启按钮。总的来说,高分,这是一个整数,工作正常,但有一个问题。如果你在那一轮中达到了新的高分,那么你刚刚完成就必须再次死去才能显示新的高分。让我说我玩游戏时高分已经是15而且当高分标签出现时我得到17它仍然显示15.我重新开始游戏并且高分再次出现它现在将显示17.高分我想要的时候没有更新。

import SpriteKit

struct physicsCatagory {
    static let person : UInt32 = 0x1 << 1
    static let Ice : UInt32 = 0x1 << 2
    static let IceTwo : UInt32 = 0x1 << 3
    static let IceThree : UInt32 = 0x1 << 4
    static let Score : UInt32 = 0x1 << 5
}

class GameScene: SKScene, SKPhysicsContactDelegate {

    var Highscore = Int()
    var timeOfLastSpawn: CFTimeInterval = 0.0
    var timePerSpawn: CFTimeInterval = 1.2
    var scorenumber = Int()
    var lifenumber = Int()
    var SpeedNumber : Double = 0.5
    var person = SKSpriteNode(imageNamed: "Person1")
    let Score = SKSpriteNode()
    var ScoreLable = SKLabelNode()
    var Highscorelabel = SKLabelNode()
    let BackGround = SKSpriteNode (imageNamed: "BackGround")
    var restartButton = SKSpriteNode()
    var Died = Bool()

    func restartScene(){
        self.removeAllChildren()
        self.removeAllActions()
        scorenumber = 0
        lifenumber = 0
        createScene()
        random()
        //spawnThirdIce()
        Died = false
        timeOfLastSpawn = 0.0
        timePerSpawn = 1.2
    }

    func createScene(){
        physicsWorld.contactDelegate = self

        if (scorenumber > Highscore){
            var Highscoredefault = NSUserDefaults.standardUserDefaults()
            Highscoredefault.setValue(scorenumber, forKey: "HighScore")
        }

        var Highscoredefault = NSUserDefaults.standardUserDefaults()
        if (Highscoredefault.valueForKey("HighScore") != nil){
            Highscore = Highscoredefault.valueForKey("HighScore") as! NSInteger
        }
        else{
            Highscore = 0
        }

        lifenumber = 0
        SpeedNumber = 1

        BackGround.size = CGSize(width: self.frame.width, height: self.frame.height)
        BackGround.position = CGPointMake(self.size.width / 2, self.size.height / 2)
        BackGround.zPosition = -5
        self.addChild(BackGround)

        Score.size = CGSize(width: 2563, height: 1)
        Score.position = CGPoint(x: 320, y: -20)
        Score.physicsBody = SKPhysicsBody(rectangleOfSize: Score.size)
        Score.physicsBody?.affectedByGravity = false
        Score.physicsBody?.dynamic = false
        Score.physicsBody?.categoryBitMask = physicsCatagory.Score
        Score.physicsBody?.collisionBitMask = 0
        Score.physicsBody?.contactTestBitMask = physicsCatagory.IceThree
        Score.color = SKColor.blueColor()
        Score.zPosition = -5
        self.addChild(Score)

        person.zPosition = 1
        person.position = CGPointMake(self.size.width/2, self.size.height/9.5)
        person.setScale(0.6)
        person.physicsBody = SKPhysicsBody (rectangleOfSize: CGSize(width: 1000, height: 50))
        person.physicsBody?.affectedByGravity = false
        person.physicsBody?.categoryBitMask = physicsCatagory.person
        person.physicsBody?.contactTestBitMask = physicsCatagory.Ice
        person.physicsBody?.collisionBitMask = physicsCatagory.Ice
        person.physicsBody?.dynamic = false
        person.physicsBody?.affectedByGravity = false
                self.addChild(person)

        ScoreLable = SKLabelNode()
        ScoreLable.fontName = "Arial"
        ScoreLable.position = CGPoint(x: self.frame.width / 2, y: 1700)
        ScoreLable.text = "\(scorenumber)"
        ScoreLable.fontColor = UIColor.yellowColor()
        ScoreLable.fontSize = 150
        self.addChild(ScoreLable)

        Highscorelabel.fontName = "Arial"
        Highscorelabel.position = CGPoint(x: self.frame.width / 2, y: 1400)
        Highscorelabel.text = "HighScore: \(Highscore)"
        Highscorelabel.fontSize = 150
        Highscorelabel.fontColor = UIColor.yellowColor()
        Highscorelabel.zPosition = -7
        self.addChild(Highscorelabel)
    }

    func random() -> CGFloat{
        return CGFloat(Float(arc4random()) / 0xFFFFFFFF)
    }

    func random(min min: CGFloat, max: CGFloat) -> CGFloat{
        return random() * (max - min) + min
    }

    var gameArea: CGRect

    override init(size: CGSize) {
        let maxAspectRatio: CGFloat = 16.0/9.0
        let playableWidth = size.height / maxAspectRatio
        let margin = (size.width - playableWidth) / 2
        gameArea = CGRect(x: margin, y: 0, width: playableWidth, height: size.height)

        super.init(size: size)
    }

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

    override func didMoveToView(view: SKView) {
        createScene()
    }

    func createButton(){
        restartButton = SKSpriteNode(imageNamed: "Restart Button")
        restartButton.position = CGPoint(x: 768, y: 1024)
        restartButton.zPosition = 6
        restartButton.setScale(2.3)
        self.addChild(restartButton)
    }

     func didBeginContact(contact: SKPhysicsContact) {
            let firstBody = contact.bodyA
            let secondBody = contact.bodyB

        if firstBody.categoryBitMask == physicsCatagory.person && secondBody.categoryBitMask == physicsCatagory.IceThree || firstBody.categoryBitMask == physicsCatagory.IceThree && secondBody.categoryBitMask == physicsCatagory.person{

           scorenumber++

            if scorenumber == 20 {
              timePerSpawn = 1.0
            }
            if scorenumber == 40{
                timePerSpawn = 0.89
            }
            if scorenumber == 60{

                timePerSpawn = 0.6
            }
            if scorenumber == 80{
                timePerSpawn = 0.5
            }
            if scorenumber == 100{
                timePerSpawn = 0.4
            }
            if scorenumber == 120{
                timePerSpawn = 0.3
            }

            ScoreLable.text = "\(scorenumber)"
            CollisionWithPerson(firstBody.node as! SKSpriteNode, Person: secondBody.node as! SKSpriteNode)  
        }

        if firstBody.categoryBitMask == physicsCatagory.Score && secondBody.categoryBitMask == physicsCatagory.IceThree ||
            firstBody.categoryBitMask == physicsCatagory.IceThree && secondBody.categoryBitMask == physicsCatagory.Score{
                lifenumber++

                if lifenumber == 1{
                    //person.texture
                    person.texture = SKTexture (imageNamed: "Flower#2")
                }
                if lifenumber == 2{
            person.texture = SKTexture (imageNamed: "Flower#3")
        }
                if lifenumber == 3{
                   // self.addChild(Highscorelabel)
                    Highscorelabel.zPosition = 5
                    createButton()
                    person.zPosition = -6
                    person.texture = SKTexture (imageNamed: "Person1")
                    //person.removeFromParent()
                    Died = true
                }
        }
    }

    func CollisionWithPerson (Ice: SKSpriteNode, Person: SKSpriteNode){
      Person.removeFromParent()

       // if (scorenumber > Highscore){
         //   var Highscoredefault = NSUserDefaults.standardUserDefaults()
           // Highscoredefault.setValue(scorenumber, forKey: "HighScore")

        //} 
    }

    func spawnThirdIce(){
        if Died == true {
        } else
        {

        var Ice = SKSpriteNode(imageNamed: "Ice")
        Ice.zPosition = 2
        Ice.setScale(1.5)
        Ice.physicsBody = SKPhysicsBody(rectangleOfSize: Ice.size)
        Ice.physicsBody?.categoryBitMask = physicsCatagory.IceThree
        Ice.physicsBody?.contactTestBitMask = physicsCatagory.person | physicsCatagory.Score
        Ice.physicsBody?.affectedByGravity = false
        Ice.physicsBody?.dynamic = true

        let randomXStart = random(min:CGRectGetMinX(gameArea), max: CGRectGetMaxX(gameArea))
        let randomXend = random(min:CGRectGetMinX(gameArea),max: CGRectGetMaxX(gameArea))

        let startPoint = CGPoint(x: randomXStart, y: self.size.height * 1.2)
        let endpoint = CGPoint(x: randomXend, y: -self.size.height * 0.2)
        Ice.position = startPoint
        let moveEnemy = SKAction.moveTo(endpoint, duration: 2.0)
        let deleteEnemy = SKAction.removeFromParent()
        let enemySequence = SKAction.sequence([moveEnemy , deleteEnemy])
        Ice.runAction(enemySequence)

        self.addChild(Ice)
        }
    }

        override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
            for touch in touches{
                let location = touch.locationInNode(self)

                if Died == true{
                    if restartButton.containsPoint(location){
                        restartScene()
                    }
                }
            }
    }

    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
        if Died == true {
        }
        else{
        for touch: AnyObject in touches {
            let location = touch.locationInNode(self)
            let previousTouch = touch.previousLocationInNode(self)
            let ammountDragged = location.x - previousTouch.x

            person.position.x += ammountDragged

            if person.position.x > CGRectGetMaxX(gameArea) - person.size.width/2{
                person.position.x = CGRectGetMaxX(gameArea) - person.size.width/2
            }

                if person.position.x <  CGRectGetMinX(gameArea) + person.size.width/2{
                    person.position.x = CGRectGetMinX(gameArea) + person.size.width/2

            }
            }
        }
    }

    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
        if (currentTime - timeOfLastSpawn > timePerSpawn) {
            spawnThirdIce()
            self.timeOfLastSpawn = currentTime
        }
    }
}

好的,如果生命数= 3,我就加入了这个:

if lifenumber == 3{

                                 if (scorenumber > Highscore){
            var Highscoredefault = NSUserDefaults.standardUserDefaults()
            Highscoredefault.setValue(scorenumber, forKey: "HighScore")

        }


        var Highscoredefault = NSUserDefaults.standardUserDefaults()
        if (Highscoredefault.valueForKey("HighScore") != nil){

            Highscore = Highscoredefault.valueForKey("HighScore") as! NSInteger
        }
        else{
            Highscore = 0

        }
                    self.addChild(Highscorelabel)
                    createButton()
                    person.zPosition = -6
                    person.texture = SKTexture (imageNamed: "Person1")
                    Died = true
                }

2 个答案:

答案 0 :(得分:1)

初始化时,您似乎只设置了text的{​​{1}}属性。如果您希望Highscorelabel在死亡后立即更新,那么您应该在此时更新它。

Highscorelabel

答案 1 :(得分:0)

我认为你必须加上这个:

func didBeginContact(contact: SKPhysicsContact)

中的//ADD THIS if scorenumber > Highscore { Highscore = scorenumber Highscorelabel.text = "HighScore: \(Highscore)" } 之后

package msg_RepAndOpt;

message RepAndOpt
{
    repeated string name = 1;
    optional string surname = 2;
    ...
    // there are lots of others.
}