SpriteKit swift 3中消失的按钮和标签

时间:2017-01-31 20:51:14

标签: ios swift sprite-kit swift3

我在SpriteKit中使用swift 3进行简单的数学游戏,这是我在SpriteKit中的第二个项目,所以请耐心等待。我有奇怪的问题,因为有时我的应用程序工作,但有时我的标签和对象绝望。在我的应用程序中,顶部的橙色圆圈在标签中随机编号下降,当用户单击红线下面的一个按钮时,标签中的数字除以按钮上的数字,当分割的数字是相当于1个游戏应该停止,它可能会弹出带有文字的标签"你赢了!"。(照片)

[]

如果落球在数字相等之前触及红线1游戏应该停止,它可能会弹出带有文字的标签"游戏结束"。(照片)

enter image description here

有时其中一个标签会消失(照片1)和(照片2)

enter image description here enter image description here

落球后的数字有时会在分割(照片)后消失

disappearing label

或在游戏开始时。我不知道为什么会这样,但有时它会完美无缺。

我的GameScene代码:

import SpriteKit
import GameplayKit

class GameScene: SKScene {


enum gametype {
    case game
    case wait
}

override func didMove(to view: SKView) {

    print("test1")
    koniec = self.childNode(withName: "gameover") as! SKLabelNode
    ball = self.childNode(withName: "pilka") as! SKSpriteNode
    ball.position.y = view.frame.height/2 - 20
    numberinball = self.childNode(withName: "liczba") as! SKLabelNode
    numberinball.position.x = ball.position.x
    numberinball.position.y = (ball.position.y) - 14
    random()
    ball.alpha = 1
    numberinball.alpha = 1
    koniec.alpha = 0
    button2 = self.childNode(withName: "2") as! SKSpriteNode
    button3 = self.childNode(withName: "3") as! SKSpriteNode
    button4 = self.childNode(withName: "4") as! SKSpriteNode
    button5 = self.childNode(withName: "5") as! SKSpriteNode
    button6 = self.childNode(withName: "6") as! SKSpriteNode
    button7 = self.childNode(withName: "7") as! SKSpriteNode
    button8 = self.childNode(withName: "8") as! SKSpriteNode
    button9 = self.childNode(withName: "9") as! SKSpriteNode
    button2.alpha = 1
    button3.alpha = 1
    button4.alpha = 1
    button5.alpha = 1
    button6.alpha = 1
    button7.alpha = 1
    button8.alpha = 1
    button9.alpha = 1
    gamedidstart1 = .game
}


//Variables

var button2 = SKSpriteNode()
var button3 = SKSpriteNode()
var button4 = SKSpriteNode()
var button5 = SKSpriteNode()
var button6 = SKSpriteNode()
var button7 = SKSpriteNode()
var button8 = SKSpriteNode()
var button9 = SKSpriteNode()

var ball = SKSpriteNode()

var numberinball = SKLabelNode()
var koniec = SKLabelNode()

var liczba = Int()
var numberingame = 0

var gamedidstart1:gametype = .game

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

    for touch: AnyObject in touches {
        let location = touch.location(in: self)
        print(location)

        if gamedidstart1 == .game {
            if button2.contains(location) && numberingame % 2 == 0 {
                numberingame = numberingame/2
                print("number has been divided by 2")
                numberinball.alpha = 1
                numberinball.text = String(numberingame)
            }else if button3.contains(location) && numberingame % 3 == 0 {
                numberingame = numberingame/3
                print("number has been divided by 3")
                numberinball.alpha = 1
                numberinball.text = String(numberingame)
            }else if button4.contains(location) && numberingame % 4 == 0 {
                numberingame = numberingame/4
                print("number has been divided by 4")
                numberinball.alpha = 1
                numberinball.text = String(numberingame)
            }else if button5.contains(location) && numberingame % 5 == 0 {
                numberingame = numberingame/5
                print("number has been divided by 5")
                numberinball.alpha = 1
                numberinball.text = String(numberingame)
                print(numberinball.alpha)
            }else if button6.contains(location) && numberingame % 6 == 0 {
                numberingame = numberingame/6
                print("number has been divided by 6")
                numberinball.alpha = 1
                numberinball.text = String(numberingame)
            }else if button7.contains(location) && numberingame % 7 == 0 {
                numberingame = numberingame/7
                print("number has been divided by 7")
                numberinball.alpha = 1
                numberinball.text = String(numberingame)
            }else if button8.contains(location) && numberingame % 8 == 0 {
                numberingame = numberingame/8
                print("number has been divided by 8")
                numberinball.alpha = 1
                numberinball.text = String(numberingame)
            }else if button9.contains(location) && numberingame % 9 == 0 {
                numberingame = numberingame/9
                print("number has been divided by 9")
                numberinball.alpha = 1
                numberinball.text = String(numberingame)
            }
            print(numberingame)
            print(numberinball.alpha)
        }//numberinball.text = String(numberingame)
    }
}


//Movng the ball
func ballpos() {
    ball.run(SKAction.moveTo(y: (ball.position.y) - 30, duration: 1))
    numberinball.run(SKAction.moveTo(y: (numberinball.position.y) - 30, duration: 1))
}

//Randomizing number

func random() {

    var randomized = Int()

    repeat{
        randomized = Int(arc4random_uniform(100)+1)
        print("randomized liczba to: \(randomized)")
    }while sprawdz(Liczba: randomized) || randomized == 1
    numberingame = randomized
    numberinball.text = String(numberingame)
    print("liczba w labelu zostaje ustawiona na: \(randomized)")
}



//Checking if number is divided by 2,3 or 5

func sprawdz(Liczba:Int) ->Bool {

    var geted = Liczba
    print("liczba przyjęta to:\(geted)")
    while true {
        if geted % 2 == 0 && geted != 1{
            geted = geted/2
            print("gate if geted % 2 == 0 && geted != 1 positive, liczba po podziale to: \(geted)")
        }else if  geted == 1 {
            print("number is equal to one after while block for number two, number: \(geted)")
            return false
        }else if geted < 10 {
            print("number is lower than ten so its returning")
            return false
        }else {
            print("gate for loop while for number two negative")
            break
        }
    }

    while true {
        if geted % 3 == 0 && geted != 1{
            geted = geted/3
            print("gate if geted % 3 == 0 && geted != 1 positive, number after dividing: \(geted)")
        }else if  geted == 1 {
            print("number is equal to one after while block for number three, number: \(geted)")
            return false
        }else if geted < 10 {
            print("number is lower than 10: \(geted) so it is returned")
            return false
        }else {
            print("gate for loop while for number three negative")
            break
        }
    }

    while true {
        if geted % 5 == 0 && geted != 1{
            geted = geted/5
            print("gate % 5 == 0 && geted != 1 positive, number after dividing: \(geted)")
        }else if  geted == 1 {
            print("number is equal one after while block for number five: \(geted)")
            return false
        }else if geted < 10 {
            print("number is lower than 10 \(geted) so it is returned")
            return false
        }else {
            print("gate for loop while for number five negative")
            break
        }
    }
    return true
}



override func update(_ currentTime: TimeInterval) {


    if gamedidstart1 == .game {
        if ball.position.y <= koniec.position.y{
            koniec.alpha = 1
            koniec.text = "Game Over"
            ball.removeAllActions()
            ball.alpha = 0
            numberinball.alpha = 1
            numberinball.removeAllActions()
            gamedidstart1 = .wait
            //print(numberinball.alpha)
            //print(numberinball.color!)
        }else {
            ballpos()
            //print(numberinball.alpha)
            //print(numberinball.color!)
        }
        if numberingame == 1 {
            ball.removeAllActions()
            ball.alpha = 1
            numberinball.removeAllActions()
            numberinball.alpha = 1
            koniec.alpha = 1
            koniec.text = "You Win!"
            gamedidstart1 = .wait
            //print(numberinball.alpha)
            //print(numberinball.color!)
        }
    }
    //numberinball.fontColor = UIColor.white


}

}

PS。我设置了按钮和标签的alpha,以确保它们没有丢失,因为它们有alpha = 0

1 个答案:

答案 0 :(得分:2)

spritekit中

zPosition个节点是一场噩梦。除非是专门设置的,否则具有相同zPosition的节点可以随机地在前面或后面结束。

我建议确保需要出现在前面的节点的zPosition大于您希望在后台显示的节点。